http://poj.org/problem?id=2117

题意:
求删除图中任意一个顶点后的最大连通分量数。

思路:

求出每个割点对应的连通分量数,注意这道题目中图可能是不连通的。

这道题目我wa了很多发,主要是我忘了根结点的连通分量数得减1。

为什么呢?因为如果我们用cut[]来记录每个结点对应的连通分量数的话,最后的答案还需要加1。

比如2结点,我们计算所得的cut[2]=3,因为它只计算了它的子树的情况,但是父亲结点并没有计算进去,所以最后需要+1,这个1也就是父亲结点方向也会产生一个连通分量,这是肯定的,因为父亲结点方向就这么一条边。那根结点是没有父亲结点的,所以根结点需要-1。

  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstring>
  4. #include<cstdio>
  5. #include<sstream>
  6. #include<vector>
  7. #include<stack>
  8. #include<queue>
  9. #include<cmath>
  10. #include<map>
  11. #include<set>
  12. using namespace std;
  13. typedef long long ll;
  14. typedef pair<int,ll> pll;
  15. const int INF = 0x3f3f3f3f;
  16. const int maxn=+;
  17.  
  18. int n, m;
  19. int tot;
  20. int sum;
  21. int ans;
  22. int dfs_clock;
  23. int iscut[maxn];
  24. int pre[maxn];
  25. int head[maxn];
  26.  
  27. struct node
  28. {
  29. int v;
  30. int next;
  31. }e[*maxn];
  32.  
  33. void addEdge(int u, int v)
  34. {
  35. e[tot].v=v;
  36. e[tot].next=head[u];
  37. head[u]=tot++;
  38. }
  39.  
  40. int dfs(int u, int fa)
  41. {
  42. int lowu=pre[u]=++dfs_clock;
  43. int child=;
  44. for(int i=head[u];i!=-;i=e[i].next)
  45. {
  46. int v=e[i].v;
  47. if(!pre[v])
  48. {
  49. child++;
  50. int lowv=dfs(v,u);
  51. lowu=min(lowu,lowv);
  52. if(lowv>=pre[u]) iscut[u]++;
  53. }
  54. else if(pre[v]<pre[u] && v!=fa)
  55. {
  56. lowu=min(lowu,pre[v]);
  57. }
  58. }
  59. if(fa< && child>=) iscut[u]--; //这儿很重要,根结点必须-1
  60. return lowu;
  61. }
  62.  
  63. void solve()
  64. {
  65. sum=;
  66. ans=;
  67. memset(pre,,sizeof(pre));
  68. memset(iscut,,sizeof(iscut));
  69. for(int i=;i<n;i++)
  70. {
  71. if(!pre[i])
  72. {
  73. sum++;
  74. dfs_clock=;
  75. dfs(i,-);
  76. }
  77. ans=max(ans,iscut[i]);
  78. }
  79. }
  80.  
  81. int main()
  82. {
  83. //freopen("in.txt","r",stdin);
  84. while(~scanf("%d%d",&n,&m))
  85. {
  86. if(n== && m==) break;
  87. if(m==) {printf("%d\n",n-);continue;}
  88. tot=;
  89. memset(head,-,sizeof(head));
  90. while(m--)
  91. {
  92. int u,v;
  93. scanf("%d%d",&u,&v);
  94. addEdge(u,v);
  95. addEdge(v,u);
  96. }
  97. solve();
  98. printf("%d\n",sum+ans);
  99. }
  100. return ;
  101. }

POJ 2117 Electricity(割点求连通分量)的更多相关文章

  1. poj 2117 Electricity(tarjan求割点删掉之后的连通块数)

    题目链接:http://poj.org/problem?id=2117 题意:求删除一个点后,图中最多有多少个连通块. 题解:就是找一下割点,根节点的割点删掉后增加son-1(son为子树个数),非根 ...

  2. POJ 1523 SPF (割点,连通分量)

    题意:给出一个网络(不一定连通),求所有的割点,以及割点可以切分出多少个连通分量. 思路:很多种情况. (1)如果给的图已经不是连通图,直接“  No SPF nodes”. (2)求所有割点应该不难 ...

  3. POJ 2117 Electricity 双联通分量 割点

    http://poj.org/problem?id=2117 这个妹妹我竟然到现在才见过,我真是太菜了~~~ 求去掉一个点后图中最多有多少个连通块.(原图可以本身就有多个连通块) 首先设点i去掉后它的 ...

  4. poj 2117 Electricity【点双连通求删除点后最多的bcc数】

    Electricity Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4727   Accepted: 1561 Descr ...

  5. POJ—— 2117 Electricity

    Electricity Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5620   Accepted: 1838 Descr ...

  6. poj 2117 Electricity

    /* Tarjan求割点 */ #include<iostream> #include<cstdio> #include<cstring> #include< ...

  7. poj 2117 去掉割点可以分得的联通图的个数模板

    #include<stdio.h> #include<string.h> #define N 11000 /* 去掉一个割点后,询问可以分得的联通图的个数 */ struct ...

  8. Electricity POJ - 2117 + SPF POJ - 1523 去除割点后求强连通分量个数问题

    Electricity POJ - 2117 题目描述 Blackouts and Dark Nights (also known as ACM++) is a company that provid ...

  9. POJ 2117 (割点+连通分量)

    题目链接: http://poj.org/problem?id=2117 题目大意:在一个非连通图中,求一个切除图中任意一个割点方案,使得图中连通分量数最大. 解题思路: 一个大陷阱,m可以等于0,这 ...

随机推荐

  1. 前端写一个月的原生 Android 是如何一种体验?

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/j01G58UC80251/article/details/79017706 一个前端程序猿的一个月原 ...

  2. PAT 1067 Sort with Swap[难]

    1067 Sort with Swap(0,*) (25)(25 分) Given any permutation of the numbers {0, 1, 2,..., N-1}, it is e ...

  3. git的使用(包括创建远程仓库到上传代码到git的详细步骤以及git的一些常用命令)

    A创建远程仓库到上传代码到git 1)登陆或这注册git账号 https://github.com 2)创建远程仓库 3)打开终端输入命令 cd到你的本地项目根目录下,执行如下git命令 git in ...

  4. servlet07

    1.session验证 可以防止非登录的用户,通过在地址栏中输入地址,访问受保护的页面 step1.在用户登录成功之后,将用户的信息保存到session中 step2.在访问受保护的页面时,校验ses ...

  5. 初次使用git上传代码(转)

    转自 http://www.cnblogs.com/cxk1995/p/5800196.html 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我 ...

  6. -webkit-line-clamp超过两行就出现省略号

    -webkit-line-clamp 是一个 不规范的属性(unsupported WebKit property),它没有出现在 CSS 规范草案中. 限制在一个块元素显示的文本的行数. 为了实现该 ...

  7. SV中的数据类型

    Verilog-1995中规定的数据类型有:变量(reg), 线网(wire), 32位有符号数(integer), 64位无符号数(time), 浮点数(real). SV扩展了reg类型为logi ...

  8. Object-C-Foundation-NSNuber

    NSNumber 是一个数值类型封装起来的数值. 装箱:基础类型->对象类型 NSNumber *number=[NSNumber numberWithInt:12]; 拆箱:对象类型-> ...

  9. Intermediate Python for Data Science learning 3 - Customization

    Customization from:https://campus.datacamp.com/courses/intermediate-python-for-data-science/matplotl ...

  10. 《算法C语言实现》————三道题目

    1.对于N = 10,100和1000,记录你的运行环境中分别运行一下程序所花费的时间.(用python) import datetime global a a = 0 def time_1(s): ...