poj 2186 Popular Cows(强连通分量模板题,tarjan)

来源:互联网 发布:linux route指令 编辑:程序博客网 时间:2024/05/20 07:54

Popular Cows
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 30374 Accepted: 12323

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 31 22 12 3

Sample Output

1
题意:有n头牛和m个关系,m个关系(a,b)表示a觉得b很受欢迎,并且如果a觉得b很受欢迎,b觉得c很受欢迎,那么a觉得c很受欢迎,现在求出受所有牛(不包括自身)欢迎的牛的个数

思路:刚开始还以为要用并查集去处理,其实并不用。

这里我们把所有的牛进行缩点,这样每个点里的牛都是相互之间受欢迎的,因为受所有牛欢迎的牛相互之间肯定是有关系的,不然就不可能受“所有”牛欢迎了

并且缩完点之后是一个DAG图, 有向无环图。(如果两个点有环,那么会缩成一个点)

如果出度为0的点只有一个那么输出这个点包含的牛的个数即可,不然输出0

因为如果出度大于0的点不止一个那么图一定不互相连通(有一些牛跟这个点没任何关系)

代码:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;#define N 10050#define M 50050struct Edge{    int v,next;} edge[M];int cnt,head[N];int dfn[N];int low[N];int s[N],top;int instack[N];int num;int index;int belong[N];int out[N];void init(){    cnt=0;    num=1;    index = 0;    top=0;    memset(dfn, -1, sizeof(dfn));    memset(low, -1, sizeof(low));    memset(head, -1, sizeof(head));    memset(instack, 0, sizeof(instack));    memset(belong, 0, sizeof(belong));    memset(out, 0, sizeof(out));}void addedge(int u,int v){    edge[cnt].v=v;    edge[cnt].next=head[u];    head[u]=cnt++;}void tarjan(int u){    int v, i;    dfn[u] = low[u] = num++;    instack[u] = 1;    s[++top] = u;    for(i = head[u]; i != -1; i = edge[i].next)    {        v = edge[i].v;        if(dfn[v] == -1)        {            tarjan(v);            low[u] = min(low[u], low[v]);        }        else if(instack[v])        {            low[u] = min(low[u], dfn[v]);        }    }    if(low[u] == dfn[u])    {        index++;        do        {            v = s[top--];            instack[v] = 0;            belong[v] = index;        }        while(top != 0 && v != u);    }}int main(){    int n,m;    int u,v;    while(~scanf("%d %d",&n,&m))    {        init();        for(int i=1; i<=m; i++)        {            scanf("%d %d",&u,&v);            addedge(u,v);        }        for(int i=1; i<=n; i++)            if(dfn[i]==-1)                tarjan(i);        for(int j=1; j<=n; j++)            for(int i=head[j]; i!=-1; i=edge[i].next)                if(belong[j]!=belong[edge[i].v])                    out[belong[j]]++;        int ans,tot=0;        for(int i=1; i<=index; i++)            if(!out[i])            {                tot++;                ans=i;            }        for(int i=1;i<=n;i++)            if(!belong[i])              tot=0;        if(tot==1)        {            int q=0;            for(int i=1; i<=n; i++)                if(belong[i]==ans)                    q++;            printf("%d\n",q);        }        else printf("0\n");    }    return 0;}





0 0
原创粉丝点击