poj2186——Popular Cows(tarjan算法)

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

tarjan将每个强连通分量缩成一个点(因为这些点之间可以互相到达)。题意是求被所有的牛认为受欢迎的牛的个数,因此要求出度为0的缩点,即这个缩点的牛都不认为其他牛受欢迎。但如果有两个以上出度为0的缩点,那么这两个缩点之间是不连通的,不合题意

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <iostream>
#define MAXN 50010
#define inf 0xffffffff
using namespace std;
struct node
{
    int to;
    int next;
} edge[MAXN];
int head[MAXN],cnt,vis[MAXN];
int index,top,ssum;
int dfn[MAXN],low[MAXN],sstack[MAXN];
int ans[MAXN],belong[MAXN],num[MAXN];
int du[MAXN];  //每个缩点的出度
void init()
{
    memset(head,-1,sizeof(head));
    memset(dfn,0,sizeof(dfn));
    memset(vis,0,sizeof(vis));
    memset(low,0,sizeof(low));
    memset(belong,0,sizeof(belong));
    memset(num,0,sizeof(num));
    memset(ans,0,sizeof(ans));
    index=top=cnt=ssum=0;
}
void add(int u,int v)
{
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void tarjan(int u)
{
    int i,v;
    dfn[u]=low[u]=++index;
    sstack[top++]=u;
    vis[u]=1;
    for(i=head[u]; i!=-1; i=edge[i].next)
    {
        v=edge[i].to;
        if(!vis[v])//可做访问标记
        {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        }
        else if(vis[v])
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(low[u]==dfn[u])
    {
        ssum++;//强连通分量+1
        while(top>0&&sstack[top]!=u)  //所有节点出栈清空,即缩点
        {
            top--;
            vis[sstack[top]]=2;
            belong[sstack[top]]=ssum; //节点所在强连通分量的缩点
        }
    }
}
int main()
{
    int n,m,i;
    int u,v;
    while(~scanf("%d%d",&n,&m))
    {
        init();
        for(i=0; i<m; i++)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
        }
        for(i=1; i<=n; ++i)
            if(!vis[i])
                tarjan(i);
        for(i=1; i<=n; i++)
            for(int j=head[i]; j!=-1; j=edge[j].next)
                if(belong[i]!=belong[edge[j].to])
                    du[belong[i]]++;
        int sum=0,x;
        for(i=1; i<=ssum; i++)
            if(!du[i])
            {
                sum++;
                x=i;
            }
        if(sum==1)
        {
            sum=0;
            for(i=1; i<=n; i++)
                if(belong[i]==x)
                    sum++;
            printf("%d\n",sum);
        }
        else
            printf("0\n");
    }
    return 0;
}

你可能感兴趣的:(c,算法,ACM,poj)