POJ2186 (强连通分量缩点后出度为0的分量内点个数)

时间:2024-09-03 11:34:27
Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 27820   Accepted: 11208

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. 

Source

题意:A喜欢B,B喜欢C,则A喜欢C,求被所有人喜欢的人的个数
思路:将每个强连通分量缩点,然后求每个点的出度,如果有多个无出度的点则无解,否则答案就是出度为0的强连通分量内点的个数
/*
ID: LinKArftc
PROG: 2186.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ;
const int maxm = ;
int n, m; struct Edge {
int v, next;
} edge[maxm]; int head[maxn], tot; void init() {
tot = ;
memset(head, -, sizeof(head));
} void addedge(int u, int v) {
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot ++;
} int dfn[maxn], low[maxn], belong[maxn];//每个点属于哪个连通分量
int scc;//连通分量总数,下标 1~scc
int Time;
int ins[maxn];
stack <int> s;
vector <int> vec[maxn];//记录每个连通分量内的点 void tarjan(int u) {
int v;
low[u] = dfn[u] = ++ Time;
s.push(u);
ins[u] = true;
for (int i = head[u]; i + ; i = edge[i].next) {
v = edge[i].v;
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (ins[v]) low[u] = min(low[u], dfn[v]);//写出low[u] = min(low[u], low[v]);也可以
}
if (low[u] == dfn[u]) {
scc ++;
do {
v = s.top();
s.pop();
ins[v] = false;
belong[v] = scc;
vec[scc].push_back(v);
} while (u != v);
}
} int outdeg[maxn]; int main() {
//input;
while (~scanf("%d %d", &n, &m)) {
init();
int u, v;
for (int i = ; i <= m; i ++) {
scanf("%d %d", &u, &v);
addedge(u, v);
}
while (!s.empty()) s.pop();
for (int i = ; i <= maxn; i ++) vec[i].clear();
memset(dfn, , sizeof(dfn));
memset(ins, , sizeof(ins));
Time = ; scc = ;
for (int i = ; i <= n; i ++) {
if (!dfn[i]) tarjan(i);
}
memset(outdeg, , sizeof(outdeg));
for (int u = ; u <= n; u ++) {
for (int i = head[u]; i + ; i = edge[i].next) {
int v = edge[i].v;
if (belong[u] == belong[v]) continue;
outdeg[belong[u]] ++;
}
}
int ans = ;
bool has = false;
bool flag = false;
int ii;
for (int i = ; i <= scc; i ++) {
if (outdeg[i] == ) {
ii = i;
if (has) {
flag = false;
break;
} else {
has = true;
flag = true;
}
}
}
if (flag) printf("%d\n", vec[ii].size());
else printf("0\n");
}
return ;
}