题目链接:POJ - 1144
求割点的模板题
1 #include<stdio.h> 2 #include<iostream> 3 #include<string.h> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 8 const int N=10010; 9 int dfn[N],low[N],f[N],n,cnt; 10 bool book[N];//是否是割点 11 vector<int>v[N]; 12 13 void init() 14 { 15 for(int i=0; i<N; i++) 16 v[i].clear(); 17 cnt=0; 18 memset(low,0,sizeof(low)); 19 memset(dfn,0,sizeof(dfn)); 20 memset(f,0,sizeof(f)); 21 memset(book,0,sizeof(book)); 22 } 23 24 void Tarjan(int u,int fa) 25 { 26 dfn[u]=low[u]=++cnt; 27 f[u]=fa; 28 for(int i=0; i<v[u].size(); i++) 29 { 30 int x=v[u][i]; 31 if(dfn[x]==0) 32 { 33 Tarjan(x,u); 34 low[u]=min(low[x],low[u]); 35 } 36 else if(fa!=x) 37 low[u]=min(dfn[x],low[u]); 38 } 39 } 40 41 int main() 42 { 43 while(~scanf("%d",&n)&&n) 44 { 45 init(); 46 int x,y; 47 while(~scanf("%d",&x)&&x) 48 { 49 while(getchar()!='\n') 50 { 51 scanf("%d",&y); 52 v[x].push_back(y); 53 v[y].push_back(x); 54 } 55 } 56 Tarjan(1,0); 57 int son=0;//统计根节点儿子的个数 58 for(int i=2; i<=n; i++) 59 { 60 x=f[i]; 61 if(x==1)//i的父亲是根节点 62 son++; 63 else if(dfn[x]<=low[i]) 64 book[x]=1; 65 } 66 int ans=0; 67 if(son>1) 68 ans++; 69 for(int i=2; i<=n; i++) 70 { 71 if(book[i]) 72 ans++; 73 } 74 printf("%d\n",ans); 75 } 76 return 0; 77 }