POJ 2186 Popular Cows

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 24076 Accepted: 9879

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只奶牛,其中奶牛A认为奶牛B备受注目,而奶牛B也可能认为奶牛C备受注目。奶牛们的这种“认为”是单向可传递的,就是说若奶牛A认为奶牛B备受注目,但奶牛B不一定会认为奶牛A备受注目。而当A认为B备受注目,且B认为C备受注目时,A一定也认为C备受注目。

       现在给出M对这样的“认为...备受注目”的关系对,问有多少只奶牛被除其本身以外的所有奶牛关注。

算法:假设有两头牛A和B都被其它牛认为是红人。那么显然,A被B认为是红人,B也被A认为是红人,即存在一个包含A、B两个顶点的圈,或者说,A、B同属于一个强连通分量。反之、如果一头牛被其它所有牛认为是红人,那么其所属的强连通分量内的所有牛都被所有其它的牛认为是红人。由此,我们把图进行强连通分量分解后,至多有一个强连通分量满足条件。而且算法分解强连通分量的同时也记录了每个分量的拓扑排序,显然,唯一可能成为解得只可能是拓扑序最后的强连通分量。所以在最后,我们只要检查这个强连通分量是否从其它所有顶点可达就可以了。

下面是AC代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<sstream>
#include<vector>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<queue>
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1 | 1
using namespace std;
const int maxn=100005;
int n,m,V,A[maxn],B[maxn];
vector<int>G[maxn],RG[maxn],vs;
bool vis[maxn];
int cmp[maxn];
void add(int from,int to)
{
    G[from].push_back(to);
    RG[to].push_back(from);
}
void dfs(int v)
{
    vis[v]=1;
    for(int i=0;i<G[v].size();i++)
        if(!vis[G[v][i]]) dfs(G[v][i]);
    vs.push_back(v);
}
void rdfs(int v,int k)
{
    vis[v]=1;
    cmp[v]=k;
    for(int i=0;i<RG[v].size();i++)
        if(!vis[RG[v][i]]) rdfs(RG[v][i],k);
}
int scc()
{
    memset(vis,0,sizeof(vis));
    vs.clear();
    for(int i=0;i<V;i++)
        if(!vis[i]) dfs(i);
    memset(vis,0,sizeof(vis));
    int k=0;
    for(int i=vs.size()-1;i>=0;i--)
        if(!vis[vs[i]]) rdfs(vs[i],k++);
    return k;
}
void solve()
{
    V=n;
    for(int i=0;i<m;i++) add(A[i]-1,B[i]-1);
    int s=scc();
    int u=0,num=0;
    for(int i=0;i<V;i++)
        if(cmp[i]==s-1) u=i,num++;
    memset(vis,0,sizeof(vis));
    rdfs(u,0);
    for(int i=0;i<V;i++)
    if(!vis[i]) {num=0;break;}
    printf("%d\n",num);
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<m;i++) scanf("%d%d",&A[i],&B[i]);
        solve();
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值