POJ 2186 Popular Cows 强连通分量

E - Popular Cows
Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u

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.

题目大意

对于关系(A,B)表示A牛喜欢B牛

如果(A,B)(B,C)则A牛也喜欢C牛,这是可传递的

求受到所以牛喜欢牛的个数



思路:

用tarjan求强连通分量并缩点,然后看每个点的出度,当且仅当只有一个点的出度为0时候这个点所带表的牛被所有的牛喜欢。

//原来边的个数开小了也会tle

ACcode:

#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define maxn 10005
using namespace std;
struct Edge{
    int to,next;
}my[5*maxn];
int head[maxn],tot;
int low[maxn],dfn[maxn],Stack[maxn],Belong[maxn],cdu[maxn],num[maxn];
int index,top,scc;
bool instack[maxn];
void add(){
    int u,v;
    scanf("%d%d",&u,&v);
    my[tot].to=v;my[tot].next=head[u];head[u]=tot++;
}
void tarjan(int u){
    int v;
    low[u]=dfn[u]=++index;
    Stack[top++]=u;
    instack[u]=true;
    for(int i=head[u];i!=-1;i=my[i].next){
        v=my[i].to;
        if(!dfn[v]){
            tarjan(v);
            if(low[u]>low[v])low[u]=low[v];
        }
            else if(instack[v]&&low[u]>dfn[v])low[u]=dfn[v];
    }
    if(low[u]==dfn[u]){
        scc++;
        do{
            v=Stack[--top];
            instack[v]=false;
            Belong[v]=scc;
            num[scc]++;
        }
            while(v!=u);
    }
}
void init(){
    memset(dfn,0,sizeof(dfn));
    memset(instack,false,sizeof(instack));
    memset(head,-1,sizeof(head));
    memset(num,0,sizeof(num));
    memset(cdu,0,sizeof(cdu));
    index=scc=top=tot=0;
}
int main(){
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        init();
        while(m--)add();
        for(int i=1;i<=n;i++)
            if(!dfn[i])
                tarjan(i);
        int v;
        for(int i=1;i<=n;++i){
            for(int k=head[i];k!=-1;k=my[k].next){
                v=my[k].to;
                if(Belong[i]!=Belong[v])
                    cdu[Belong[i]]++;
            }
        }
        int flag=0;
        int ans=0;
        for(int i=1;i<=scc;i++)
            if(cdu[i]==0){
                flag++;
                ans=num[i];
            }
        if(flag!=1)
            ans=0;
        printf("%d\n",ans);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值