POJ2186Popular Cows

原创 2013年12月01日 22:55:01
Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total 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 3
1 2
2 1
2 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 10
using 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.
//Accepted	2756K	782MS	G++	2296B	2013-12-01 22:54:30

void 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;
}

版权声明:欢迎转载!

相关文章推荐

【强连通分量+缩点】 POJ 2186 Popular Cows

先求出强连通,再缩点。然后qiu

poj 2186 Popular Cows (Tarjan 强连通分量+压缩点)

考察算法:强连通分量+压缩点。 先说明下题目的大意。 有一群牛在相互崇拜。崇拜是单向的。且崇拜是可传递的,即a->b,b->c,可以得到a->c。 分别告诉你总共有n头牛,m条彼此间的崇拜关系,...

POJ2186--Popular Cows

Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1
  • a305657
  • a305657
  • 2013年07月21日 02:09
  • 530

poj2186 - Popular Cows

想看更多的解题报告:http://blog.csdn.net/wangjian8006/article/details/7870410                                ...

poj 2186--Popular Cows

题意:农场有N个大牛,有些大牛喜欢

POJ2186 Popular Cows 【裸缩环+Gabow算法强连通分量分解再建图】

这题比较裸,根据牛的膜拜情况建图,然后强连通分量处理,处理后再建新图,记录出度入度,找出所有出度为0的,如果有2个及以上的,则输出(0),很简单,不可能有被所有牛膜拜的牛了(因为这几个结点互相不可达)...

POJ 2186 Popular Cows

强连通分量

【poj2186】 Popular Cows

http://poj.org/problem?id=2186 (题目链接)题意:给出一个n个点m条边的有向图,求其中没有出度强连通分量所包含的点有几个Solution   其实这道题的题解已经在“题...

POJ - 2186 - Popular Cows (tarjan)

Popular Cows题目传送:Popular Cows思路:tarjan算法求强连通分量AC代码:#include #include #include #include #include ...

POJ_2186_Popular Cows

#include #include #include #include #include #include #include #in...
  • cxy7tv
  • cxy7tv
  • 2016年04月13日 22:28
  • 98
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ2186Popular Cows
举报原因:
原因补充:

(最多只允许输入30个字)