您的位置:首页 > 其它

POJ 1523 SPF tarjan求割点模板

2016-10-22 10:44 369 查看
时空隧道

说起来真是惭愧QAQ…

现在刚会求割点QAQ…

题意:

给出一张无向图,求出割点以及去掉割点之后原图分成了几个联通块

代码如下:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
//by NeighThorn
#define Max(a,b,c) max(a,max(b,c))
using namespace std;
//天上夭桃盛,云中杏蕊多
const int maxn=1000+5,maxm=maxn*maxn*2+5;
int cas,n,hd[maxn],to[maxm],nxt[maxm],cnt,dfn[maxn],low[maxn],stk[maxn],tail,instk[maxn],tim,vis[maxn];

inline void add(int x,int y){
to[cnt]=y;
nxt[cnt]=hd[x];
hd[x]=cnt++;
}

inline void tarjan(int root){
dfn[root]=low[root]=++tim;stk[++tail]=root;instk[root]=1;vis[root]=1;
for(int i=hd[root];i!=-1;i=nxt[i]){
if(!vis[to[i]]){
tarjan(to[i]);low[root]=min(low[root],low[to[i]]);
if(dfn[root]<=low[to[i]])
vis[root]++;
}
else low[root]=min(low[root],dfn[to[i]]);
}
}

signed main(void){
int x,y;cas=0;
while(scanf("%d",&x)&&x){
n=0;cnt=tail=0;
memset(hd,-1,sizeof(hd));
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(vis,0,sizeof(vis));
scanf("%d",&y),add(x,y),add(y,x);n=Max(x,y,n);
while(scanf("%d",&x)&&x)
scanf("%d",&y),add(x,y),add(y,x),n=Max(x,y,n);
printf("Network #%d\n",++cas);tarjan(1);int flag=0;
for(int i=1;i<=n;i++)
if((i==1&&vis[i]>2)||(i>1&&vis[i]>1))
flag=1,printf("  SPF node %d leaves %d subnets\n",i,i==1?vis[i]-1:vis[i]);
if(!flag)
puts("  No SPF nodes");
puts("");
}
return 0;
}


by >_< NeighThorn
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: