POJ 2186 Popular Cows

Source: Internet
Author: User

Popular cowstime limit:2000msmemory limit:65536kbthis problem would be judged onPKU. Original id:2186
64-bit integer IO format: %lld Java class name: Main 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
HintCow 3 is the only Cow of high popularity.
SourceUsaco 2003 Fall Problem Solving: Because the admiration relationship is transitive, the vertex in a strongly connected component: If a cow a in a strongly connected component is admired by the other end of the strong connected component, then every cow in the strong connected component is admired by B. After strong connectivity shrinks, if only one SCC has a degree of 0, then the point in this SCC is admired for all points. If more, of course there is no point to be admired by others. Because the points in an SCC are mutually admired.
1#include <cstdio>2#include <iostream>3#include <stack>4#include <cstring>5 using namespacestd;6 Const intMAXN =10010;7 intDfn[maxn],low[maxn],head[maxn],belong[maxn],scc,tot,idx;8 intOUTDE[MAXN];9 BOOLINSTACK[MAXN];Tenstack<int>Stk; One structarc{ A     intTo,next; -Arcintx =0,inty =-1){ -to =x; theNext =y; -     } -}e[200000]; - voidAddintUintv) { +E[tot] =arc (V,head[u]); -Head[u] = tot++; + } A voidTarjan (intu) { atDfn[u] = Low[u] = idx++; - stk.push (u); -Instack[u] =true; -      for(inti = Head[u]; ~i; i =E[i].next) { -         if(Dfn[e[i].to] = =-1){ - Tarjan (e[i].to); inLow[u] =min (low[u],low[e[i].to]); -}Else if(Instack[e[i].to]) low[u] =min (low[u],dfn[e[i].to]); to     } +     if(Dfn[u] = =Low[u]) { -Scc++; the         intv; *          Do{ $v =stk.top ();Panax Notoginseng Stk.pop (); -INSTACK[V] =false; theBELONG[V] =SCC; +} while(V! =u); A     } the } + intMain () { -     intn,m,u,v; $      while(~SCANF ("%d%d",&n,&m)) { $memset (head,-1,sizeof(head)); -Memset (Belong,0,sizeof(belong)); -memset (dfn,-1,sizeof(DFN)); thememset (low,-1,sizeof(Low)); -memset (OUTDE,0,sizeof(OUTDE));Wuyi          while(!stk.empty ()) Stk.pop (); thetot = SCC = IDX =0; -          for(inti =0; I < m; ++i) { Wuscanf"%d%d",&u,&v); - Add (u,v); About         } $          for(inti =1; I <= N; ++i) -             if(Dfn[i] = =-1) Tarjan (i); -          for(inti =1; I <= N; ++i) -          for(intj = Head[i]; ~j; j =e[j].next) A             if(Belong[i]! = belong[e[j].to]) outde[belong[i]]++; +         intAns =0, p =0; the          for(inti =1; I <= SCC; ++i) -             if(!outde[i]) p = i,ans++; $         if(ans = =1){ theAns =0; the              for(inti =1; I <= N; ++i)if(Belong[i] = = p) ans++; theprintf"%d\n", ans); the}ElsePuts"0"); -     } in     return 0; the}
View Code

POJ 2186 Popular Cows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.