#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 10005;
vector<int>e[N];
int dfn[N], low[N], inn[N], outt[N], scc[N], cnt, tot, instk[N], stk[N], top, siz[N];
void Tarjan(int x)
{
dfn[x] = low[x] = ++tot;
stk[++top] = x; instk[x] = 1;
for (int y : e[x])
{
if (!dfn[x])
{
Tarjan(y);
low[x] = min(low[x], low[y]);
}
else if(instk[y])
{
low[x] = min(low[x], dfn[y]);
}
}
if (low[x] == dfn[x])
{
int y; ++cnt;
do
{
y = stk[top--];
instk[y] = 0;
scc[y] = cnt;
siz[cnt]++;
} while (x!=y);
}
}
int main()
{
int n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
e[u].push_back(v);
}
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])
{
inn[scc[y]]++;
outt[scc[x]]++;
}
}
}
int ans = 0,sum=0;
for (int i = 1; i <= cnt; i++)
{
if (!outt[i])
{
sum++;
ans += siz[i];
}
}
if (sum == 1) cout << ans;
else cout << 0;
return 0;
}
如题,求大佬调教