#include <cstdio> #include <iostream> using namespace std; const int N = 1E4 + 10; const int M = 5E4 + 10; struct node { int to, nxt; } e[M]; int head[N], tot; void add(int u, int v) { e[++tot].to = v; e[tot].nxt = head[u]; head[u] = tot; } int dfn[N], low[N], dfncnt, s[N], tp; int scc[N], sc; int sz[N], n, m, out[N]; void tarjan(int u) { low[u] = dfn[u] = ++dfncnt, s[++tp] = u; for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to; if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]); else if (!scc[v]) low[u] = min(low[u], dfn[v]); } if (dfn[u] == low[u]) { ++sc; while (s[tp] != u) scc[s[tp]] = sc, sz[sc]++, --tp; scc[s[tp]] = sc, sz[sc]++, --tp; } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); add(u, v); } for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i); for (int u = 1; u <= n; u++) for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to; if (scc[u] == scc[v]) continue; out[scc[u]]++; } int ans = 0; for (int i = 1; i <= sc; i++) if (!out[i]) if (!ans) ans = i; else { cout << 0; return 0; } cout << sz[ans]; }
链接:https://loj.ac/p/10091
标签:10091,sc,int,tp,++,3.5,low,受欢迎,dfn From: https://www.cnblogs.com/acmLLF/p/17137292.html