http://poj.org/problem?id=1523
1 #define IO std::ios::sync_with_stdio(0) 2 #include <iostream> 3 #include <vector> 4 #include <stdio.h> 5 #include <string.h> 6 #define pb push_back 7 using namespace std; 8 typedef long long ll; 9 10 const int N=1005; 11 12 vector<int>g[N]; 13 14 int n,cnt; 15 int vis[N],low[N],ans[N]; 16 17 void dfs(int u){ 18 low[u]=vis[u]=++cnt; 19 for(int i=0;i<g[u].size();i++){ 20 int v=g[u][i]; 21 if(!vis[v]){ 22 dfs(v); 23 if(low[v]>=vis[u])ans[u]++; 24 low[u]=min(low[u],low[v]); 25 } 26 else low[u]=min(low[u],vis[v]); 27 } 28 } 29 30 void init(){ 31 cnt=0; 32 vis[1]=1; 33 memset(vis,0,sizeof(vis)); 34 memset(low,0,sizeof(low)); 35 memset(ans,0,sizeof(ans)); 36 } 37 38 39 int main(){ 40 int a,b; 41 int Case=1; 42 while(1) 43 { 44 while(scanf("%d",&a)&&a) 45 { 46 47 scanf("%d",&b); 48 g[a].push_back(b); 49 g[b].push_back(a); 50 } 51 init(); 52 53 dfs(1); 54 if(Case>1) 55 cout<<endl; 56 printf("Network #%d ",Case++); 57 int flag=1; 58 ans[1]--; 59 for(int i=1; i<=1000; i++) 60 if(ans[i]>0) 61 { 62 flag=0; 63 printf(" SPF node %d leaves %d subnets ",i,ans[i]+1); 64 } 65 if(flag) 66 cout<<" No SPF nodes"<<endl; 67 68 69 for(int i=1; i<=1000; i++) 70 g[i].clear(); 71 scanf("%d",&a); 72 if(a==0) 73 break; 74 scanf("%d",&b); 75 g[a].push_back(b); 76 g[b].push_back(a); 77 } 78 return 0; 79 80 }