Links: http://poj.org/problem?id=2186
All the cows in a unicom component satisfy any one who is considered a red-eyed by other cows. After the strong link contraction point only need to find one and only one unicom component and its out of 0 the answer is the number of the strong Unicom component points.
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include < vector> #include <stack> #include <cmath> #include <map> #define Lson o<<1,l,m#define rson o <<1|1,m+1,r#define Mem (a) memset (A,0,sizeof (a)) typedef long LONG ll;const int N = 10005;const int m = 50005;const ll MoD = 1000000007;using namespace Std;int N, M, Dfs_clock, Scc_cnt;int He[n], pre[n], low[n], Scc[n];stack <int> s;s truct C {int NE, to;} e[m];void add (int id, int x, int y) {e[id].to = y; E[id].ne = He[x]; HE[X] = ID;} void Dfs (int u) {pre[u] = Low[u] = + + Dfs_clock; S.push (U); for (int i = he[u]; i =-1; i = e[i].ne) {int v = e[i].to; if (pre[v] = = 0) {dfs (v); Low[u] = min (Low[u], low[v]); } else if (scc[v] = = 0) {Low[u] = min (Low[u], pre[v]); }} if (low[u] = = Pre[u]) {scc_cnt + +; while (1) {int x = S.top (); S.pop (); SCC[X] = scc_cnt; if (x = = u) break; }}}void FIND_SCC () {mem (SCC); MEM (pre); Dfs_clock = scc_cnt = 0; for (int i = 1; I <= n; i++) {if (pre[i] = = 0) dfs (i); }}int Out[n], V[n];int main () {while (Cin >> N >> m) {memset (He,-1, sizeof (he)); for (int i = 1; I <= m; i++) {int x, y; scanf ("%d%d", &x, &y); Add (i, x, y); } FIND_SCC (); Mem (v); for (int i = 1; I <= n; i++) {v[scc[i]]++; }//printf ("%d\n", scc_cnt); int ans; for (int u = 1, u <= n; u++) {for (int i = he[u]; I! = 1; i = e[i].ne) {int v = e[i].to; if (scc[u]! = Scc[v]) {Out[scc[u]] = 1; }}} int cnt = 0; for (int i = 1; I <= scc_cnt; i++) {if (out[i] = = 0) {cnt++; ans = v[i]; }} if (cnt = = 1) {printf ("%d\n", ans); } else puts ("0"); } return 0;}
POJ 2186 Popular Cows (strong unicom component)