diff --git a/kosaraju.cpp b/kosaraju.cpp new file mode 100644 index 0000000..f741538 --- /dev/null +++ b/kosaraju.cpp @@ -0,0 +1,37 @@ +int n; +stack st; +vector check; +vector> adj, adj_r; + +void dfs(int curr) { + check[curr] = true; + for (auto& next : adj[curr]) + if (!check[next]) dfs(next); + st.push(curr); +} +vector> kosaraju() { + check.resize(n); + for (int i = 0; i < n; i++) + if (!check[i]) dfs(i); + check = vector(n); + vector> ret; + stack sk; + vector comp; + while (!st.empty()) { + int node = st.top(); + st.pop(); + if (check[node]) continue; + sk.push(node); + comp.clear(); + while (!sk.empty()) { + int curr = sk.top(); + sk.pop(); + check[curr] = true; + comp.push_back(curr); + for (auto next : adj_r[curr]) + if (!check[next]) sk.push(next); + } + ret.push_back(comp); + } + return ret; +}