[luoguP2863] [USACO06JAN]牛的舞会The Cow Prom(Tarjan)
Posted 蒟蒻zht的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[luoguP2863] [USACO06JAN]牛的舞会The Cow Prom(Tarjan)相关的知识,希望对你有一定的参考价值。
有向图,找点数大于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)的主要内容,如果未能解决你的问题,请参考以下文章
P2863 [USACO06JAN]牛的舞会The Cow Prom
洛谷——P2863 [USACO06JAN]牛的舞会The Cow Prom
洛谷P2863 [USACO06JAN]牛的舞会The Cow Prom
P2863 [USACO06JAN]牛的舞会The Cow Prom