POJ2186-Popular Cows

来源:互联网 发布:软件测试的原理 编辑:程序博客网 时间:2024/06/05 02:43

Popular Cows
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 34411 Accepted: 14028

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

Hint

Cow 3 is the only cow of high popularity. 

Source

USACO 2003 Fall


题意:有n只牛,牛A认为牛B很牛,牛B认为牛C很牛。给你m个关系(谁认为谁牛),求大家都认为它很牛的牛有几只(如果牛A认为牛B很牛,牛B认为牛C很牛。那么牛A认为牛C很牛)

解题思路:先进行强联通分量缩点,若最后的图不是一棵树,那么答案必然为0,若为一棵树,则需要判断出度为零的点是不是只有一个,若只有一个则输出这个点中的所有点,若不止一个则答案为0



#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <cmath>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <functional>#include <climits>using namespace std;#define LL long longconst int INF=0x3f3f3f3f;const int N=10009;int n, m;struct Node{    int v,nt;} edge[N*10];int s[N],cnt;int dfn[N],low[N],id[N],dep;bool vis[N],instack[N],tot[N];int res;stack<int>st;void AddEdge(int u,int v){    edge[cnt].v=v;    edge[cnt].nt=s[u];    s[u]=cnt++;}void tarjan(int u){    st.push(u);    instack[u]=true;    vis[u]=true;    dfn[u]=low[u]=++dep;    for(int i=s[u]; ~i; i=edge[i].nt)    {        int v=edge[i].v;        if(!vis[v])  // 生成树的边.        {            tarjan(v);            low[u]=min(low[u],low[v]);        }        else if(instack[v])//在栈中,回边.            low[u]=min(low[u],dfn[v]);    }    if(dfn[u]==low[u])//顶点u为根的子树是一个强连同块    {        int t;        do        {            id[t=st.top()]=res;            st.pop();            instack[t]=false; //low[t] = n;        }        while(t!=u);        res++;//强连通分量增加    }}void solve(){    res=0,dep=0;    while(!st.empty()) st.pop();    memset(vis,0,sizeof vis);    memset(instack,0,sizeof instack);    for(int i=1; i<=n; i++)        if(!vis[i]) tarjan(i);    // Debug        /* for(int i = 1; i <= n; i++)             printf("dfn[%d] = %d, low[%d] = %d\n", i,dfn[i], i,low[i]);         for(int i = 1; i <= n; i++)             printf("id[%d] = %d\n", i, id[i] );*/    int k,sum=0;    for(int u=1; u<=n; u++)        for(int i=s[u]; ~i; i=edge[i].nt)            if(id[u]!=id[edge[i].v]) tot[id[u]]++;    for(int i=0;i<res;i++)        if(!tot[i]) {sum++,k=i;}    if(sum!=1) printf("0\n");    else    {        sum=0;        for(int i=1;i<=n;i++)            if(id[i]==k) sum++;        printf("%d\n",sum);    }}int main(){    while(~scanf("%d%d",&n,&m))    {        memset(s,-1,sizeof s);        cnt=0;        for(int i=0; i<m; i++)        {            int u,v;            scanf("%d%d",&u,&v);            AddEdge(u,v);        }        solve();    }    return 0;}