Popular cows
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 27531 |
|
Accepted: 11077 |
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 The test instructions when every cow can admire to beg for its cattle, and admire the relationship can pass each other to ask the number of the most popular cattle can use the strong link to the contraction point method, the last to find the point after the perceive 0 points of the number, pay attention to a problem is: if there is no degree of 0 points, So the result of the output answer is 0 below provides a few sets of samples, the main attention to the output of 0 points need to be particularly determined to solve the problem of the template with the above the resolution of the school to pass the number of file relationship between the template is a set of templates, and judge the degree out there is also a change PS:: Attach a whine?
#include <string.h>#include<stdio.h>#defineV 10500#defineE 50500structedge{intto, next;} Edge[e];intHead[v], E, n;intINDEG[V], outdeg[v];//the degree of entry and the degrees of the pointintBELONG[V], Low[v], Dfn[v], SCC, CNT;//dfn[]: The time to traverse to the U point, the smallest of the points reachable by the Low[]:u point Dfn[v]intS[v], top;BOOLVIS[V];//v is in the stackintAddedge (intUintv) {edge[e].to=v; Edge[e].next=Head[u]; Head[u]= e++; return 0;}voidTarjan (intu) { intv; Dfn[u]= Low[u] = ++cnt;//start Dfn[u] = = Low[u]s[top++] = u;//no matter 3,721 in the stackVis[u] =true; for(intI=head[u]; i!=-1; I=Edge[i].next) {v=edge[i].to; if(Dfn[v] = =0)//if the V-point has not been traversed{Tarjan (v);//Traverse downLow[u] = Low[u] < Low[v]? Low[u]: low[v];//ensure low[u] minimum } Else if(Vis[v] && low[u] > Dfn[v])//v in the stack, modify Low[u]Low[u] =Dfn[v]; } if(Dfn[u] = = Low[u])//u traverse the root of the tree in the strongly connected component { ++SCC; Do{v= S[--top];//all points to u in the stack belong to the strong connected component, and the stack is retiredVIS[V] =false; BELONG[V]=SCC; } while(U! =v); }}intsolve () {SCC= top = CNT =0; memset (DFN,0,sizeof(DFN)); memset (Vis,false,sizeof(VIS)); for(intu=1; u<=n; ++u)if(Dfn[u] = =0) Tarjan (U); returnSCC;}voidcount_deg () {memset (indeg,0,sizeof(indeg)); memset (Outdeg,0,sizeof(outdeg)); for(intu=1; u<=n; ++u) for(intI=head[u]; i!=-1; I=Edge[i].next) { intv =edge[i].to; if(Belong[u]! =Belong[v]) {Indeg[belong[v]]++; Outdeg[belong[u]]++; } }}intMain () {intu, V, I; intm; while(~SCANF ("%d%d", &n,&m)) {e=0; memset (Head,-1,sizeof(head)); for(intI=1; i<=m;i++) {scanf ("%d%d",&u,&v); Addedge (U, v); } solve (); Count_deg (); intnum=0, TMP; for(inti =1; I <= SCC; i++) { if(!Outdeg[i]) {num++; TMP=i; } } intans=0; if(num = =1) { for(inti =1; I <= N; i++) { if(Belong[i] = =tmp) ans++; } printf ("%d\n", ans); } Else{printf ("0\n"); } } return 0;}
poj2186 Most popular cattle (for the number of the most popular cows)