# POJ2186Popular Cows

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 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) {
S.push(u);
for(int i = 0; i < (int)G[u].size(); i++) {
int v = G[u][i];
if(!pre[v]) {
dfs(v);
} else if(!sccno[v]) {
}
}
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;
}

• 本文已收录于以下专栏：

## 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
• 2013年07月21日 02:09
• 530

## 【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
• 2016年04月13日 22:28
• 98

举报原因： 您举报文章：POJ2186Popular Cows 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)