标签:
Description
Input
Output
Sample Input
1 2 5 4 3 1 3 2 3 4 3 5 0 1 2 2 3 3 4 4 5 5 1 0 1 2 2 3 3 4 4 6 6 3 2 5 5 1 0 0
Sample Output
Network #1 SPF node 3 leaves 2 subnets Network #2 No SPF nodes Network #3 SPF node 2 leaves 2 subnets SPF node 3 leaves 2 subnets
1 #include<cstdio> 2 #include<vector> 3 #include<cstring> 4 using namespace std; 5 6 vector<int>G[1005]; 7 bool flag; 8 int low[1005],dfs_clock,pre[1005]; 9 bool iscut[1005]; 10 bool vis[1005]; 11 12 void init() 13 { 14 memset(iscut,0,sizeof(iscut)); 15 memset(pre,0,sizeof(pre)); 16 memset(vis,0,sizeof(vis)); 17 for(int i=0;i<1005;i++)G[i].clear(); 18 dfs_clock=0; 19 flag=0; 20 } 21 22 int findcut(int u,int fa) 23 { 24 int lowu=pre[u]=++dfs_clock; 25 int child=0; 26 for(int i=0;i<G[u].size();i++) 27 { 28 int v=G[u][i]; 29 if(!pre[v]) 30 { 31 child++; 32 int lowv=findcut(v,u); 33 lowu=min(lowu,lowv); 34 if(lowv>=pre[u]) 35 iscut[u]=true; 36 } 37 else if(pre[v]<pre[u]&&v!=fa) 38 lowu=min(lowu,pre[v]); 39 } 40 if(fa<0&&child==1)iscut[u]=0; 41 low[u]=lowu; 42 return lowu; 43 } 44 45 void dfs(int u) 46 { 47 vis[u]=1; 48 for(int i=0;i<G[u].size();i++) 49 if(!vis[G[u][i]]) 50 { 51 vis[G[u][i]]=1; 52 dfs(G[u][i]); 53 } 54 } 55 56 void output() 57 { 58 for(int i=1;i<1005;i++) 59 { 60 if(iscut[i]) 61 { 62 flag=1; 63 memset(vis,0,sizeof(vis)); 64 vis[i]=1; 65 int son=0; 66 for(int j=0;j<G[i].size();j++) 67 { 68 if(!vis[G[i][j]]) 69 { 70 dfs(G[i][j]); 71 son++; 72 } 73 } 74 printf(" SPF node %d leaves %d subnets\n",i,son); 75 } 76 } 77 if(!flag) 78 printf(" No SPF nodes\n"); 79 printf("\n"); 80 } 81 82 int main() 83 { 84 //freopen("in.txt","r",stdin); 85 int u,v,t; 86 bool key=0; 87 t=0; 88 while(scanf("%d",&u)) 89 { 90 if(u==0&&key) 91 break; 92 else if(u==0&&!key) 93 { 94 printf("Network #%d\n",++t); 95 findcut(1,-1); 96 output(); 97 init(); 98 key=1; 99 continue; 100 } 101 if(u!=0)key=0;else key=1; 102 scanf("%d",&v); 103 G[u].push_back(v); 104 G[v].push_back(u); 105 } 106 return 0; 107 }
标签:
原文地址:http://www.cnblogs.com/homura/p/4748996.html