POJ 2186 Popular Cows (强连通分量)

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.

题意,给你n个牛,和m个序列(A,B),表示A认为B是红人,且这个关系会传递,比如A认为B是红牛,B认为C是红牛,则A认为C是红牛。问你有多少头牛被其他所有牛认为是红牛

思路:
这个题最开始我以为是传递闭包,结果这个n是1w,显然会超时,仔细想一下可以看出,对图进行缩点后,出度为0的点就是被所有人认为是红牛的点。求一下这个强连通分量的数目就行了。不过用kosaraju
算法求强联通分量,已经是拓扑排序好的,最后一个序号的点就是出度为0的点。这题要注意,图首先要是联通的。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<set>
#include<vector>
#include<algorithm>

using namespace std;
const int MAX_V = 10010;
const int MAX_E = 50010;
vector<int> G[MAX_V],RG[MAX_V],vs;
int cmp[MAX_V];
bool used[MAX_V];
int V,E;
void init(){
    for(int i=1;i<=V;++i){
        G[i].clear();
        RG[i].clear();
    }
    vs.clear();
    memset(used,false,sizeof(used));
}
void add(int u,int v){
    G[u].push_back(v);
    RG[v].push_back(u);
}
void dfs(int v){
    used[v] = true;
    for(int i=0;i<G[v].size();++i){
        if(!used[G[v][i]])  dfs(G[v][i]);
    }
    vs.push_back(v);
}
void rdfs(int v,int k){
    used[v] = true;
    cmp[v] = k;
    for(int i=0;i<RG[v].size();++i){
        if(!used[RG[v][i]]) rdfs(RG[v][i],k);
    }
}
int scc(){
    vs.clear();
    memset(used,false,sizeof(used));
    for(int i=1;i<=V;++i)
        if(!used[i])    dfs(i);
    memset(used,false,sizeof(used));
    int k = 0;
    for(int i=vs.size()-1;i >=0;--i){
        if(!used[vs[i]])    rdfs(vs[i],++k);
    }
    return k;
}
int main(void){
  //  freopen("in.txt","r",stdin);
    while(scanf("%d%d",&V,&E) != EOF){
        int x,y;
        for(int i=1;i<=E;++i){
            scanf("%d%d",&x,&y);
            add(x,y);
        }
        int k = scc();
        int u,num = 0;
        for(int i=1;i<=V;++i){
            if(cmp[i] == k){
                u = i;
                num++;
            }
        }
        memset(used,false,sizeof(used));
        rdfs(u,0);
        for(int i=1;i<=V;++i)
        if(!used[i]){
            num = 0;
            break;
        }
        printf("%d\n",num);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值