#include // clang-format off std::ostream&operator<<(std::ostream&os,std::int8_t x){return os<<(int)x;} std::ostream&operator<<(std::ostream&os,std::uint8_t x){return os<<(int)x;} std::ostream&operator<<(std::ostream&os,const __int128_t &v){if(!v)os<<"0";__int128_t tmp=v<0?(os<<"-",-v):v;std::string s;while(tmp)s+='0'+(tmp%10),tmp/=10;return std::reverse(s.begin(),s.end()),os<std::ostream &operator<<(std::ostream&os,const std::pair&x){return os<<"("<std::ostream &operator<<(std::ostream&os,const std::vector&vec){os<<'[';for(int _=0,__= vec.size();_<__;++_)os<<(_ ?", ":"")<std::ostream &operator<<(std::ostream&os,const std::set&s){os<<'{';int _=0;for(const auto &x:s)os<<(_++ ? ", " : "")<std::ostream&operator<<(std::ostream &os,const std::array &arr) {os<<'['<void print(std::ostream&os,const Tup &x,std::index_sequence){(void)(int[]){(os<(x)<<", ",0)...};} templatestd::ostream &operator<<(std::ostream&os,const std::tuple &x) {static constexpr std::size_t N = sizeof...(Args);os<<"(";if constexpr(N>=2)print(os,x,std::make_index_sequence());return os<(x)<<")";} const std::string COLOR_RESET="\033[0m",BRIGHT_GREEN="\033[1;32m",BRIGHT_RED="\033[1;31m",BRIGHT_CYAN="\033[1;36m",NORMAL_CROSSED="\033[0;9;37m",ITALIC="\033[3m",BOLD="\033[1m",RED_BACKGROUND="\033[1;41m",NORMAL_FAINT="\033[0;2m"; #define func_LINE_FILE NORMAL_FAINT<<" in "<"<> adj, rev; public: StronglyConnectedComponents(int n): adj(n), rev(n) {} void add_edge(int src, int dst) { adj[src].push_back(dst), rev[dst].push_back(src); } std::vector> get_block() const { const int n= adj.size(); std::vector> blk; std::vector ord(n), par(n, -2), dat(n, 0); int k= n; for (int s= 0; s < n; ++s) if (par[s] == -2) { par[s]= -1; for (int p= s; p >= 0;) { if (dat[p] == (int)adj[p].size()) { ord[--k]= p, p= par[p]; continue; } if (int q= adj[p][dat[p]++]; par[q] == -2) par[q]= p, p= q; } } dat.assign(n, 1); for (int s: ord) if (dat[s]) { blk.resize(++k), dat[s]= 0, blk.back().push_back(s); for (int i= 0; i < (int)blk.back().size(); ++i) for (int v: rev[blk.back()[i]]) if (dat[v]) dat[v]= 0, blk.back().push_back(v); } return blk; } std::vector get_index(const std::vector> &blk) const { std::vector index(adj.size()); for (int i= blk.size(); i--;) for (int v: blk[i]) index[v]= i; return index; } std::vector> get_dag(const std::vector &index, int num) const { std::vector> dag(num); std::vector> es; for (int i= adj.size(); i--;) for (int j: adj[i]) if (int u= index[i], v= index[j]; u != v) es.push_back({u, v}); std::sort(es.begin(), es.end()), es.erase(std::unique(es.begin(), es.end()), es.end()); for (auto [u, v]: es) dag[u].push_back(v); return dag; } }; class TwoSatisfiability { int sz; StronglyConnectedComponents scc; public: TwoSatisfiability(int n): sz(n), scc(2 * n) {} void add_if(int u, int v) { scc.add_edge(u, v), scc.add_edge(neg(v), neg(u)); } // u -> v <=> !v -> !u void add_or(int u, int v) { add_if(neg(u), v); } // u or v <=> !u -> v void add_nand(int u, int v) { add_if(u, neg(v)); } // u nand v <=> u -> !v void set_true(int u) { scc.add_edge(neg(u), u); } // u <=> !u -> u void set_false(int u) { scc.add_edge(u, neg(u)); } // !u <=> u -> !u inline int neg(int x) const { return x >= sz ? x - sz : x + sz; } std::vector solve() const { std::vector I= scc.get_index(scc.get_block()); std::vector ret(sz); for (int i= 0; i < sz; i++) { if (I[i] == I[neg(i)]) return {}; // no solution ret[i]= I[i] > I[neg(i)]; } return ret; } }; using namespace std; namespace yosupo_scc { signed main() { cin.tie(0); ios::sync_with_stdio(0); int N, M; cin >> N >> M; StronglyConnectedComponents scc(N); for (int i= 0; i < M; ++i) { int a, b; cin >> a >> b; scc.add_edge(a, b); } auto ans= scc.get_block(); cout << ans.size() << '\n'; for (const auto &blk: ans) { cout << blk.size(); for (int v: blk) cout << " " << v; cout << '\n'; } return 0; } } namespace yukicoder1293 { // https://yukicoder.me/problems/no/1293 signed main() { cin.tie(0); ios::sync_with_stdio(0); int N, D, W; cin >> N >> D >> W; StronglyConnectedComponents scc(N + N); for (int i= 0; i < D; ++i) { int a, b; cin >> a >> b; --a, --b; scc.add_edge(a, b), scc.add_edge(b, a); } for (int i= 0; i < W; ++i) { int c, d; cin >> c >> d; c+= N - 1, d+= N - 1; scc.add_edge(c, d), scc.add_edge(d, c); } for (int i= 0; i < N; ++i) scc.add_edge(i, i + N); auto blk= scc.get_block(); int C= blk.size(); auto index= scc.get_index(blk); auto dag= scc.get_dag(index, C); long long dp[C]; fill_n(dp, C, 0); for (int i= 0; i < N; ++i) ++dp[index[i]]; for (int i= 0; i < C; ++i) for (int j: dag[i]) dp[j]+= dp[i]; long long ans= 0; for (int i= 0; i < N; ++i) ans+= dp[index[i + N]] - 1; cout << ans << '\n'; return 0; } } namespace yukicoder1813 { // https://yukicoder.me/problems/no/1813 signed main() { cin.tie(0); ios::sync_with_stdio(0); int N, M; cin >> N >> M; StronglyConnectedComponents scc(N); for (int i= 0; i < M; ++i) { int A, B; cin >> A >> B; scc.add_edge(--A, --B); } auto blk= scc.get_block(); int C= blk.size(); if (C == 1) { cout << 0 << '\n'; } else { auto dag= scc.get_dag(scc.get_index(blk), C); int cnt[2]= {0, 0}; bool st[C]; fill_n(st, C, true); for (int i= 0; i < C; ++i) { if (st[i]) ++cnt[0]; for (int j: dag[i]) st[j]= false; if (!dag[i].size()) ++cnt[1]; } cout << max(cnt[0], cnt[1]) << '\n'; } return 0; } } signed main() { cin.tie(0); ios::sync_with_stdio(0); // yosupo_scc::main(); // yukicoder1293::main(); yukicoder1813::main(); return 0; }