当前位置: 代码迷 >> 综合 >> poj 3352 Road Construction 边双连通分量
  详细解决方案

poj 3352 Road Construction 边双连通分量

热度:65   发布时间:2024-01-19 06:26:15.0

题意:

给一个有桥的非边双连通无向图(当然也就是非点双连通图了),求最少加多少条边让它变成边双连通的。

思路;

网上很多解题报告不区分点双连通和边双连通。。。这题求的是边双连通分量。求出各个边双连通分量后缩点得一棵树,把树的叶节点按离最近公共祖先距离和由长到段两两配对相连就可以了,答案为(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;	
} 


  相关解决方案