# Include <cstdio> # include <algorithm> # include <vector> # include <cstring> # include <stack> using namespace std; const int maxn = 1000 + 10; // In the figure, the node number starts from 0, and the scc starts from 1. vector <int> G [maxn]; int pre [maxn], lowlink [maxn], sccno [maxn], dfs_clock, scc_cnt; stack <int> S; void dfs (int u) {pre [u] = lowlink [u] = ++ dfs_clock; S. push (u); for (int I = 0; I <G [u]. size (); I ++) {int v = G [u] [I]; if (! Pre [v]) {dfs (v); lowlink [u] = min (lowlink [u], lowlink [v]);} else if (! Sccno [v]) {lowlink [u] = min (lowlink [u], pre [v]) ;}} if (lowlink [u] = pre [u]) {scc_cnt ++; for (;) {int x = S. top (); S. pop (); sccno [x] = scc_cnt; if (x = u) break ;}} void find_scc (int n) {dfs_clock = scc_cnt = 0; memset (sccno, 0, sizeof (sccno); memset (pre, 0, sizeof (pre); for (int I = 0; I <n; I ++) if (! Pre [I]) dfs (I );}