From c851fa44d443752b91fdf9cda430985878b24be4 Mon Sep 17 00:00:00 2001 From: JooyoungKim Date: Sat, 26 Sep 2020 21:04:03 +0900 Subject: [PATCH 1/4] Kosaraju --- kosaraju.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 kosaraju.cpp diff --git a/kosaraju.cpp b/kosaraju.cpp new file mode 100644 index 0000000..d333993 --- /dev/null +++ b/kosaraju.cpp @@ -0,0 +1,81 @@ +#include +#define endl '\n' +using namespace std; + +typedef long long ll; +typedef pair pii; + +vector> edge, inverse_edge; +int v, e; + +void dfs(vector &check, stack &visit, int curr) +{ + check[curr] = true; + for (int next : edge[curr]) + { + if (check[next]) + continue; + dfs(check, visit, next); + } + visit.push(curr); +} + +set> kosaraju(void) +{ + vector check(v + 1, false); + stack visit; + for (int i = 1; i <= v; i++) + { + if (check[i]) + continue; + dfs(check, visit, i); + } + + check = vector(v + 1, false); + set> ret; + while (!visit.empty()) + { + int node = visit.top(); + visit.pop(); + if (check[node]) + continue; + + stack sk; + sk.push(node); + set se; + while (!sk.empty()) + { + int curr = sk.top(); + sk.pop(); + check[curr] = true; + se.insert(curr); + for (int next : inverse_edge[curr]) + { + if (check[next]) + continue; + sk.push(next); + } + } + ret.insert(se); + } + return ret; +} +int main(void) +{ + ios_base::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> v >> e; + edge.resize(v + 1); + inverse_edge.resize(v + 1); + for (int i = 0; i < e; i++) + { + int a, b; + cin >> a >> b; + edge[a].push_back(b); + inverse_edge[b].push_back(a); + } + set> scc = kosaraju(); + + return 0; +} \ No newline at end of file From 7d4f6c25670a6fc0e300a4952be8944441a13a0d Mon Sep 17 00:00:00 2001 From: Jaemin Choi <1dotolee@gmail.com> Date: Thu, 8 Oct 2020 22:20:03 +0900 Subject: [PATCH 2/4] Reformat Kosaraju --- kosaraju.cpp | 88 +++++++++++++--------------------------------------- 1 file changed, 21 insertions(+), 67 deletions(-) diff --git a/kosaraju.cpp b/kosaraju.cpp index d333993..e1957cd 100644 --- a/kosaraju.cpp +++ b/kosaraju.cpp @@ -1,81 +1,35 @@ -#include -#define endl '\n' -using namespace std; - -typedef long long ll; -typedef pair pii; - -vector> edge, inverse_edge; -int v, e; - -void dfs(vector &check, stack &visit, int curr) -{ +int n; +stack &st; +vector check; +vector> adj, adj_r; +void dfs(int curr) { check[curr] = true; - for (int next : edge[curr]) - { - if (check[next]) - continue; - dfs(check, visit, next); - } - visit.push(curr); + for (auto& next : adj[curr]) + if (!check[next]) dfs(next); + st.push(curr); } - -set> kosaraju(void) -{ - vector check(v + 1, false); - stack visit; - for (int i = 1; i <= v; i++) - { - if (check[i]) - continue; - dfs(check, visit, i); - } - - check = vector(v + 1, false); +set> kosaraju() { + check.resize(n); + for (int i = 0; i < n; i++) + if (!check[i]) dfs(i); + check = vector(n + 1); set> ret; - while (!visit.empty()) - { - int node = visit.top(); - visit.pop(); - if (check[node]) - continue; - + while (!st.empty()) { + int node = st.top(); + st.pop(); + if (check[node]) continue; + set se; stack sk; sk.push(node); - set se; - while (!sk.empty()) - { + while (!sk.empty()) { int curr = sk.top(); sk.pop(); check[curr] = true; se.insert(curr); - for (int next : inverse_edge[curr]) - { - if (check[next]) - continue; - sk.push(next); - } + for (auto next : adj_r[curr]) + if (!check[next]) sk.push(next); } ret.insert(se); } return ret; } -int main(void) -{ - ios_base::sync_with_stdio(false); - cin.tie(nullptr); - - cin >> v >> e; - edge.resize(v + 1); - inverse_edge.resize(v + 1); - for (int i = 0; i < e; i++) - { - int a, b; - cin >> a >> b; - edge[a].push_back(b); - inverse_edge[b].push_back(a); - } - set> scc = kosaraju(); - - return 0; -} \ No newline at end of file From b03f8b7faeb99e05f243b312a4e784b2a1f57dc8 Mon Sep 17 00:00:00 2001 From: Jaemin Choi <1dotolee@gmail.com> Date: Thu, 8 Oct 2020 22:28:22 +0900 Subject: [PATCH 3/4] Fix minor issues in Kosaraju --- kosaraju.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kosaraju.cpp b/kosaraju.cpp index e1957cd..2d39208 100644 --- a/kosaraju.cpp +++ b/kosaraju.cpp @@ -1,7 +1,8 @@ int n; -stack &st; +stack st; vector check; vector> adj, adj_r; + void dfs(int curr) { check[curr] = true; for (auto& next : adj[curr]) @@ -12,7 +13,7 @@ set> kosaraju() { check.resize(n); for (int i = 0; i < n; i++) if (!check[i]) dfs(i); - check = vector(n + 1); + check = vector(n); set> ret; while (!st.empty()) { int node = st.top(); From ee97e1c47feb19a7b0f72711fa6c6bdf35272f0d Mon Sep 17 00:00:00 2001 From: Jaemin Choi <1dotolee@gmail.com> Date: Thu, 8 Oct 2020 22:41:24 +0900 Subject: [PATCH 4/4] Don't use set --- kosaraju.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/kosaraju.cpp b/kosaraju.cpp index 2d39208..f741538 100644 --- a/kosaraju.cpp +++ b/kosaraju.cpp @@ -9,28 +9,29 @@ void dfs(int curr) { if (!check[next]) dfs(next); st.push(curr); } -set> kosaraju() { +vector> kosaraju() { check.resize(n); for (int i = 0; i < n; i++) if (!check[i]) dfs(i); check = vector(n); - set> ret; + vector> ret; + stack sk; + vector comp; while (!st.empty()) { int node = st.top(); st.pop(); if (check[node]) continue; - set se; - stack sk; sk.push(node); + comp.clear(); while (!sk.empty()) { int curr = sk.top(); sk.pop(); check[curr] = true; - se.insert(curr); + comp.push_back(curr); for (auto next : adj_r[curr]) if (!check[next]) sk.push(next); } - ret.insert(se); + ret.push_back(comp); } return ret; }