http://poj.org/problem?id=2186
Description
Every cow's dream is to become the very popular cow in the herd. In a herd of n (1 <= n <=) cows, you is given up to M (1 <= m <= 50,000) ordered pairs of the form (A, b) That's cow A thinks that cow B is popular. Since popularity is transitive,If a thinks B is popular and b thinks C was popular, then a would also think that C is
Popular, even if this is a ordered pair in the input. explicitly specified.Your task is to compute the number of cows that was considered popular by every other cow.
Input
* Line 1:two space-separated integers, N and M
* Lines 2..1+m:two space-separated numbers A and B, meaning that A thinks B is popular.
Output
* Line 1: A single integer, that is, the number of cows who was considered popular by every and other cow.
Sample Input
3 31 22) 12 3
Sample Output
1
First contraction point (to see the strong Unicom component as a point), to determine the degree of 0 points there are several, if more than 1 output 0, otherwise the output is zero of the number of points
Code:
#include <cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<queue>#include<stack>#include<algorithm>using namespacestd;#defineN 50005structnode{intV, Next;} A[n];intHead[n], CNT;intDfn[n], low[n], time, BNT, belong[n];intStack[n], instack[n], top;voidInit () {CNT= time = BNT = top =0; memset (Head,-1,sizeof(Head)); memset (DFN,0,sizeof(DFN)); memset (Low,0,sizeof(low)); memset (Stack,0,sizeof(Stack)); memset (Instack,0,sizeof(Instack));}voidADD (intUintv) {A[CNT].V=v; A[cnt].next=Head[u]; Head[u]= cnt++;}voidTarjar (intu) { intv; Low[u]= Dfn[u] = + +Time ; Instack[u]=1; Stack[top++] =u; for(intJ=head[u]; j!=-1; j=A[j].next) {v=a[j].v; if(!Dfn[v]) {Tarjar (v); Low[u]=min (Low[u], low[v]); } Else if(Instack[v]) Low[u]=min (Low[u], dfn[v]); } if(dfn[u]==Low[u]) {BNT++; Do{v= stack[--top]; INSTACK[V]=0; BELONG[V]=BNT; } while(u!=v); }}intMain () {intN, M; while(SCANF ("%d%d", &n, &m)! =EOF) { inti, u, v; Init (); for(i=1; i<=m; i++) {scanf ("%d%d", &u, &v); ADD (U, v); } for(i=1; i<=n; i++) { if(!Dfn[i]) Tarjar (i); } intout[n]={0}; for(intI=1; i<=n; i++) { for(intJ=head[i]; j!=-1; j=A[j].next) {u= Belong[i], V =BELONG[A[J].V]; if(u!=v) out[u]++; } } intflag=0, Index; for(i=1; i<=bnt; i++) { if(!Out[i]) {Flag++; Index=i; } } if(flag>1) printf ("0\n"); Else { intAns =0; for(i=1; i<=n; i++) { if(belong[i]==Index) ans++; } printf ("%d\n", ans); } } return 0;}
(connected Graph indent Branch) Popular cows--POJ--2186