Popular cows
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 31241 |
|
Accepted: 12691 |
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 was popular and b thinks C is 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
Hint
Cow 3 is the only Cow of high popularity.
Source
Usaco 2003 Fall Test instructions
Every cow dreamed of becoming a star in the barn. Cows that are liked by all cows are a star cow. All milk
Cows are narcissistic, and each cow always likes its own. The "like" between cows can be passed on-if a-hi
Huan b,b like C, so a also likes C. There are n cows in the barn, given the affection between some cows, please
Figure out how many cows can be stars.
Third Way.
Only one answer to an SCC with a degree of 0 is its size
Otherwise no solution
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;Const intn=1e4+5, m=5e4+5; typedefLong LongLl;inlineintRead () {CharC=getchar ();intx=0, f=1; while(c<'0'|| C>'9'){if(c=='-') f=-1; c=GetChar ();} while(c>='0'&&c<='9') {x=x*Ten+c-'0'; c=GetChar ();} returnx*F;}intn,m,u,v;structedge{intV,ne;} E[M];intH[n],cnt=0; inlinevoidInsintUintv) {CNT++; E[CNT].V=v;e[cnt].ne=h[u];h[u]=CNT;}intDfn[n],belong[n],low[n],dfc,scc,st[n],top;intSize[n];voidDfsintu) {Dfn[u]=low[u]=++DFC; st[++top]=u; for(intI=h[u];i;i=e[i].ne) { intv=e[i].v; if(!Dfn[v]) {DFS (v); Low[u]=min (low[u],low[v]); }Else if(!Belong[v]) Low[u]=min (low[u],dfn[v]); } if(low[u]==Dfn[u]) {SCC++; while(true){ intx=st[top--]; BELONG[X]=SCC; SIZE[SCC]++; if(X==u) Break; } }}intOutd[n],ind[n],ans;voidPoint () { for(intu=1; u<=n;u++) for(intI=h[u];i;i=e[i].ne) { intv=e[i].v; if(Belong[u]!=belong[v]) outd[belong[u]]++,ind[belong[v]]++; }}intMain () {n=read (); m=read (); for(intI=1; i<=m;i++) {u=read (); v=read (); Ins (u,v);} for(intI=1; i<=n;i++)if(!Dfn[i]) DFS (i); Point (); for(intI=1; i<=scc;i++){ if(outd[i]==0){ if(ANS) {ans=0; Break;} Elseans=Size[i]; }} printf ("%d", ans);}
POJ2186 Popular cows [Tarjan pinch point]