poj 2186 Popular Cows(找出度为0的点)

Popular Cows
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 20288 Accepted: 8236

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受欢迎,要求找出哪些牛都被除自己外的其他所有牛认为受欢迎,输出这些牛有多少头。
思路:先缩点,显然在一个强连通分量中所有牛都互相认为对方受欢迎。对缩点后的新图,找出度为0的点,这时若出度为0的点多于1个,那么就没有满足要求的牛了,因为总有牛不被其他某些牛认为受欢迎。若出度为0的点只有1个,那么输出这个强连通分量里点的数目就可以了。

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <map>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll long long
using namespace std;

const int maxn=10005;
const int maxm=50005;
const double INF=1000000000;

struct node
{
    int v,next;
}edge[maxm];
int head[maxn],scc[maxn],amount[maxn],out[maxn];
int low[maxn],dfn[maxn],stack[maxn];
bool ins[maxn];
int n,m,num,cnt,snum,top;
void init()
{
    memset(head,-1,sizeof(head));
    num=0;
}
void add(int u,int v)
{
    edge[num].v=v;
    edge[num].next=head[u];
    head[u]=num++;
}
void input()
{
    int a,b;
    while(m--)
    {
        scanf("%d%d",&a,&b);
        add(a,b);
    }
}
void dfs(int u)
{
    int x;
    dfn[u]=low[u]=++cnt;
    stack[top++]=u;
    ins[u]=true;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!dfn[v])
        {
            dfs(v);
            low[u]=min(low[u],low[v]);
        }
        else if(ins[v]) low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u])
    {
        snum++;
        do{
            x=stack[--top];
            ins[x]=false;
            scc[x]=snum;
            amount[snum]++;
        }while(x!=u);
    }
}
void tarjan()
{
    memset(ins,false,sizeof(ins));
    memset(dfn,0,sizeof(dfn));
    memset(amount,0,sizeof(amount));
    snum=top=cnt=0;
    for(int i=1;i<=n;i++)
    if(!dfn[i]) dfs(i);
}
void solve()
{
    memset(out,0,sizeof(out));
    for(int u=1;u<=n;u++)
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(scc[u]!=scc[v])
        out[scc[u]]++;
    }
    int k=-1;
    bool flag=true;
    for(int i=1;i<=snum;i++)
    {
        if(!out[i])
        {
            if(k==-1) k=i;
            else {flag=false; break;}
        }
    }
    if(flag) printf("%d\n",amount[k]);
    else printf("0\n");
}
int main()
{
   while(~scanf("%d%d",&n,&m))
   {
       init();
       input();
       tarjan();
       solve();
   }
   return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值