#include #include using namespace std; using namespace atcoder; struct SCC{ vector > groups; vector > graph; vector > dag; SCC(int n, vector >& edges){ build_groups(n, edges); build_graph(n, edges); build_dag(n); } void build_groups(int n, vector >& edges){ scc_graph sg(n); for(auto&[x, y] : edges){ sg.add_edge(x, y); } groups = sg.scc(); } void build_graph(int n, vector >& edges){ graph = vector >(n, vector({})); for(auto&[x, y] : edges){ graph[x].push_back(y); } } void build_dag(int n) { int sz = (int)groups.size(); dag = vector >(sz, vector({})); vector group_id(n); for (int i = 0; i < sz; ++i) { for (int x : groups[i]) { group_id[x] = i; } } for (int i = 0; i < n; i++) { for (int k : graph[i]) { int x = group_id[i], y = group_id[k]; if (x != y) { dag[x].push_back(y); } } } for (vector& vec : dag) { sort(vec.begin(), vec.end()); vec.erase(unique(vec.begin(), vec.end()), vec.end()); } } }; int vec_sum(vector& vec){ int ret = 0; for(int x : vec){ ret += x; } return ret; } int main(){ int n, m; cin >> n >> m; vector > edges(m); for(auto&[x, y] : edges){ cin >> x >> y; --x, --y; } SCC scc(n, edges); int N = (int)scc.dag.size(); if(N == 1){ cout << 0 << endl; return 0; } vector zero_in(N, true), zero_out(N, true); for(int i = 0; i < N; ++i){ for(int j : scc.dag[i]){ zero_in[j] = zero_out[i] = false; } } int ans = max(vec_sum(zero_in), vec_sum(zero_out)); cout << ans << endl; return 0; }