链接:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=737
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82833#problem/C
说实话还不是太懂,自己再写点题理解理解!
代码:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<iostream> 5 #include<algorithm> 6 #include<cstring> 7 #include<vector> 8 using namespace std; 9 #define N 1005 10 11 struct Edge{int v, next;}e[N*N]; 12 struct Bridge{int u, v;}bri[N]; 13 14 int bnt, n; 15 int Head[N], cnt; 16 int dfn[N], low[N], f[N], Index; 17 18 bool cmp(Bridge a, Bridge b) 19 { 20 if(a.u != b.u) 21 return a.u < b.u; 22 return a.v < b.v; 23 } 24 25 void Init() 26 { 27 cnt = bnt = Index = 0; 28 memset(low, 0, sizeof(low)); 29 memset(dfn, 0, sizeof(dfn)); 30 memset(f, 0, sizeof(f)); 31 32 for(int i=0; i<=n; i++) 33 { 34 Head[i] = -1; 35 dfn[i] = 0; 36 } 37 } 38 39 void AddEdge(int u, int v) 40 { 41 e[cnt].v = v; 42 e[cnt].next = Head[u]; 43 Head[u] = cnt++; 44 } 45 46 void TarJan(int u, int fa) 47 { 48 f[u] = fa; 49 low[u] = dfn[u] = ++Index; 50 51 for(int j=Head[u]; j!=-1; j=e[j].next) 52 { 53 int v = e[j].v; 54 55 if(!dfn[v]) 56 { 57 TarJan(v, u); 58 low[u] = min(low[u], low[v]); 59 } 60 else if(v!=fa) 61 low[u] = min(low[u], dfn[v]); 62 } 63 } 64 65 int main() 66 { 67 while(scanf("%d", &n)!=EOF) 68 { 69 int i, j, u, v, m; 70 71 Init(); 72 73 for(i=0; i<n; i++) 74 { 75 scanf("%d (%d)", &u, &m); 76 77 for(j=0; j<m; j++) 78 { 79 scanf("%d", &v); 80 AddEdge(u, v); 81 } 82 } 83 84 for(i=0; i<n; i++) 85 { 86 if(!dfn[i]) 87 TarJan(i, i); 88 } 89 90 for(i=0; i<n; i++) 91 { 92 int u = f[i]; 93 if(low[i] > dfn[u]) 94 { 95 bri[bnt].u = min(u, i); 96 bri[bnt++].v = max(u, i); 97 } 98 } 99 100 sort(bri, bri+bnt, cmp); 101 102 printf("%d critical links\n", bnt); 103 104 for(i=0; i<bnt; i++) 105 printf("%d - %d\n", bri[i].u, bri[i].v); 106 printf("\n"); 107 } 108 return 0; 109 }
勿忘初心