Skip to content

Commit f5c359b

Browse files
lrvideckisweb-flow
andauthored
Another scc golf (#123)
* golf * [auto-verifier] verify commit 596f983 * golf --------- Co-authored-by: GitHub <noreply@github.com>
1 parent 2186709 commit f5c359b

File tree

4 files changed

+33
-45
lines changed

4 files changed

+33
-45
lines changed

.verify-helper/timestamps.remote.json

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,18 @@
4949
"tests/library_checker_aizu_tests/flow/dinic_lib_checker.test.cpp": "2024-11-17 14:04:03 -0600",
5050
"tests/library_checker_aizu_tests/flow/hungarian.test.cpp": "2024-11-17 14:04:03 -0600",
5151
"tests/library_checker_aizu_tests/flow/min_cost_max_flow.test.cpp": "2024-12-05 10:41:42 -0600",
52-
"tests/library_checker_aizu_tests/graphs/biconnected_components.test.cpp": "2024-12-14 19:50:29 -0600",
52+
"tests/library_checker_aizu_tests/graphs/biconnected_components.test.cpp": "2024-12-15 08:00:27 -0600",
5353
"tests/library_checker_aizu_tests/graphs/connected_components_of_complement_graph.test.cpp": "2024-12-14 19:50:29 -0600",
5454
"tests/library_checker_aizu_tests/graphs/dijkstra_aizu.test.cpp": "2024-12-14 19:50:29 -0600",
5555
"tests/library_checker_aizu_tests/graphs/dijkstra_lib_checker.test.cpp": "2024-12-14 19:50:29 -0600",
56-
"tests/library_checker_aizu_tests/graphs/directed_cycle.test.cpp": "2024-12-15 06:41:10 -0600",
5756
"tests/library_checker_aizu_tests/graphs/enumerate_triangles.test.cpp": "2024-12-14 19:50:29 -0600",
5857
"tests/library_checker_aizu_tests/graphs/hopcroft_karp_aizu.test.cpp": "2024-12-14 19:50:29 -0600",
5958
"tests/library_checker_aizu_tests/graphs/hopcroft_karp_lib_checker.test.cpp": "2024-12-14 19:50:29 -0600",
6059
"tests/library_checker_aizu_tests/graphs/mst.test.cpp": "2024-11-17 14:04:03 -0600",
61-
"tests/library_checker_aizu_tests/graphs/offline_incremental_scc.test.cpp": "2024-12-14 19:50:29 -0600",
62-
"tests/library_checker_aizu_tests/graphs/strongly_connected_components_aizu.test.cpp": "2024-12-14 19:50:29 -0600",
63-
"tests/library_checker_aizu_tests/graphs/strongly_connected_components_lib_checker.test.cpp": "2024-12-14 19:50:29 -0600",
64-
"tests/library_checker_aizu_tests/graphs/two_edge_components.test.cpp": "2024-12-14 19:50:29 -0600",
60+
"tests/library_checker_aizu_tests/graphs/offline_incremental_scc.test.cpp": "2024-12-15 08:00:27 -0600",
61+
"tests/library_checker_aizu_tests/graphs/strongly_connected_components_aizu.test.cpp": "2024-12-15 08:00:27 -0600",
62+
"tests/library_checker_aizu_tests/graphs/strongly_connected_components_lib_checker.test.cpp": "2024-12-15 08:00:27 -0600",
63+
"tests/library_checker_aizu_tests/graphs/two_edge_components.test.cpp": "2024-12-15 08:00:27 -0600",
6564
"tests/library_checker_aizu_tests/handmade_tests/count_paths_forest.test.cpp": "2024-12-14 19:50:29 -0600",
6665
"tests/library_checker_aizu_tests/handmade_tests/dsu_size.test.cpp": "2024-12-14 19:50:29 -0600",
6766
"tests/library_checker_aizu_tests/handmade_tests/dynamic_bitset.test.cpp": "2024-11-22 10:47:44 -0600",

library/graphs/bridges_cuts/bridges.hpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@
2020
//! @time O(n + m)
2121
//! @space O(n + m)
2222
auto bridges(const auto& adj, int m) {
23-
int n = sz(adj), num_ccs = 0, timer = 0;
24-
vi br_id(n, -1), is_br(m), tin(n), st;
25-
auto dfs = [&](auto&& self, int v, int p_id) -> int {
26-
int low = tin[v] = ++timer, siz = sz(st);
27-
st.push_back(v);
28-
for (auto [u, e_id] : adj[v])
29-
if (e_id != p_id && br_id[u] < 0)
30-
low = min(low, tin[u] ?: self(self, u, e_id));
23+
int n = sz(adj), num_ccs = 0, q = 0, s = 0;
24+
vi br_id(n, -1), is_br(m), tin(n), st(n);
25+
auto dfs = [&](auto&& self, int v, int p) -> int {
26+
int low = tin[v] = ++q;
27+
st[s++] = v;
28+
for (auto [u, e] : adj[v])
29+
if (e != p && br_id[u] < 0)
30+
low = min(low, tin[u] ?: self(self, u, e));
3131
if (tin[v] == low) {
32-
if (p_id != -1) is_br[p_id] = 1;
33-
rep(i, siz, sz(st)) br_id[st[i]] = num_ccs;
34-
st.resize(siz);
32+
if (p != -1) is_br[p] = 1;
33+
while (br_id[v] < 0) br_id[st[--s]] = num_ccs;
3534
num_ccs++;
3635
}
3736
return low;

library/graphs/bridges_cuts/cuts.hpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,22 @@
2121
//! @time O(n + m)
2222
//! @space O(n + m)
2323
auto cuts(const auto& adj, int m) {
24-
int n = sz(adj), num_bccs = 0, timer = 0;
25-
vi bcc_id(m, -1), is_cut(n), tin(n), st;
26-
auto dfs = [&](auto&& self, int v, int p_id) -> int {
27-
int low = tin[v] = ++timer, deg = 0;
28-
for (auto [u, e_id] : adj[v]) {
24+
int n = sz(adj), num_bccs = 0, q = 0, s = 0;
25+
vi bcc_id(m, -1), is_cut(n), tin(n), st(m);
26+
auto dfs = [&](auto&& self, int v, int p) -> int {
27+
int low = tin[v] = ++q;
28+
for (auto [u, e] : adj[v]) {
2929
assert(v != u);
30-
if (e_id == p_id) continue;
31-
if (!tin[u]) {
32-
int siz = sz(st);
33-
st.push_back(e_id);
34-
int low_ch = self(self, u, e_id);
35-
if (low_ch >= tin[v]) {
36-
is_cut[v] = 1;
37-
rep(i, siz, sz(st)) bcc_id[st[i]] = num_bccs;
38-
st.resize(siz);
39-
num_bccs++;
40-
}
41-
low = min(low, low_ch);
42-
deg++;
43-
} else if (tin[u] < tin[v]) {
44-
st.push_back(e_id);
45-
low = min(low, tin[u]);
30+
if (e == p) continue;
31+
if (tin[u] < tin[v]) st[s++] = e;
32+
int lu = -1;
33+
low = min(low, tin[u] ?: (lu = self(self, u, e)));
34+
if (lu >= tin[v]) {
35+
is_cut[v] = p >= 0 || tin[v] + 1 < tin[u];
36+
while (bcc_id[e] < 0) bcc_id[st[--s]] = num_bccs;
37+
num_bccs++;
4638
}
4739
}
48-
if (p_id == -1) is_cut[v] = (deg > 1);
4940
return low;
5041
};
5142
rep(i, 0, n) if (!tin[i]) dfs(dfs, i, -1);

library/graphs/strongly_connected_components/scc.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@
1313
//! @time O(n + m)
1414
//! @space O(n)
1515
auto sccs(const auto& adj) {
16-
int n = sz(adj), num_sccs = 0, timer = 0;
17-
vi scc_id(n, -1), tin(n), st;
16+
int n = sz(adj), num_sccs = 0, q = 0, s = 0;
17+
vi scc_id(n, -1), tin(n), st(n);
1818
auto dfs = [&](auto&& self, int v) -> int {
19-
int low = tin[v] = ++timer, siz = sz(st);
20-
st.push_back(v);
19+
int low = tin[v] = ++q;
20+
st[s++] = v;
2121
for (int u : adj[v])
2222
if (scc_id[u] < 0)
2323
low = min(low, tin[u] ?: self(self, u));
2424
if (tin[v] == low) {
25-
rep(i, siz, sz(st)) scc_id[st[i]] = num_sccs;
26-
st.resize(siz);
25+
while (scc_id[v] < 0) scc_id[st[--s]] = num_sccs;
2726
num_sccs++;
2827
}
2928
return low;

0 commit comments

Comments
 (0)