题目链接
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 100;
int n, m, ans, f[N];
bool vis[N];
vector<int> g[N];
int d(int x) {
if(f[x]) return f[x];
if(g[x].empty()) return 1;
for (int i = 0; i < g[x].size(); i ++) {
f[x] += d(g[x][i]);
}
return f[x];
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i ++) {
int u, v;
cin >> u >> v;
g[u].push_back(v);
vis[v] = 1;
}
for (int i = 1; i <= n; i ++) {
if(vis[i] == 0) {
ans += d(i);
}
}
cout << ans;
return 0;
}