(连通图 缩点 强联通分支)Popular Cows -- poj --2186

http://poj.org/problem?id=2186

 

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

 

先缩点(把强联通分量看为一个点), 判断出度为 0 的点有几个,如果大于 1 则输出 0, 否则输出 出度为零的点的个数

 

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

#define N 50005

struct node
{
    int v, next;
}a[N];

int Head[N], cnt;
int dfn[N], low[N], Time, bnt, belong[N];
int Stack[N], InStack[N], top;

void Init()
{
    cnt = Time = bnt = top = 0;
    memset(Head, -1, sizeof(Head));
    memset(dfn, 0, sizeof(dfn));
    memset(low, 0, sizeof(low));
    memset(Stack, 0, sizeof(Stack));
    memset(InStack, 0, sizeof(InStack));
}

void Add(int u, int v)
{
    a[cnt].v = v;
    a[cnt].next = Head[u];
    Head[u] = cnt++;
}

void Tarjar(int u)
{
    int v;
    low[u] = dfn[u] = ++Time;
    InStack[u] = 1;
    Stack[top++] = u;

    for(int j=Head[u]; j!=-1; j=a[j].next)
    {
        v = a[j].v;
        if(!dfn[v])
        {
            Tarjar(v);
            low[u] = min(low[u], low[v]);
        }
        else if(InStack[v])
            low[u] = min(low[u], dfn[v]);
    }

    if(dfn[u]==low[u])
    {
        bnt++;
        do
        {
            v = Stack[--top];
            InStack[v] = 0;
            belong[v] = bnt;
        }while(u!=v);
    }
}

int main()
{
    int n, m;
    while(scanf("%d%d", &n, &m)!=EOF)
    {
        int i, u, v;

        Init();
        for(i=1; i<=m; i++)
        {
            scanf("%d%d", &u, &v);
            Add(u, v);
        }

        for(i=1; i<=n; i++)
        {
            if(!dfn[i])
            Tarjar(i);
        }

        int Out[N]={0};
        for(int i=1; i<=n; i++)
        {
            for(int j=Head[i]; j!=-1; j=a[j].next)
            {
                u = belong[i], v = belong[a[j].v];
                if(u!=v)
                    Out[u]++;
            }
        }

        int flag=0, Index;
        for(i=1; i<=bnt; i++)
        {
            if(!Out[i])
            {
                flag++;
                Index = i;
            }
        }

        if(flag>1)
            printf("0\n");
        else
        {
            int ans = 0;
            for(i=1; i<=n; i++)
            {
                if(belong[i]==Index)
                    ans++;
            }
            printf("%d\n", ans);
        }
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/YY56/p/4747685.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值