POJ2186 Popular Cows

Popular Cows

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 22818 Accepted: 9355
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.
Source

USACO 2003 Fall

题意:有n头牛,找出其中最受欢迎的牛有几头。

先强连通把图分成很多强连通分量,每个强连通分量里的欢迎指数一定是相同的,所以只要查看出度就可以了,把强连通分量看成一个点,找出度为0的那个点,因为如果出度不为0,那么这个点肯定指向了另外的点,另外的点的欢迎指数肯定比这个高,出度为0的点直接输出数量就行了,如果有多个出度为0的点,输出0,在这里一直WA.....

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
const int MAXN=10010;
const int MAXE=50010;
struct EDGE
{
    int v,next;
}G[MAXE];
int pre[MAXN],low[MAXN],sccno[MAXN],dfs_clock,sc_cnt;
int cnt[MAXN],in[MAXN],out[MAXN];
stack<int> S;
int head[MAXN],size;
void init()
{
    memset(pre,0,sizeof(pre));
    memset(sccno,0,sizeof(sccno));
    memset(head,-1,sizeof(head));
    memset(cnt,0,sizeof(cnt));
    dfs_clock=sc_cnt=size=0;
}
void add(int u,int v)
{
    G[size].v=v;
    G[size].next=head[u];
    head[u]=size++;
}
void tarjan(int u)
{
    pre[u]=low[u]=++dfs_clock;
    S.push(u);
    for(int i=head[u];i!=-1;i=G[i].next)
    {
        int v=G[i].v;
        if(!pre[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(!sccno[v])
        {
            low[u]=min(low[u],pre[v]);
        }
    }
    if(pre[u]==low[u])
    {
        sc_cnt++;
        while(1)
        {
            int x=S.top();
            S.pop();
            sccno[x]=sc_cnt;
            cnt[sc_cnt]++;
            if(u==x)
                break;
        }
    }
}
int main()
{
    int n,m,i;
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        init();
        int u,v;
        while(m--)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
        }
        for(i=1;i<=n;i++)
            if(!pre[i])
            tarjan(i);
        if(sc_cnt==1)
        {
            printf("%d\n",cnt[sc_cnt]);
            continue;
        }
        for(i=1;i<=n;i++)
            in[i]=out[i]=0;
        for(u=1;u<=n;u++)
        {
            for(i=head[u];i!=-1;i=G[i].next)
            {
                v=G[i].v;
                if(sccno[u]!=sccno[v])
                {
                    in[sccno[v]]++;
                    out[sccno[u]]++;
                }
            }
        }
        int ans=0;
        int flag=0;
        for(i=1;i<=sc_cnt;i++)
        {
            if(out[i]==0)
            {
                flag++;
                ans=i;
            }
        }
        if(flag==1)
        printf("%d\n",cnt[ans]);
        else
            printf("0\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值