poj 3352 Road Construction 边双连通分量
来源:互联网 发布:程序员接活的网站 编辑:程序博客网 时间:2024/05/16 12:53
题意:
给一个有桥的非边双连通无向图(当然也就是非点双连通图了),求最少加多少条边让它变成边双连通的。
思路;
网上很多解题报告不区分点双连通和边双连通。。。这题求的是边双连通分量。求出各个边双连通分量后缩点得一棵树,把树的叶节点按离最近公共祖先距离和由长到段两两配对相连就可以了,答案为(leaf+1)/2。
代码:
//poj 3352//sepNINE#include <iostream>#include <stack>using namespace std;const int maxN=1024;struct Edge{int v,next;}edge[maxN*2]; int n,m,e,t,cnt;int head[maxN],dfn[maxN],low[maxN],inStack[maxN],par[maxN],belong[maxN];stack<int> mystack;void tarjan(int h){inStack[h]=1;mystack.push(h);dfn[h]=low[h]=++t;for(int i=head[h];i!=-1;i=edge[i].next){int v=edge[i].v;if(!dfn[v]){par[v]=h;tarjan(v);low[h]=min(low[h],low[v]);}else if(inStack[v]&&v!=par[h]){low[h]=min(low[h],dfn[v]);} }if(low[h]==dfn[h]){int k;++cnt;do{k=mystack.top();belong[k]=cnt;mystack.pop();inStack[k]=0;}while(low[k]!=dfn[k]);}return ;}void solve(){memset(dfn,0,sizeof(dfn));memset(low,0,sizeof(low));memset(inStack,0,sizeof(inStack));memset(belong,0,sizeof(belong));while(!mystack.empty()) mystack.pop();int i,j;for(i=1;i<=n;++i)if(!dfn[i]){par[i]=-1;tarjan(i);}int leaf=0,d[maxN];memset(d,0,sizeof(d));for(i=1;i<=n;++i)for(j=head[i];j!=-1;j=edge[j].next){int u=belong[i],v=belong[edge[j].v];if(u!=v){++d[u];++d[v];}}for(i=1;i<=cnt;++i)if(d[i]==2)++leaf;printf("%d\n",(leaf+1)/2);return ;}int main(){while(scanf("%d%d",&n,&m)==2){t=0;e=0;cnt=0;memset(head,-1,sizeof(head));while(m--){int a,b;scanf("%d%d",&a,&b);edge[e].v=b;edge[e].next=head[a];head[a]=e++;edge[e].v=a;edge[e].next=head[b];head[b]=e++;}solve();}return 0;}
0 0
- [双连通分量]poj 3352 Road Construction
- poj 3352--Road Construction(双连通分量)
- POJ---3352-Road Construction(双连通分量)
- POJ 3352 Road Construction / 边双连通分量
- POJ 3352 Road Construction(边双连通分量)
- POJ 3352 Road Construction (边双连通分量)
- poj 3352 Road Construction 边双连通分量
- POJ 3352 Road Construction(边双连通分量)
- POJ - 3352 Road Construction(边双连通分量)
- POJ-3352 Road Construction (边双连通分量[Tarjan])
- poj 3352 Road Construction(边-双连通分量)
- poj 3352 Road Construction(边双连通分量)
- POJ-3352 Road Construction(边双连通分量+缩点)
- POJ 3352 Road Construction (边双连通分量)
- POJ 3352 Road Construction(边双连通分量)
- poj 3352 Road Construction(Tarjan,边双连通分量)
- poj 3352 Road Construction 双连通分量+缩点 模板
- poj3352 Road Construction(边双连通分量)
- 第十二周项目三(5)转化二进制
- 程序使用代码控制UI属性
- int*&、int&*
- 初始ssh_概念+简单使用
- NSUserDefaults的用法
- poj 3352 Road Construction 边双连通分量
- 代理和协议的简单讲解,和自己的一些见解
- 寻找丑数 【微软面试100题 第六十四题】
- 博客导航
- Eclipse下svn的创建分支/合并/切换使用
- c语言输入输出
- iOS 根据文字长度计算Lable高度
- SpringMVC控制器
- HDU's ACM 1028 Ignatius and the Princess III