POJ_2186_Popular Cows(强联通分量)

Popular Cows

 POJ - 2186 

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个关系,关系x,y表示牛x喜欢牛y,如果x喜欢y,y喜欢z,那么x也喜欢z。问是否存在一些牛,其他所有牛都喜欢他。

 

思路:先tarjian将环缩成点,然后,如果有两个或两个以上的强联通分量的出度为0的话,这样的牛一定不存在(自己可以画图看看),如果只有一个出度为0的强连通分量的话,那么,都受欢迎的牛就是这个强连通分量里面点的个数

 

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int Max=1e5+10;
struct EDGE
{
    int to,next;
};
EDGE edge[500005];
int n,m,e,cnt,t,Count;
int head[Max],vis[Max],dfsn[Max],low[Max];
int outdeg[Max],col[Max],sta[Max],sum[Max];
void init()
{
    Count=t=cnt=e=0;
    memset(head,-1,sizeof head);
    for(int i=0;i<=n;i++){
        sum[i]=sta[i]=outdeg[i]=vis[i]=dfsn[i]=low[i]=0;
    }
}
void add(int u,int v)
{
    edge[e].to=v;
    edge[e].next=head[u];
    head[u]=e++;
}
void tarjian(int u)
{
    vis[u]=1;
    dfsn[u]=low[u]=cnt++;
    sta[++t]=u;
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(vis[v]==0) tarjian(v);
        if(vis[v]==1) low[u]=min(low[u],low[v]);
    }
    if(dfsn[u]==low[u]){
        Count++;
        do{
            col[sta[t]]=Count;
            sum[Count]++;
            vis[sta[t]]=-1;
        }while(sta[t--]!=u);
    }
}
void solve()
{
    for(int i=1;i<=n;i++)
        if(!vis[i]) tarjian(i);
    for(int i=1;i<=n;i++){
        for(int j=head[i];j!=-1;j=edge[j].next){
            int v=edge[j].to;
            if(col[i]!=col[v]) outdeg[col[i]]++;
        }
    }
    int ans=0,flag=0;
    for(int i=1;i<=Count;i++){
        if(flag&&outdeg[i]==0){
            ans=0;
            break;
        }
        if(!flag&&outdeg[i]==0){
            ans=sum[i];
            flag=1;
        }
    }
    cout<<ans<<endl;
}
int main()
{
    int x,y;
    while(cin>>n>>m){
        init();
        for(int i=0;i<m;i++){
            cin>>x>>y;
            add(x,y);
        }
        solve();
    }
    return 0;
}
/*9 11
1 2
2 3
3 1
2 4
4 5
5 6
6 4
3 7
7 8
8 9
9 7*/

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值