POJ - 2186 Popular Cows

题面

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奶牛,求出被所有奶牛崇拜的奶牛的数量。

思路及分析:

首先非常容易想到的就是朴素算法,枚举每一头奶牛,看是否所有奶牛都崇拜它,如果是则答案+1,这样的做法虽然简单,但是复杂度却o(mn),那么有没有简单的做法呢?

当然有,通过观察可以发现,这是一张有向图,其中很有可能存在环,而环在这里面的性质非常特殊,假如有一头环外的奶牛a崇拜一头环内的奶牛b,那么a将崇拜环内的所有奶牛,同理,假如有一头环内的奶牛a崇拜一头环外的奶牛b,那么整个环内的奶牛都会崇拜b,那么这个环就可以看出一只大奶牛,肚子里有很多小奶牛(这个比喻有点不恰当),总之就是把整个环看成一个点,也就是所谓的缩点思想,那么如何求出所有的环并将其缩点呢?我们注意到有向图中的环就是强连通分量,求强连通分量可以用tarjan算法,然后可以用一个数组对其每个点进行染色,同一个环上的点染相同的颜色。最后再遍历所有边,如果边两边的的点不在同一个环内,也就是染的颜色不一样,说明起始边所在的环崇拜到达边所在的环,就在这两个环直接连接一条有向边。最后如何判断哪一个环是被其它所有环崇拜的?答案很简单,出度为0的环便是,假如有两个或两个以上环出度都为0,说明它们相互不崇拜,则答案为0,否则输出出度为0的环上的点的数量即可。由于只需要知道每个环的出度,那么每个环直接不需要真正连上边,只需要计算出度就可以了。
这个算法的复杂度为o(m)。
AC代码:

#include<iostream>
#include<cstring>
#include<stack>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define per(i,x,n) for(int i=n-1;i>=x;i--)
using namespace std;
//head
const int maxn=10006;
struct Edge{int to,next;}edge[50006];
int n,m,a,b,tot=0,cnt=0,cur=0,head[maxn],vis[maxn],low[maxn],dfn[maxn],color[maxn],ans[50006],out[50006];
stack<int>q;
void addedge(){edge[cur].to=b;edge[cur].next=head[a];head[a]=cur++;}
void tarjan(int x)
{
    low[x]=dfn[x]=++tot;
    q.push(x);
    vis[x]=1;
    for(int i=head[x];i!=-1;i=edge[i].next)
    {
        int nx=edge[i].to;
        if(!dfn[nx])
        {
            tarjan(nx);
            low[x]=min(low[x],low[nx]);
        }
        else if(vis[nx])
            low[x]=min(low[x],dfn[nx]);
    }
    if(low[x]==dfn[x])
    {
        cnt=0;
        int tmp=0;
        while(!q.empty())
        {
            tmp=q.top();
            q.pop();
            vis[tmp]=0;
            cnt++;
            color[tmp]=cur;
            if(tmp==x)break;
        }
        ans[cur]=cnt;
        ++cur;
    }
}
int main()
{
     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
     memset(head,-1,sizeof(head));
     cin>>n>>m;
     rep(i,0,m)cin>>a>>b,addedge();
     cur=0;
     rep(i,1,n+1)if(!dfn[i])tarjan(i);
     rep(i,1,n+1)
     {
        for(int x=head[i];x!=-1;x=edge[x].next)
            {if(color[i]!=color[edge[x].to])out[color[i]]++;}
     }
     cnt=0;int num=-1;
     rep(i,0,cur)
     {
       if(out[i]==0){cnt++,num=i;if(cnt==2){cout<<0<<endl;return 0;}}
     }
     if(num!=-1)
     cout<<ans[num]<<endl;
     else cout<<0<<endl;
     return 0;
}

版权声明:本文为原创文章,转载请标明出处。
https://blog.csdn.net/u014390156

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值