【POJ 2186】Popular Cows(强连通分量_tarjan)

问题描述
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.
输入
* 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.
    输出
  • 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
    样例输出
    1
    算法讨论
    题目大意:n头奶牛,给出若干个欢迎关系a b,表示a欢迎b,欢迎关系是单向的,但是是可以传递的。另外每个奶牛都是欢迎他自己的。求出被所有的奶牛欢迎的奶牛的数目
    先跑一遍taijian算法。那么出度为0的强连通分量代表的就是受其他奶牛欢迎的,但是如果出度为0的强连通分量的个数大于1.那么则无解。因为将至少有两个分量里的奶牛互相不喜欢。所以我们的算法就是如果出度为0的强连通分量的个数是1.那么我们算出这里面点的个数就是最后的答案。
#include<cstdio>
#include<cstring>
using namespace std;
struct edge
{
    int x,y,n;
}g[50005];
int dfn[10005],low[10005],stack[10005],belong[10005],cd[10005],rd[10005],ls[50005];
bool instack[10005];
int n,m,tot=0,bcnt=0,dindex=0;

int tarjan(int x)
{
    int j=0,t=0;
    dindex++;
    dfn[x]=dindex;
    low[x]=dindex;
    tot++;
    stack[tot]=x;
    instack[x]=true;
    t=ls[x];
    while (t!=0)
    {
        j=g[t].y;
        if (dfn[j]==0)
        {
            tarjan(j);
            if (low[j]<low[x])
                low[x]=low[j];
        }
        else
            if ((instack[j])&&(dfn[j]<low[x]))
                low[x]=dfn[j];
        t=g[t].n;
    }
    if (dfn[x]==low[x])
    {
        bcnt++;
        do
        {
            j=stack[tot];
            instack[j]=false;
            tot--;
            belong[j]=bcnt;
        }
        while (x!=j);
    }
    return 0;
}

int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;i++)
    {
        scanf("%d%d",&g[i].x,&g[i].y);
        g[i].n=ls[g[i].x];
        ls[g[i].x]=i;
    }
    for (int i=1;i<=n;i++)
        if (dfn[i]==0)
            tarjan(i);
    for (int i=1;i<=m;i++)
        if (belong[g[i].x]!=belong[g[i].y])
        {
            cd[belong[g[i].x]]++;
            rd[belong[g[i].y]]++;
        }
    int ans=0,p=0;
    for (int i=1;i<=bcnt;i++)
    {
        if (cd[i]==0)
        {
            ans++;
            p=i;
        }
        if (ans>1)
        {
            printf("0\n");
            return 0;
        }
    }
    if (ans!=1)
    {
        printf("0\n");
        return 0;
    }
    int s=0;
    for (int i=1;i<=n;i++)
        if (belong[i]==p)
            s++;
    printf("%d\n",s);
    return 0;
}

这里写图片描述
Pixiv ID:36728381

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值