POJ 2186 Popular Cows 【Tarjan+缩点】

55 篇文章 1 订阅
12 篇文章 0 订阅


Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 34405 Accepted: 14025

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. 

Source


原题链接:http://poj.org/problem?id=2186

题意:给你n头牛,m个关系,每个关系表示a崇拜b,问有多少牛被所有牛所崇拜。崇拜具有传递性。

还是先Tarjan缩点,找出出度为0的点,此点为缩点后的点,并判断数量,如果数量为1,则存在,否则不存在。

那个点在缩点前的数量即为被所有牛崇拜的数量。

为加强理解,给上几组数据。

3 3
1 2
2 3
3 1

5 5
1 2
2 3
1 4
4 3
3 5

答案:

3
1

参考博客:http://blog.csdn.net/huzhengnan/article/details/7834242

http://blog.csdn.net/mengxiang000000/article/details/51613914

AC代码:


/**
  * 行有余力,则来刷题!
  * 博客链接:http://blog.csdn.net/hurmishine
  * 个人博客网站:http://wuyunfeng.cn/
*/
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
const int maxn=10000+5;
int n,m;
int low[maxn],dfn[maxn];
bool vis[maxn];
int belong[maxn];
int out[maxn],in[maxn];
vector<int>G[maxn];
stack<int>s;
int cnt,index;
void Init()
{
    cnt=index=0;
    for(int i=0;i<=n;i++)
    {
        G[i].clear();
        low[i]=dfn[i]=0;
    }
}
void Tarjan(int u)
{
    dfn[u]=low[u]=++index;
    s.push(u);
    vis[u]=true;
    for(int i=0;i<G[u].size();i++)
    {
        int v = G[u][i];
        if(!dfn[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(vis[v])
        {
            low[u] = min(low[u],dfn[v]);
        }
    }
    if(dfn[u]==low[u])
    {
        ++cnt;
        int x;
        do
        {
            x = s.top();
            s.pop();
            vis[x]=false;
            belong[x]=cnt;
        }while(x!=u);
    }
}
int main()
{
    //freopen("C:\\Users\\yangzhifang\\Desktop\\data.txt","r",stdin);
    while(cin>>n>>m)
    {
        Init();
        int u,v;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&u,&v);
            G[u].push_back(v);
        }
        for(int i=1;i<=n;i++)
        {
            if(!dfn[i])
                Tarjan(i);
        }
        //cout<<cnt<<endl;
        for(int u=1;u<=n;u++)
        {
            for(int i=0;i<G[u].size();i++)
            {
                int v = G[u][i];
                if(belong[u]!=belong[v])
                {
                    out[belong[u]]++;
                }
            }
        }
        int sum=0;
        int p;
        for(int i=1;i<=cnt;i++)
        {
            if(!out[i])
            {
                sum++;
                p = i;
            }
        }
        if(sum==1)
        {
            int ans=0;
            for(int i=1;i<=n;i++)
            {
                if(belong[i] == p)
                    ans++;
            }
            cout<<ans<<endl;
        }
        else
        {
            cout<<"0"<<endl;
        }
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值