poj 2186 Popular Cows(强连通分量模板题,tarjan)

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 30374 Accepted: 12323

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个关系,m个关系(a,b)表示a觉得b很受欢迎,并且如果a觉得b很受欢迎,b觉得c很受欢迎,那么a觉得c很受欢迎,现在求出受所有牛(不包括自身)欢迎的牛的个数

思路:刚开始还以为要用并查集去处理,其实并不用。

这里我们把所有的牛进行缩点,这样每个点里的牛都是相互之间受欢迎的,因为受所有牛欢迎的牛相互之间肯定是有关系的,不然就不可能受“所有”牛欢迎了

并且缩完点之后是一个DAG图, 有向无环图。(如果两个点有环,那么会缩成一个点)

如果出度为0的点只有一个那么输出这个点包含的牛的个数即可,不然输出0

因为如果出度大于0的点不止一个那么图一定不互相连通(有一些牛跟这个点没任何关系)

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define N 10050
#define M 50050
struct Edge
{
    int v,next;
} edge[M];
int cnt,head[N];
int dfn[N];
int low[N];
int s[N],top;
int instack[N];
int num;
int index;
int belong[N];
int out[N];
void init()
{
    cnt=0;
    num=1;
    index = 0;
    top=0;
    memset(dfn, -1, sizeof(dfn));
    memset(low, -1, sizeof(low));
    memset(head, -1, sizeof(head));
    memset(instack, 0, sizeof(instack));
    memset(belong, 0, sizeof(belong));
    memset(out, 0, sizeof(out));
}
void addedge(int u,int v)
{
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void tarjan(int u)
{
    int v, i;
    dfn[u] = low[u] = num++;
    instack[u] = 1;
    s[++top] = u;
    for(i = head[u]; i != -1; i = edge[i].next)
    {
        v = edge[i].v;
        if(dfn[v] == -1)
        {
            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])
    {
        index++;
        do
        {
            v = s[top--];
            instack[v] = 0;
            belong[v] = index;
        }
        while(top != 0 && v != u);
    }
}
int main()
{
    int n,m;
    int u,v;
    while(~scanf("%d %d",&n,&m))
    {
        init();
        for(int i=1; i<=m; i++)
        {
            scanf("%d %d",&u,&v);
            addedge(u,v);
        }
        for(int i=1; i<=n; i++)
            if(dfn[i]==-1)
                tarjan(i);
        for(int j=1; j<=n; j++)
            for(int i=head[j]; i!=-1; i=edge[i].next)
                if(belong[j]!=belong[edge[i].v])
                    out[belong[j]]++;
        int ans,tot=0;
        for(int i=1; i<=index; i++)
            if(!out[i])
            {
                tot++;
                ans=i;
            }
        for(int i=1;i<=n;i++)
            if(!belong[i])
              tot=0;
        if(tot==1)
        {
            int q=0;
            for(int i=1; i<=n; i++)
                if(belong[i]==ans)
                    q++;
            printf("%d\n",q);
        }
        else printf("0\n");
    }
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值