POJ 2117 Electricity(割点求连通分量)
http://poj.org/problem?id=2117
题意:
求删除图中任意一个顶点后的最大连通分量数。
思路:
求出每个割点对应的连通分量数,注意这道题目中图可能是不连通的。
这道题目我wa了很多发,主要是我忘了根结点的连通分量数得减1。
为什么呢?因为如果我们用cut[]来记录每个结点对应的连通分量数的话,最后的答案还需要加1。
比如2结点,我们计算所得的cut[2]=3,因为它只计算了它的子树的情况,但是父亲结点并没有计算进去,所以最后需要+1,这个1也就是父亲结点方向也会产生一个连通分量,这是肯定的,因为父亲结点方向就这么一条边。那根结点是没有父亲结点的,所以根结点需要-1。
- #include<iostream>
- #include<algorithm>
- #include<cstring>
- #include<cstdio>
- #include<sstream>
- #include<vector>
- #include<stack>
- #include<queue>
- #include<cmath>
- #include<map>
- #include<set>
- using namespace std;
- typedef long long ll;
- typedef pair<int,ll> pll;
- const int INF = 0x3f3f3f3f;
- const int maxn=+;
- int n, m;
- int tot;
- int sum;
- int ans;
- int dfs_clock;
- int iscut[maxn];
- int pre[maxn];
- int head[maxn];
- struct node
- {
- int v;
- int next;
- }e[*maxn];
- void addEdge(int u, int v)
- {
- e[tot].v=v;
- e[tot].next=head[u];
- head[u]=tot++;
- }
- int dfs(int u, int fa)
- {
- int lowu=pre[u]=++dfs_clock;
- int child=;
- for(int i=head[u];i!=-;i=e[i].next)
- {
- int v=e[i].v;
- if(!pre[v])
- {
- child++;
- int lowv=dfs(v,u);
- lowu=min(lowu,lowv);
- if(lowv>=pre[u]) iscut[u]++;
- }
- else if(pre[v]<pre[u] && v!=fa)
- {
- lowu=min(lowu,pre[v]);
- }
- }
- if(fa< && child>=) iscut[u]--; //这儿很重要,根结点必须-1
- return lowu;
- }
- void solve()
- {
- sum=;
- ans=;
- memset(pre,,sizeof(pre));
- memset(iscut,,sizeof(iscut));
- for(int i=;i<n;i++)
- {
- if(!pre[i])
- {
- sum++;
- dfs_clock=;
- dfs(i,-);
- }
- ans=max(ans,iscut[i]);
- }
- }
- int main()
- {
- //freopen("in.txt","r",stdin);
- while(~scanf("%d%d",&n,&m))
- {
- if(n== && m==) break;
- if(m==) {printf("%d\n",n-);continue;}
- tot=;
- memset(head,-,sizeof(head));
- while(m--)
- {
- int u,v;
- scanf("%d%d",&u,&v);
- addEdge(u,v);
- addEdge(v,u);
- }
- solve();
- printf("%d\n",sum+ans);
- }
- return ;
- }
POJ 2117 Electricity(割点求连通分量)的更多相关文章
- poj 2117 Electricity(tarjan求割点删掉之后的连通块数)
题目链接:http://poj.org/problem?id=2117 题意:求删除一个点后,图中最多有多少个连通块. 题解:就是找一下割点,根节点的割点删掉后增加son-1(son为子树个数),非根 ...
- POJ 1523 SPF (割点,连通分量)
题意:给出一个网络(不一定连通),求所有的割点,以及割点可以切分出多少个连通分量. 思路:很多种情况. (1)如果给的图已经不是连通图,直接“ No SPF nodes”. (2)求所有割点应该不难 ...
- POJ 2117 Electricity 双联通分量 割点
http://poj.org/problem?id=2117 这个妹妹我竟然到现在才见过,我真是太菜了~~~ 求去掉一个点后图中最多有多少个连通块.(原图可以本身就有多个连通块) 首先设点i去掉后它的 ...
- poj 2117 Electricity【点双连通求删除点后最多的bcc数】
Electricity Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4727 Accepted: 1561 Descr ...
- POJ—— 2117 Electricity
Electricity Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5620 Accepted: 1838 Descr ...
- poj 2117 Electricity
/* Tarjan求割点 */ #include<iostream> #include<cstdio> #include<cstring> #include< ...
- poj 2117 去掉割点可以分得的联通图的个数模板
#include<stdio.h> #include<string.h> #define N 11000 /* 去掉一个割点后,询问可以分得的联通图的个数 */ struct ...
- Electricity POJ - 2117 + SPF POJ - 1523 去除割点后求强连通分量个数问题
Electricity POJ - 2117 题目描述 Blackouts and Dark Nights (also known as ACM++) is a company that provid ...
- POJ 2117 (割点+连通分量)
题目链接: http://poj.org/problem?id=2117 题目大意:在一个非连通图中,求一个切除图中任意一个割点方案,使得图中连通分量数最大. 解题思路: 一个大陷阱,m可以等于0,这 ...
随机推荐
- 前端写一个月的原生 Android 是如何一种体验?
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/j01G58UC80251/article/details/79017706 一个前端程序猿的一个月原 ...
- 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 ...
- git的使用(包括创建远程仓库到上传代码到git的详细步骤以及git的一些常用命令)
A创建远程仓库到上传代码到git 1)登陆或这注册git账号 https://github.com 2)创建远程仓库 3)打开终端输入命令 cd到你的本地项目根目录下,执行如下git命令 git in ...
- servlet07
1.session验证 可以防止非登录的用户,通过在地址栏中输入地址,访问受保护的页面 step1.在用户登录成功之后,将用户的信息保存到session中 step2.在访问受保护的页面时,校验ses ...
- 初次使用git上传代码(转)
转自 http://www.cnblogs.com/cxk1995/p/5800196.html 首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我 ...
- -webkit-line-clamp超过两行就出现省略号
-webkit-line-clamp 是一个 不规范的属性(unsupported WebKit property),它没有出现在 CSS 规范草案中. 限制在一个块元素显示的文本的行数. 为了实现该 ...
- SV中的数据类型
Verilog-1995中规定的数据类型有:变量(reg), 线网(wire), 32位有符号数(integer), 64位无符号数(time), 浮点数(real). SV扩展了reg类型为logi ...
- Object-C-Foundation-NSNuber
NSNumber 是一个数值类型封装起来的数值. 装箱:基础类型->对象类型 NSNumber *number=[NSNumber numberWithInt:12]; 拆箱:对象类型-> ...
- Intermediate Python for Data Science learning 3 - Customization
Customization from:https://campus.datacamp.com/courses/intermediate-python-for-data-science/matplotl ...
- 《算法C语言实现》————三道题目
1.对于N = 10,100和1000,记录你的运行环境中分别运行一下程序所花费的时间.(用python) import datetime global a a = 0 def time_1(s): ...