#include<bits/stdc++.h> usingnamespace std; constint N = 10010; int n, m, dfn[N], low[N], instk[N], siz[N], top, tot, cnt, scc[N], stk[N], cdu[N]; vector<int> e[N]; voidtarjan(int x) { dfn[x] = low[x] = ++tot; stk[++top] = x; instk[x] = 1;
for (int y : e[x]) { //邻接表存图 if (!dfn[y]) { tarjan(y); low[x] = min(low[x], low[y]); } elseif (instk[y]) { low[x] = min(low[x], low[y]); } }
if (dfn[x] == low[x]) { int y; ++cnt; do { y = stk[top--]; instk[y] = 0; scc[y] = cnt; ++siz[cnt]; } while (y != x); } } intmain() { ios::sync_with_stdio(false), cin.tie(nullptr); cin >> n >> m; for (int i = 1; i <= m; i++) { int a, b; cin >> a >> b; e[a].push_back(b); } for (int i = 1; i <= n; i++) { if (!dfn[i]) tarjan(i); } /*---------------核心代码--------------------*/ //记录强连通分量的出度 for (int x = 1; x <= n; x++) for (int y : e[x]) if (scc[x] != scc[y]) cdu[scc[x]]++;//rdu[scc[y]]++; int ans = 0; for (int i = 1; i <= cnt; i++) { if(!cdu[i]) { if(ans) { cout << 0 << '\n';//出现两个以上出度为 0 的强连通分量 return0; } ans = i; } } cout << siz[ans] << '\n';//输出该强连通分量的大小 /*---------------核心代码--------------------*/ }