POJ 2186 强连通分量缩点
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 int en[10010], col[10010], dfn[10010], low[10010], stack[10010], tot[10010], chu[10010]; 7 bool ins[10010]; 8 int n, m, esize, dtime, stop, scc; 9 struct edge{ 10 int v, n; 11 } e[50010]; 12 void insert(int u, int v) 13 { 14 e[esize].v = v; 15 e[esize].n = en[u]; 16 en[u] = esize ++; 17 } 18 void dfs(int x) 19 { 20 dfn[x] = low[x] = dtime ++; 21 stack[stop++] = x; 22 ins[x] = true; 23 for (int t = en[x]; t != -1; t = e[t].n){ 24 int v = e[t].v; 25 if (!dfn[v]){ 26 dfs(v); 27 low[x] = min(low[x], low[v]); 28 } 29 else if (ins[v]){ 30 low[x] = min(low[x], dfn[v]); 31 } 32 } 33 if (dfn[x] == low[x]){ 34 scc ++; 35 while(stack[--stop] != x){ 36 col[stack[stop]] = scc; 37 ins[stack[stop]] = false; 38 } 39 col[x] = scc; 40 ins[x] = false; 41 } 42 } 43 int main() 44 { 45 scanf("%d %d", &n, &m); 46 int a, b; 47 esize = 0; 48 memset(en, -1, sizeof(en)); 49 for (int i = 0; i < m; i++){ 50 scanf("%d %d", &a, &b); 51 insert(a, b); 52 } 53 memset(dfn, 0, sizeof(dfn)); 54 memset(col, 0, sizeof(col)); 55 memset(ins, 0, sizeof(ins)); 56 dtime = 1; stop = 0; scc = 0; 57 for (int i = 1; i <= n; i++) 58 if (!dfn[i]) dfs(i); 59 memset(chu, 0, sizeof(chu)); 60 memset(tot, 0, sizeof(tot)); 61 for (int i = 1; i <= n; i++){ 62 int u = col[i]; 63 tot[u]++; 64 for (int t = en[i]; t != -1; t = e[t].n){ 65 int v = col[e[t].v]; 66 if (u != v) 67 chu[u] ++; 68 } 69 } 70 int cnt = 0, ans; 71 for (int i = 1; i <= scc; i++) 72 if (chu[i] == 0){ 73 cnt ++; 74 ans = tot[i]; 75 } 76 if (cnt > 1) ans = 0; 77 printf("%d\n", ans); 78 return 0; 79 }
POJ 1144 TOJ 1026 求割点数量
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<cstdlib> 5 #include<algorithm> 6 using namespace std; 7 8 int esize, n, root, dtime; 9 struct edge{ 10 int v, n; 11 } e[100000]; 12 int en[200], dfn[200], low[200]; 13 bool cut[200]; 14 int min(int a, int b) 15 { 16 if (a < b) return a; else return b; 17 } 18 void insert(int u, int v) 19 { 20 e[esize].v = v; 21 e[esize].n = en[u]; 22 en[u] = esize++; 23 } 24 void dfs(int x, int fa) 25 { 26 int son = 0; 27 dfn[x] = low[x] = dtime++; 28 for (int t = en[x]; t != -1; t = e[t].n){ 29 int v = e[t].v; 30 if (v == fa) continue; 31 if (!dfn[v]){ 32 son++; 33 dfs(v, x); 34 low[x] = min(low[x], low[v]); 35 if ((x != root && low[v] >= dfn[x]) || (x == root && son > 1)) 36 cut[x] = true; 37 } 38 else{ 39 low[x] = min(low[x], dfn[v]); 40 } 41 } 42 } 43 int main() 44 { 45 while(scanf("%d", &n) == 1 && n) 46 { 47 memset(en, -1, sizeof(en)); 48 int x, y, esize = 0; 49 char ch; 50 while(scanf("%d", &x) == 1 && x){ 51 while((ch = getchar()) != '\n'){ 52 scanf("%d", &y); 53 insert(x, y); 54 insert(y, x); 55 } 56 } 57 memset(dfn, 0, sizeof(dfn)); 58 memset(cut, 0, sizeof(cut)); 59 root = 1; dtime = 1; 60 dfs(1, 0); 61 int ans = 0; 62 for (int i = 1; i <= n; i++) 63 if (cut[i]) { 64 ans++; 65 } 66 printf("%d\n", ans); 67 } 68 return 0; 69 }
HDOJ 4738 无向图求桥
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 6 const int inf = 2147483647; 7 int esize, n, m, dtime, ans; 8 int low[1010], dfn[1010], en[1010], f[1010]; 9 struct edge{ 10 int v, n, w; 11 bool u; 12 } e[2002000]; 13 int getf(int x) 14 { 15 if (x == f[x]) return x; 16 f[x] = getf(f[x]); 17 return f[x]; 18 } 19 void unionxy(int x, int y) 20 { 21 int xroot = getf(x), 22 yroot = getf(y); 23 f[xroot] = yroot; 24 } 25 void insert(int u, int v, int w){ 26 e[esize].v = v; 27 e[esize].n = en[u]; 28 e[esize].w = w; 29 e[esize].u = false; 30 en[u] = esize ++; 31 } 32 void dfs(int u) 33 { 34 low[u] = dfn[u] = dtime ++; 35 for (int t = en[u]; t != -1; t = e[t].n){ 36 if (e[t^1].u) continue; 37 e[t].u = true; 38 int v = e[t].v; 39 if (!dfn[v]){ 40 dfs(v); 41 low[u] = min(low[u], low[v]); 42 if (low[v] > dfn[u]){ 43 if (e[t].w == 0) 44 ans = min(ans, 1); 45 else 46 ans = min(ans, e[t].w); 47 } 48 } 49 else 50 low[u] = min(low[u], dfn[v]); 51 } 52 } 53 int main() 54 { 55 while(scanf("%d %d", &n, &m) && (n + m)) 56 { 57 memset(en, -1, sizeof(en)); 58 esize = 0; 59 for (int i = 1; i <= n; i++) 60 f[i] = i; 61 int x, y, w; 62 for (int i = 0; i < m; i++){ 63 scanf("%d %d %d", &x, &y, &w); 64 insert(x, y, w); 65 insert(y, x, w); 66 unionxy(x, y); 67 } 68 int cnt = 0; 69 for (int i = 1; i <= n; i++) 70 if (f[i] == i) cnt ++; 71 if (cnt > 1){ 72 puts("0"); 73 continue; 74 } 75 memset(dfn, 0, sizeof(dfn)); 76 dtime = 1, ans = inf; 77 for (int i = 1; i <= n; i++) 78 if (!dfn[i]) dfs(i); 79 if (ans == inf) ans = -1; 80 printf("%d\n", ans); 81 } 82 return 0; 83 }