POJ 2186 Popular Cows (Tarjan)

Source: Internet
Author: User
Tags min

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 3
1 2
2 1
2 3

Sample Output

1

Hint
Cow 3 is the only Cow of high popularity.

Source
Usaco 2003 Fall

Main topic:

There are n cows, all want to become most poluler cattle, give m relationships, such as (1) Welcome 2, the relationship can be passed, but can not mutually, that is 1 Welcome 2 does not mean 2 welcome 1, but if 2 also welcome 3 then 1 also welcome 3.
Give a n,m and M welcome relationship to the number of cows that are welcomed by all cows.

Ideas:

1. Use Tarjan to find the double connected component and then indent it into points. These points form a tree.

2, the number of nodes in the tree to find out the degree of zero, if there is a output that point contains all the points (because it is a shrinking point out of the tree).

Attention:

1, the given diagram will have the possibility of non-connectivity, if so sure output zero. Because it's not connected, there's definitely not going to be a situation where all the other cows think a cow is bull.

2, if there are many points after the contraction point, then output zero. Because the picture is connected, there is no case that all the other cows think that a cow is very bull.

Tarjan algorithm can also be collected as a template, if not understand, recommended to find a blog carefully understand.

#include <iostream> #include <cstring> using namespace std;
#define MAXN 10001 int n,m; struct node {int to,next;}
EDGE[MAXN*5];

int head[maxn],cnt,time,bcnt,top;
    void Add (int u,int v) {edge[cnt].to=v;
    Edge[cnt].next=head[u];
head[u]=cnt++;
} int BELONG[MAXN],DFN[MAXN],LOW[MAXN],STACK[MAXN];                                      int OUT[MAXN];

Record the degree of bool INSTACK[MAXN];
    void Tarjan (int u) {int V;           Dfn[u]=low[u]=++time;                 The DFS traversal ordinal of the Mark Point U stack[top++]=u;               Pressed into the stack instack[u]=true;
        The mark U point is already in the stack for (int j=head[u];j!=-1;j=edge[j].next) {v=edge[j].to;
            if (!dfn[v])//If the adjacency point of U is not into the stack {Tarjan (v);     Low[u]=min (Low[u],low[v]); If you can reach this point directly not in the stack, the earliest ancestor of U is the lesser of them} else if (Instack[v])//If low[u]=min in the stack (low[u],dfn[    V]); Then the earliest ancestor of U is his ordinal number and the smaller one in the dot ordinal (Dfn[u]==low[u])//if DFN[U] and low[u] are equal, then the U point is theStrongly connected Branch DFS traversal starting point, this strongly connected branch all points are above the U point {bcnt++;
            do{V=stack[--top];
            Instack[v]=false;
        belong[v]=bcnt;          }while (U!=V);
    The stack, which holds the points belonging to the same strong connected branch, the value of Belong[v] represents the ordinal of the strongly connected branch of the Vertex V ()} int main () {int n,m,a,b,u,v,ans;
        while (cin>>n>>m) {cnt=0,ans=0;
        memset (head,-1,sizeof (head));
            while (m--) {cin>>a>>b;                             Add (A, b);
        Build the diagram} memset (Dfn,0,sizeof (DFN));
        bcnt=top=time=0;;                         
        for (int u=1;u<=n;u++)//taijan if (!dfn[u]) Tarjan (U);
        Memset (out,0,sizeof (out)); 
            for (int u=1;u<=n;u++)//Taijan the degree of the indent after {for (int j=head[u];j!=-1;j=edge[j].next)
                {v=edge[j].to; if (Belong[u]!=belong[v])//If the adjacency point of U does not belong to the same connected branch as U out[belong[u]]++;                         The total exit of the connected branch ordinal of U belongs to an increment of one}} int num=0;                          Num records the number of zeros with a scale of 0 int flag;  
          Flag Entry degree 0 of the indentation mark (that is, the number of connected components) for (int i=1;i<=bcnt;i++)//In the BCNT connected branch statistics of the number of connected branches of 0, and flag to save the serial number of the connected branch
              if (out[i]==0) {num++;
          Flag=i; } if (num==1)//Only one connected component has a 0 for (int i=1;i<=n;i++) if (belong[i]==
        Flag)//Calculate all bovine ans++ belonging to this connected component;   
    cout<<ans<<endl;
} return 0; }

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.