Popular Cows
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 37824 | Accepted: 15398 |
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.
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.
* 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.
//Poj - 2186 强连通分量
//题意:
//总共有n头牛,m个有序对(a, b) 代表牛a崇拜牛b, 崇拜具有传递性,
//求出被其他所有牛崇拜的牛的总数。
//思路:
//如果枚举每一个点, 看看能不能连通其他的所有顶点, 复杂度是O(nm) 会超时,
//用强连通分量分解, 将相互连通的点缩成一个点,最后在对强连通分解后的图求拓扑排序,
//那么只需验证拓扑序最后一位的缩点是否能连通其他的点, 如果可以那么就输出最后一个缩点集合的个数。
//复杂度为 O((n + m) + n)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
#define v_int vector<int>
#define vv_int vector<v_int>
#define ll long long
#define INF (ll)0x3f3f3f3f3f3f3f
const int maxn = 1e5 + 10;
const int maxm = 5e5 + 10;
v_int G[maxn], rG[maxn], sc;
int n, m;
int vis[maxn], a[maxn], b[maxn], topo[maxn];
void addEdge(int from, int to) {
G[from].push_back(to);
rG[to].push_back(from);
}
void dfs(int v) {
vis[v] = 1;
for (int i = 0; i < G[v].size(); i++) {
if (!vis[G[v][i]]) dfs(G[v][i]);
}
sc.push_back(v);
}
void rdfs(int v, int k) {
vis[v] = 1;
topo[v] = k;
for (int i = 0; i < rG[v].size(); i++) {
if (!vis[rG[v][i]]) rdfs(rG[v][i], k);
}
}
int scc() {
sc.clear();
memset(vis, 0, sizeof(vis));
for (int i = 0; i < n; i++) {
if (!vis[i]) dfs(i);
}
memset(vis, 0, sizeof(vis));
int k = 0;
for (int i = sc.size() - 1; i >= 0; i--) {
if (!vis[sc[i]]) rdfs(sc[i], k++);
}
return k;
}
void solve() {
for (int i = 0; i < m; i++)
addEdge(a[i] - 1, b[i] - 1);
int cur_n = scc();
int ans = 0, flag_u = 0;
for (int i = 0; i < n; i++) {
if (topo[i] == cur_n - 1) {
flag_u = i;
ans++;
}
}
memset(vis, 0, sizeof(vis));
rdfs(flag_u, 0);
for (int i = 0; i < n; i++) {
if (!vis[i]) {
ans = 0; break;
}
}
cout << ans;
}
int main() {
#ifdef ZTZTMS
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i];
}
solve();
return 0;
}