poj 1523 求割点
思路:对于所有节点,每次找的子树,key[root]++;输出时,对于根节点就输出key[root],对于其它节点i,输出key[i]+1;
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> #define Maxn 1010 #define Maxm Maxn*10 #define inf 0x7fffffff using namespace std; int dfn[Maxn],low[Maxn],index[Maxn],vi[Maxn],n,e,lab,key[Maxn],flag=0; struct Edge{ int to,next,val,from; }edge[Maxm]; void init() { memset(dfn,0,sizeof(dfn)); memset(key,0,sizeof(key)); for(int i=1;i<=Maxn-1;i++) low[i]=inf; memset(index,-1,sizeof(index)); memset(vi,0,sizeof(vi)); e=lab=flag=0; } void addedge(int from, int to) { edge[e].from=from; edge[e].to=to; edge[e].next=index[from]; index[from]=e++; edge[e].to=from; edge[e].from=to; edge[e].next=index[to]; index[to]=e++; } void dfs(int u) { dfn[u]=lab++; vi[u]=1; int i,temp; for(i=index[u];i!=-1;i=edge[i].next) { temp=edge[i].to; if(!vi[temp]) dfs(temp); } } void find(int u) { int i,j,temp; vi[u]=1; for(i=index[u];i!=-1;i=edge[i].next) { int temp=edge[i].to; if(!vi[temp]) { find(temp); if(low[temp]>=dfn[u]) { key[u]++; flag=1; } low[u]=min(low[temp],low[u]); low[u]=min(low[u],dfn[u]); } low[u]=min(low[u],dfn[temp]); } } int main() { int i,j,m,a,b,Case=1; while(scanf("%d",&a),a) { init(); scanf("%d",&b); addedge(a,b); while(scanf("%d",&a),a) { scanf("%d",&b); addedge(a,b); } dfs(1); memset(vi,0,sizeof(vi)); vi[1]=1; for(i=index[1];i!=-1;i=edge[i].next) { int temp=edge[i].to; if(!vi[temp]) { find(temp); key[1]++; } } printf("Network #%d\n",Case++); if(key[1]>=2) { printf(" SPF node %d leaves %d subnets\n",1,key[1]); flag=1; } if(!flag) printf(" No SPF nodes\n"); else for(i=2;i<=1000;i++) { if(key[i]) printf(" SPF node %d leaves %d subnets\n",i,key[i]+1); } printf("\n"); } return 0; }