poj 2186 Popular Cows

来源:互联网 发布:安卓看漫画软件 编辑:程序博客网 时间:2024/06/05 16:32
Language:
Popular Cows
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 25257 Accepted: 10345

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. 
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>using namespace std;const int N = 10000 + 10;const int M = 50000 + 10;int n, m;int head1[N], tot1, head2[N], tot2;bool vis[N];vector<int> v; //后序访问顺序的顶点列表int cmp[N]; //所属强连通分量的拓扑序bool mark[N];int tmp;struct Edge{    int v, next;}e1[M], e2[M];void init(){    v.clear();    tot1 = tot2 = 0;    memset(head1, -1, sizeof(head1));    memset(head2, -1, sizeof(head2));}void adde(int *head, Edge *edge, int &tot, int u, int v){    edge[tot].v = v;    edge[tot].next = head[u];    head[u] = tot++;}void dfs(int u){    vis[u] = true;    for(int i=head1[u]; i!=-1; i=e1[i].next)    {        int v = e1[i].v;        if(!vis[v]) dfs(v);    }    v.push_back(u);}void rdfs(int u, int k){    vis[u] = true;    cmp[u] = k;    for(int i=head2[u]; i!=-1; i=e2[i].next)    {        int v = e2[i].v;        if(!vis[v]) rdfs(v, k);    }}void disp(int *head, Edge *edge){    for(int i=1; i<=n; i++)    {        printf("%d : ", i);        for(int j=head[i]; j!=-1; j=edge[j].next)            cout<<edge[j].v<<"  ";        cout<<endl;    }}int SCC()//查找出有多少个强连通分量{    memset(vis, 0, sizeof(vis));    for(int i=1; i<=n; i++)        if(!vis[i]) dfs(i);    memset(vis, 0, sizeof(vis));    int k=0;    for(int i=v.size()-1; i>=0; i--)        if(!vis[v[i]])       rdfs(v[i], k++);    return k;}void dfs2(int u){    vis[u] = true;    if(!mark[cmp[u]])    {        mark[cmp[u]] = true;        tmp++;    }    for(int i=head2[u]; i!=-1; i=e2[i].next)        {            int v = e2[i].v;            if(!vis[v])                dfs2(v);        }}void solve(){    int k = SCC();    //cout<<k<<endl;    int t = v[0];    //cout<<t<<endl;    memset(vis, 0, sizeof(vis));    memset(mark, 0, sizeof(mark));    tmp = 0;    dfs2(t);    //printf("tmp : %d\n", tmp);    int ans = 0;    if(tmp == k)    {        for(int i=1; i<=n; i++)            if(cmp[i] == k-1)              ans++;    }    printf("%d\n", ans);}int main(){    while(~scanf("%d%d", &n, &m))    {        int u, v;        init();        for(int i=0; i<m; i++)        {            scanf("%d%d", &u, &v);            adde(head1, e1, tot1, u, v); //正向图            adde(head2, e2, tot2, v, u); //反向图        }//        disp(head1, e1);//        cout<<endl;//        disp(head2, e2);        solve();    }    return 0;}/*9 111 22 32 43 44 55 35 66 77 88 99 6*/



0 0