标签:des style blog http color os java io for
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
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 <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 1010; 18 vector<int>g[maxn]; 19 bool iscut[maxn]; 20 int dfn[maxn],low[maxn],cnt,vis[maxn]; 21 void tarjan(int u,int fa) { 22 dfn[u] = low[u] = ++cnt; 23 vis[u] = 1; 24 int son = 0; 25 for(int i = 0; i < g[u].size(); i++) { 26 if(!vis[g[u][i]]) { 27 tarjan(g[u][i],u); 28 son++; 29 low[u] = min(low[u],low[g[u][i]]); 30 if(fa == -1 && son > 1 || fa != -1 && low[g[u][i]] >= dfn[u]) 31 iscut[u] = true; 32 } else if(vis[g[u][i]] == 1) low[u] = min(low[u],dfn[g[u][i]]); 33 } 34 vis[u] = 2; 35 } 36 void dfs(int u) { 37 vis[u] = 1; 38 for(int i = 0; i < g[u].size(); i++) 39 if(!vis[g[u][i]]) dfs(g[u][i]); 40 } 41 void init() { 42 memset(vis,0,sizeof(vis)); 43 memset(iscut,false,sizeof(iscut)); 44 for(int i = 0; i < maxn; i++) g[i].clear(); 45 } 46 int main() { 47 int i,j,u,v,ks = 1,n,son; 48 bool flag; 49 while(scanf("%d",&u),u) { 50 n = 0; 51 init(); 52 scanf("%d",&v); 53 n = max(max(u,v),n); 54 g[u].push_back(v); 55 g[v].push_back(u); 56 while(scanf("%d",&u),u) { 57 scanf("%d",&v); 58 n = max(max(u,v),n); 59 g[u].push_back(v); 60 g[v].push_back(u); 61 } 62 flag = true; 63 tarjan(1,-1); 64 printf("Network #%d\n",ks++); 65 for(i = 1; i <= n; i++) { 66 if(iscut[i]) { 67 flag = false; 68 son = 0; 69 memset(vis,0,sizeof(vis)); 70 vis[i] = 1;//记得把路锁死 71 for(j = 0; j < g[i].size(); j++) { 72 if(!vis[g[i][j]]) { 73 son++; 74 dfs(g[i][j]); 75 } 76 } 77 printf(" SPF node %d leaves %d subnets\n",i,son); 78 } 79 } 80 if(flag) puts(" No SPF nodes"); 81 puts(""); 82 } 83 return 0; 84 }
标签:des style blog http color os java io for
原文地址:http://www.cnblogs.com/crackpotisback/p/3939372.html