思路:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
const int N = 100005, M = 1000005;
int idx=0, idc=1, n, m, cnt=0, MOD;
int head[N], e[M<<1];
int dfn[N], low[N], place[N], wgh[N];
int in[N], out[N], vis[N], f[N], num[N];
bool ins[N];
stack <int> state;
struct Edge{
int to, next;
}ed[M<<1];
void adde(int u, int v) {
ed[++idc].to = v;
ed[idc].next = head[u];
head[u] = idc;
}
void tarjan(int u){
dfn[u] = low[u] = ++idx;
vis[u] = ins[u] = 1;
state.push(u);
for(int i=head[u]; i; i=ed[i].next){
int v = ed[i].to;
if( !vis[v] ){
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if( ins[v] ){
low[u] = min(low[u], dfn[v]);
}
}
if(dfn[u] == low[u]){
cnt++; int t=-1;
while(t != u){
t = state.top();
place[t] = cnt;
ins[t] = 0;
wgh[cnt]++;//记录每个强连通分量中包含的点的个数
state.pop();
}
}
}
void rebuild () {//重新构图
for(int x=1; x<=n; x++)
for(int k=head[x]; k; k=ed[k].next){
int v = ed[k].to;
if(place[x] != place[v])
++in[place[v]], ++out[place[x]];//统计入度
}
}
int main () {
freopen ("roundtrip.in", "r", stdin);
freopen ("roundtrip.out", "w", stdout);
int u, v;
scanf("%d%d", &n, &m);
for(int i=1; i<=m; i++) {
scanf("%d%d", &u, &v);
adde(u, v);
}
for(int i=1; i<=n; i++)
if( !dfn[i] ) tarjan(i);//缩点
rebuild();
int ans1=0, ans2=0;
for(int i=1; i<=cnt; i++) {
if(in[i] == 0) ans1++;
if(out[i] == 0) ans2++;
}
int ans = max(ans1, ans2);
if(cnt == 1) printf("%d", 0);//只有一个联通块
else printf("%d", ans);
return 0;
}