标签:const play cst top ios ack close tac getch
有向图,找点数大于1的强连通分量个数
——代码
1 #include <stack> 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 6 const int MAXN = 50001; 7 int n, m, cnt, idx, size, ans; 8 int head[MAXN], to[MAXN << 1], next[MAXN << 1]; 9 int dfn[MAXN], low[MAXN], belong[MAXN], tot[MAXN]; 10 bool ins[MAXN]; 11 std::stack <int> s; 12 13 inline int read() 14 { 15 int x = 0, f = 1; 16 char ch = getchar(); 17 for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1; 18 for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘; 19 return x * f; 20 } 21 22 inline int min(int x, int y) 23 { 24 return x < y ? x : y; 25 } 26 27 inline void add(int x, int y) 28 { 29 to[cnt] = y; 30 next[cnt] = head[x]; 31 head[x] = cnt++; 32 } 33 34 inline void dfs(int u) 35 { 36 int i, v; 37 s.push(u); 38 ins[u] = 1; 39 dfn[u] = low[u] = ++idx; 40 for(i = head[u]; i ^ -1; i = next[i]) 41 { 42 v = to[i]; 43 if(!dfn[v]) 44 { 45 dfs(v); 46 low[u] = min(low[u], low[v]); 47 } 48 else if(ins[v]) low[u] = min(low[u], dfn[v]); 49 } 50 if(low[u] == dfn[u]) 51 { 52 size++; 53 do 54 { 55 v = s.top(); 56 s.pop(); 57 ins[v] = 0; 58 belong[v] = size; 59 } 60 while(u ^ v); 61 } 62 } 63 64 int main() 65 { 66 int i, x, y; 67 n = read(); 68 m = read(); 69 memset(head, -1, sizeof(head)); 70 for(i = 1; i <= m; i++) 71 { 72 x = read(); 73 y = read(); 74 add(x, y); 75 } 76 for(i = 1; i <= n; i++) 77 if(!dfn[i]) 78 dfs(i); 79 for(i = 1; i <= n; i++) tot[belong[i]]++; 80 for(i = 1; i <= size; i++) 81 if(tot[i] > 1) 82 ans++; 83 printf("%d\n", ans); 84 return 0; 85 }
[luoguP2863] [USACO06JAN]牛的舞会The Cow Prom(Tarjan)
标签:const play cst top ios ack close tac getch
原文地址:http://www.cnblogs.com/zhenghaotian/p/6900605.html