poj 2186 Popular Cows tarjan

Popular Cows

Description

Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is 
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are 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 are considered popular by every 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. 
low数组的理解:对于一个点,能从未遍历的边到达的最低的深度优先级
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define inf 2000000001
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    {
        if( ch == EOF )  return 1 << 30 ;
    }
    res = ch - '0' ;
    while( ( ch = getchar() ) >= '0' && ch <= '9' )
        res = res * 10 + ( ch - '0' ) ;
    return res ;
}
struct is
{
    int u,v;
    int next;
}edge[50010];
int head[50010];
int belong[50010];
int dfn[50010];
int low[50010];
int stackk[50010];
int instack[50010];
int number[50010];
int du[100010];
int n,m,jiedge,lu,bel,top;
void update(int u,int v)
{
    jiedge++;
    edge[jiedge].u=u;
    edge[jiedge].v=v;
    edge[jiedge].next=head[u];
    head[u]=jiedge;
}
void dfs(int x)
{
    dfn[x]=low[x]=++lu;
    stackk[++top]=x;
    instack[x]=1;
    for(int i=head[x];i;i=edge[i].next)
    {
        if(!dfn[edge[i].v])
        {
            dfs(edge[i].v);
            low[x]=min(low[x],low[edge[i].v]);
        }
        else if(instack[edge[i].v])
        low[x]=min(low[x],dfn[edge[i].v]);
    }
    if(low[x]==dfn[x])
    {
        int sum=0;
        bel++;
        int ne;
        do
        {
            sum++;
            ne=stackk[top--];
            belong[ne]=bel;
            instack[ne]=0;
        }while(x!=ne);
        number[bel]=sum;
    }
}
void tarjan()
{
    memset(dfn,0,sizeof(dfn));
    bel=lu=top=0;
    for(int i=1;i<=n;i++)
    if(!dfn[i])
    dfs(i);
}
int main()
{
    int i,t;
    while(~scanf("%d%d",&n,&m))
    {
        memset(head,0,sizeof(head));
        jiedge=0;
        for(i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            update(u,v);
        }
        tarjan();
        for(i=1;i<=jiedge;i++)
        if(belong[edge[i].v]!=belong[edge[i].u])
        du[belong[edge[i].u]]++;
        int flag=0,pos;
        /*for(i=1;i<=n;i++)
        cout<<belong[i]<<endl;*/
        for(i=1;i<=bel;i++)
        {
            if(!du[i])
            {
                flag++;
                pos=i;
            }
        }
        if(flag!=1)
        printf("0\n");
        else
        printf("%d\n",number[pos]);
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/jhz033/p/5360779.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值