poj1144 关节点
题意也就是找一个网络中的关节点(割点)个数。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; struct node { int i; int next; }; int head[101],pre[101],low[101],ss[101],N,count,cnt; node edge[10001]; int dfs(int i,int root) { int j,w,k=0; if(pre[i]==0) pre[i]=low[i]=++cnt; for(j=head[i];j;j=edge[j].next) { w=edge[j].i; if(pre[w]==0) { dfs(w,i); if(low[i]>low[w]) low[i]=low[w]; if(low[w]>=pre[i]) k++; } else if(w!=root) { if(low[i]>pre[w]) low[i]=pre[w]; } } if(k) count++; return k; } int add(int s,int t) { edge[N].i=t; edge[N].next=head[s]; return N++; } int main () { int k,m,n,i,j; char c; while(scanf("%d",&n) && n) { N=1; memset(head,0,sizeof(head)); memset(low,0,sizeof(low)); while(scanf("%d%c",&m,&c) && m) { do { scanf("%d%c",&k,&c); head[m]=add(m,k); head[k]=add(k,m); } while(c!='\n'); } memset(pre,0,sizeof(pre)); memset(ss,0,sizeof(ss)); count=cnt=0; for(i=1;i<=n;i++) { if(pre[i]==0) { k=dfs(i,0); if(k<2) count--; } } printf("%d\n",count); } return 0; }