POJ2186Popular Cows

来源:互联网 发布:基轮轮嫂淘宝店 编辑:程序博客网 时间:2024/03/29 19:00
Popular Cows
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 21149 Accepted: 8620

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 <vector>#include <algorithm>#include <cstring>#include <stack>#define DEBUG 10using namespace std;const int maxn = 10000+10;const int maxm = 50000+10;int gn, gm;vector<int> G[maxn], G2[maxn];stack<int> S;int pre[maxn], sccno[maxn], lowlink[maxn];int dfs_clock, scc_cnt;int weight[maxn];// the number of every scc.//Accepted2756K782MSG++2296B2013-12-01 22:54:30void dfs(int u) {    pre[u] = lowlink[u] = ++dfs_clock;    S.push(u);    for(int i = 0; i < (int)G[u].size(); i++) {        int v = G[u][i];        if(!pre[v]) {            dfs(v);            lowlink[u] = min(lowlink[u], lowlink[v]);        } else if(!sccno[v]) {            lowlink[u] = min(lowlink[u], pre[v]);        }    }    if(lowlink[u] == pre[u]) {        scc_cnt++;        for(;;) {            int x = S.top(); S.pop();            sccno[x] = scc_cnt;            if(x == u) break;        }    }}void find_scc(int n) {    dfs_clock = scc_cnt = 0;    memset(pre, 0, sizeof(pre));    memset(sccno, 0, sizeof(pre));    for(int i = 1; i <= gn; i++) {        if(!pre[i]) {            dfs(i);        }    }}void build_map() {    int u, v;    memset(weight, 0, sizeof(weight));    for(int i = 1; i <= gn; i++) {        int v = sccno[i];        weight[v] += 1;    }#ifndef DEBUG    for(int i = 1; i <= scc_cnt; i++) {        printf("weight[%d] = %d\n", i, weight[i]);    }#endif    for(int i = 1; i <= gn; i++) {        for(int j = 0; j < (int)G[i].size(); j++) {            u = sccno[i]; v = sccno[G[i][j]];            if(u != v) {                G2[u].push_back(v);            }        }    }}int work() {    vector<int> V;    V.clear();    for(int i = 1; i <= scc_cnt; i++) {        if(!G2[i].size()) {            V.push_back(i);        }    }    if(V.size() > 1) return 0;    else return weight[V[0]];}int main(){   // freopen("in", "r", stdin);    int u, v;    while(scanf("%d%d", &gn, &gm) != EOF) {        for(int i = 1; i <= gm; i++) {            scanf("%d%d", &u, &v);            G[u].push_back(v);        }        find_scc(gn);        build_map();        int output = work();        printf("%d\n", output);    }    return 0;}

原创粉丝点击