Popular cows (POJ 2186)

Source: Internet
Author: User

  • The original title is as follows: Popular cows
    Time Limit: 2000MS Memory Limit: 65536K
    Total Submissions: 40746 Accepted: 16574

    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.
  • The problem: Building the diagram obviously, assuming that both cows A and B are all the other cows are considered to be red, then obviously a and b each other is a red, that is, there is a, b two vertices of the circle, or, A, B is a strong connected component, conversely, if a cow is all the other cattle considered to be the Reds, Then all the cows in the strong connected component of their affiliation are considered reds by all the other cows. It is shown that when the graph is decomposed with strong connected components, at most, a strong connected component satisfies the condition of the problem. In the case of strong connectivity decomposition, we can also get the order of the topological ordering of each strong connected component, the only strong connected component that can become the final topological order of the solution, so in the end, we just need to check whether the last strongly connected component is up to all vertices. The complexity of the algorithm is O (n+m).
  • Code:
    1#include <cstdio>2#include <stack>3#include <vector>4#include <algorithm>5#include <cstring>6 7 using namespacestd;8 9stack<int>s;Ten Const intmax_v=11000; One BOOLInstack[max_v]; A intDfn[max_v]; - intLow[max_v]; - intComponentnumber=0; the intindex; -vector<int>Edge[max_v]; -vector<int>Redge[max_v]; -vector<int>Component[max_v]; + intIncomponent[max_v]; - intN, M; + BOOLVisited[max_v]; A  at voidAdd_edge (intXinty) - { - edge[x].push_back (y); - redge[y].push_back (x); - } -  in voidTarjan (inti) - { todfn[i]=low[i]=index++; +instack[i]=true; - S.push (i); the     intJ; *      for(intE=0; E<edge[i].size (); e++) $     {Panax Notoginsengj=Edge[i][e]; -         if(dfn[j]==-1) the         { + Tarjan (j); Alow[i]=min (low[i], low[j]); the         } +         Else  -             if(Instack[j]) low[i]=min (low[i], dfn[j]); $     } $     if(dfn[i]==Low[i]) -     { -componentnumber++; the          Do -         {Wuyij=s.top (); the S.pop (); -instack[j]=false; Wu Component[componentnumber].push_back (j); -incomponent[j]=Componentnumber; About         } $          while(j!=i); -     } - } -  A voidRdfsintv) + { thevisited[v]=true; -      for(intI=0; I<redge[v].size (); i++) $     { the         if(!Visited[redge[v][i]]) the         { the Rdfs (Redge[v][i]); the         } -     } in } the  the intMain () About { thememset (DFN,-1,sizeof(DFN)); thescanf"%d%d", &n, &M); the      for(intI=0; i<m; i++) +     { -         intx, y; thescanf"%d%d", &x, &y);Bayi Add_edge (x, y); the     } the      for(intI=1; i<n+1; i++) -     { -         if(dfn[i]==-1) Tarjan (i); the     } the     intv=component[1][0]; the     intnum=component[1].size (); the Rdfs (v); -      for(intI=1; i<=n; i++) the     { the         if(!Visited[i]) the         {94num=0; the              Break; the         } the     }98printf"%d\n", num); About}

Popular cows (POJ 2186)

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.