poj2186Popular Cows

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 36337 Accepted: 14804

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
题意:
每一头牛的愿望就是变成一头最受欢迎的牛。现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎。 这种关系是具有
传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认为牛C受欢迎。你的任务是求出有多少头牛被所有的牛认为是受
欢迎的。
思路:
用tarjan求出每个强连通分量,再缩点,统计每个点的出度,如果有且只有1个出度为0的点,就输出这个点包含的节点数,否
则输出0.
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
const int N=55555;
int dfn[N],low[N],belong[N];
bool instack[N];
int flag[N];
int used[N];
int in[N],out[N];
int cnt,index;
int m,n;
vector<int>g[N];
stack<int>s;
void init()
{
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(instack,false,sizeof(instack));
    memset(in,0,sizeof(in));
    memset(out,0,sizeof(out));
    index=cnt=0;
    for(int i=0;i<=n;i++)
    {
        g[i].clear();
    }
    while(!s.empty())
    {
        s.pop();
    }
}
void tarjan(int u)
{
    dfn[u]=low[u]=++index;
    s.push(u);
    instack[u]=true;
    int v;
    for(int i=0;i<g[u].size();i++)
    {
        v=g[u][i];
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(instack[v])
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(low[u]==dfn[u])
    {
        cnt++;
        do{
            v=s.top();
            s.pop();
            belong[v]=cnt;
            instack[v]=false;
        }while(u!=v);
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int i,j;
        int a,b;
        init();
        for(i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            g[a].push_back(b);
        }
        for(i=1;i<=n;i++)
        {
            if(!dfn[i])
                tarjan(i);
        }
        for(i=1;i<=n;i++)
        {
            for(j=0;j<g[i].size();j++)
            {
                int v=g[i][j];
                if(belong[v]!=belong[i])
                {
                    out[belong[i]]++;
                }
            }
        }
        int x;
        int sum=0;
        for(i=1;i<=cnt;i++)
        {
            if(out[i]==0)
            {
                sum++;
                x=i;
            }
        }
        if(sum==1)
        {
            sum=0;
            for(i=1;i<=n;i++)
            {
                if(belong[i]==x)
                {
                    sum++;
                }
            }
            printf("%d\n",sum);
        }
        else printf("0\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值