POJ2186 Popular Cows

来源:互联网 发布:网络直播受众范围 编辑:程序博客网 时间:2024/05/18 17:57

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;}


0 0
原创粉丝点击