poj 2186 Popular Cows

题目:

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 18659 Accepted: 7516

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. 


代码:

#include<iostream>
#include<stdio.h>
#include<stack>
#include<string.h>
using namespace std;
#define MAXN 10010
struct Node
{
    int v;
    int next;
} edge[50010];
int head[MAXN];
int dfn[MAXN];
int low[MAXN];
bool mark[MAXN]; //to judge in stack or not
int id[MAXN]; //the id of scc
int T;
stack<int> sta;
int scc;
void tarjan(int v)
{
    dfn[v] = low[v] = ++T;
    sta.push(v);
    mark[v] = true;
    int k;
    for (k = head[v]; k != -1; k = edge[k].next)
    {
        int u = edge[k].v;
        if (!dfn[u])
        {
            tarjan(u);
            low[v] = min(low[v], low[u]);
        }
        else if (mark[u])
            low[v] = min(low[v], dfn[u]);
    }
    if (low[v] == dfn[v])
    {
        ++scc;
        int u;
        do
        {
            u = sta.top();
            sta.pop();
            mark[u] = false;
            id[u] = scc;
        }
        while (u != v);
    }
    return;
}
void solve(int n)
{
    scc = 0;
    int i;
    for (i = 1; i <= n; ++i)
        if (!dfn[i])
            tarjan(i);
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        head[i]=-1;
        mark[i]=false;
        dfn[i]=0;
    }
    scc=0;
    int a,b;
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&a,&b);
        edge[i].v=b;
        edge[i].next=head[a];
        head[a]=i;
    }
    solve(n);

    memset(mark,false,sizeof(mark));
    for(int i=1;i<=n;i++)
    {
        for(int j=head[i];j!=-1;j=edge[j].next)
        {
            if(id[i]!=id[edge[j].v])
            {
                mark[id[i]]=true;
            }
        }
    }
    int ans=0;
    int idd;
    for(int i=1;i<=scc;i++)
    {
        if(!mark[i])
        {
            ans++;
            idd=i;
        }
    }
    if(ans>1)
    {
        printf("0\n");
    }
    else
    {
        ans=0;
        for(int i=1;i<=n;i++)
        {
            if(id[i]==idd)
            ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值