poj 1144 Network (割点)
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int N=110; int num[N],low[N],head[N],vis[N]; bool cut[N]; int k,n,cnt,root; struct Edge{ int to,next; }edge[N<<1]; void addedge(int u,int v){ edge[cnt].to=v; edge[cnt].next=head[u]; head[u]=cnt++; } void Tarjan(int u,int fa){ int son=0; vis[u]=1; num[u]=low[u]=++k; for(int i=head[u];i!=-1;i=edge[i].next){ int v=edge[i].to; if(vis[v]==1 && v!=fa) low[u]=min(low[u],num[v]); if(vis[v]==0){ Tarjan(v,u); son++; low[u]=min(low[u],low[v]); if((u==root && son>1) || (u!=root && num[u]<=low[v])) cut[u]=1; } } vis[u]=2; } int main() { while(~scanf("%d",&n)&&n){ memset(head,-1,sizeof(head)); memset(num,0,sizeof(num)); memset(low,0,sizeof(low)); memset(vis,0,sizeof(vis)); memset(cut,0,sizeof(cut)); cnt=0; int u,v; while(scanf("%d",&u) && u){ while(getchar()!='\n'){ scanf("%d",&v); addedge(u,v); addedge(v,u); } } root=1; Tarjan(root,-1); int ans=0; for(int i=1;i<=n;i++) if(cut[i]) ans++; printf("%d\n",ans); } return 0; }