From ab39eb6ea8c10201ac72b31416b8b95c16639b8a Mon Sep 17 00:00:00 2001 From: subcrip Date: Tue, 16 Apr 2024 18:41:34 +0800 Subject: [PATCH] Update graph/tarjan.cc Signed-off-by: subcrip --- graph/tarjan.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/graph/tarjan.cc b/graph/tarjan.cc index 3d3a844..715accd 100644 --- a/graph/tarjan.cc +++ b/graph/tarjan.cc @@ -1,4 +1,5 @@ namespace tarjan { + // Returns the mapping between vertices and their affiliated sccs. vector scc(const vector>& ch) { int n = ch.size(); int cnt = 0, scn = 0; @@ -25,6 +26,7 @@ namespace tarjan { return br; } + // This method can eliminate redundant edges or self-loops vector> build_scc(const vector>& ch) { int n = ch.size(); auto br = scc(ch); @@ -32,7 +34,7 @@ namespace tarjan { vector> rb(cnt + 1); for (int i = 0; i < n; ++i) { for (auto&& u : ch[i]) { - rb[br[i]].emplace(br[u]); + if (br[i] != br[u]) rb[br[i]].emplace(br[u]); } } vector> res(cnt + 1); @@ -42,6 +44,8 @@ namespace tarjan { return res; } + // This method can eliminate redundant edges or self-loops + // return form: (scc size, children of scc) vector>> build_scc_with_size(const vector>& ch) { int n = ch.size(); auto br = scc(ch); @@ -49,7 +53,7 @@ namespace tarjan { vector> rb(cnt + 1); for (int i = 0; i < n; ++i) { for (auto&& u : ch[i]) { - rb[br[i]].emplace(br[u]); + if (br[i] != br[u]) rb[br[i]].emplace(br[u]); } } vector>> res(cnt + 1);