Skip to content

Commit

Permalink
Merge pull request #18 from skku-npc/user/Jooyoung
Browse files Browse the repository at this point in the history
Kosaraju
  • Loading branch information
illuminoplanet authored Oct 9, 2020
2 parents 77f349c + ee97e1c commit ebac1dc
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions kosaraju.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
int n;
stack<int> st;
vector<bool> check;
vector<vector<int>> adj, adj_r;

void dfs(int curr) {
check[curr] = true;
for (auto& next : adj[curr])
if (!check[next]) dfs(next);
st.push(curr);
}
vector<vector<int>> kosaraju() {
check.resize(n);
for (int i = 0; i < n; i++)
if (!check[i]) dfs(i);
check = vector<bool>(n);
vector<vector<int>> ret;
stack<int> sk;
vector<int> 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;
}

0 comments on commit ebac1dc

Please sign in to comment.