题目链接:
分析:
听说是一个Floyd求传递闭包
被拓扑的标签骗了进去
首先如果整个图不连通那么显然没办法确定,因为两个连通块之间的信息没有办法传递
所以先并查集判一下
然后考虑拓扑排序,一个点能得到确定的排名当且仅当它能被之前所有入过队的点到达
代码:
#include<bits/stdc++.h>
#define N (300 + 10)
#define M (50000 + 10)
using namespace std;
inline int read() {
int cnt = 0, f = 1; char c = getchar();
while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();}
while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + (c ^ 48); c = getchar();}
return cnt * f;
}
int n, m, first[N], nxt[M], to[M], fa[N], x, y, gone[N][N], total = 0, topo[N], rd[N], tot, cnt;
queue <int> q;
void add(int x, int y) {nxt[++tot] = first[x], first[x] = tot, to[tot] = y;}
int get_fa(int x) {return x == fa[x] ? x : fa[x] = get_fa(fa[x]);}
void tpsort() {
for (register int i = 1; i <= n; ++i) if (!rd[i]) q.push(i), topo[++total] = i;
// if (q.size() == 1) ++cnt;
while (!q.empty()) {
int u = q.front(); q.pop();
bool flag = 0;
for (register int i = 1; i <= total; ++i) if (!gone[u][topo[i]]) flag = 1;
if (!flag && !q.size()) ++cnt;
for (register int i = first[u]; i; i = nxt[i]) {
int v = to[i];
--rd[v];
for (register int i = 1; i <= n; ++i) gone[v][i] |= gone[u][i];
if (!rd[v]) q.push(v), topo[++total] = v;
}
}
}
int main() {
// freopen("1.in", "r", stdin);
n = read(), m = read();
for (register int i = 1; i <= n; ++i) fa[i] = i, gone[i][i] = 1;
for (register int i = 1; i <= m; ++i) {
x = read(), y = read();
add(x, y), ++rd[y], fa[get_fa(y)] = get_fa(x);
}
for (register int i = 1; i <= n; ++i) if (fa[i] == i) ++cnt;
if (cnt > 1) return printf("0"), 0;
cnt = 0;
tpsort();
printf("%d", cnt);
return 0;
}