每一头牛的愿望就是变成一头最受欢迎的牛。现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎。 这
种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认为牛C受欢迎。你的任务是求出有多少头
牛被所有的牛认为是受欢迎的。
标签:cstring main ack 自己 haoi2006 limit problem tar turn
一个数,即有多少头牛被所有的牛认为是受欢迎的。
#include <cstdio> #include <stack> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 10010, maxm = 50010; int n, m,head[maxn],to[maxm],nextt[maxm],tot,scc[maxn],dfsclock,low[maxn],pre[maxn],num,shuliang[maxn],ans; int head2[maxn], to2[maxn], nextt2[maxn], tot2; stack <int> s; void add(int x, int y) { tot++; to[tot] = y; nextt[tot] = head[x]; head[x] = tot; } void add2(int x, int y) { tot2++; to2[tot2] = y; nextt2[tot2] = head2[x]; head2[x] = tot2; } void tarjan(int u) { s.push(u); low[u] = pre[u] = ++dfsclock; for (int i = head[u];i;i = nextt[i]) { int v = to[i]; if (!pre[v]) { tarjan(v); low[u] = min(low[u], low[v]); } else if (!scc[v]) low[u] = min(low[u], pre[v]); } if (low[u] == pre[u]) { num++; while (1) { int t = s.top(); s.pop(); scc[t] = num; shuliang[num]++; if (t == u) break; } } } void lianbian(int u) { for (int i = head[u]; i; i = nextt[i]) { int v = to[i]; if (scc[u] != scc[v]) add2(scc[u], scc[v]); } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { int a, b; scanf("%d%d", &a, &b); add(a, b); } for (int i = 1; i <= n; i++) if (!scc[i]) tarjan(i); for (int i = 1; i <= n; i++) lianbian(i); for (int i = 1; i <= num; i++) if (!head2[i]) { if (ans) { printf("0\n"); return 0; } ans = shuliang[i]; } printf("%d\n", ans); return 0; }
标签:cstring main ack 自己 haoi2006 limit problem tar turn
原文地址:http://www.cnblogs.com/zbtrs/p/6155128.html