Skip to content

Commit 3de5fbe

Browse files
lrvideckisweb-flow
andauthored
Func graph nit (#133)
* changes * [auto-verifier] verify commit 1dcf679 * formatted now * [auto-verifier] verify commit 9df472c * update comment * [auto-verifier] verify commit 58663a4 --------- Co-authored-by: GitHub <noreply@github.com>
1 parent 19596e6 commit 3de5fbe

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

.verify-helper/timestamps.remote.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"tests/library_checker_aizu_tests/handmade_tests/dsu_size.test.cpp": "2024-12-14 19:50:29 -0600",
6868
"tests/library_checker_aizu_tests/handmade_tests/edge_cd_small_trees.test.cpp": "2025-04-27 21:47:37 -0600",
6969
"tests/library_checker_aizu_tests/handmade_tests/fib_matrix_expo.test.cpp": "2024-12-14 19:50:29 -0600",
70-
"tests/library_checker_aizu_tests/handmade_tests/functional_graph.test.cpp": "2025-04-22 21:37:22 -0500",
70+
"tests/library_checker_aizu_tests/handmade_tests/functional_graph.test.cpp": "2025-05-31 11:14:03 -0600",
7171
"tests/library_checker_aizu_tests/handmade_tests/lca_ladder_forest.test.cpp": "2025-02-10 23:30:47 -0700",
7272
"tests/library_checker_aizu_tests/handmade_tests/manacher.test.cpp": "2024-12-14 19:50:29 -0600",
7373
"tests/library_checker_aizu_tests/handmade_tests/merge_st_and_wavelet.test.cpp": "2025-02-11 13:53:30 -0700",

library/graphs/functional_graph_processor.hpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
//! https://github.com/Aeren1564/Algorithms/blob/master/Algorithm_Implementations_Cpp/Graph_Theory/Special_Graphs/functional_graph_processor.sublime-snippet
33
//! @code
44
//! // 0 <= a[i] < n
5-
//! auto [t, cycle] = func_graph(a);
6-
//! auto [cyc_id, cyc_pos] = t[v].root_of;
5+
//! auto [root_of, cycle, childs] = func_graph(a);
6+
//! auto [cyc_id, cyc_pos] = root_of[v];
77
//! int root = cycle[cyc_id][cyc_pos];
88
//! bool is_on_cycle = (v == root);
99
//! @endcode
@@ -12,13 +12,9 @@
1212
//! @time O(n)
1313
//! @space O(n)
1414
struct func_graph {
15-
struct node {
16-
pii root_of;
17-
basic_string<int> childs;
18-
};
19-
vector<node> t;
20-
vector<vi> cycle;
21-
func_graph(const vi& a): t(sz(a)) {
15+
vector<pii> root_of;
16+
vector<basic_string<int>> cycle, childs;
17+
func_graph(const vi& a): root_of(sz(a)), childs(sz(a)) {
2218
vi state(sz(a));
2319
rep(i, 0, sz(a)) {
2420
if (state[i] == 0) {
@@ -30,17 +26,16 @@ struct func_graph {
3026
if (state[u] == 1) {
3127
cycle.emplace_back();
3228
while (state[u] == 1) {
33-
t[u].root_of = {
34-
sz(cycle) - 1, sz(cycle.back())};
35-
cycle.back().push_back(u);
29+
root_of[u] = {sz(cycle) - 1, sz(cycle.back())};
30+
cycle.back() += u;
3631
state[u] = 2;
3732
u = a[u];
3833
}
3934
}
4035
int v = i;
4136
while (state[v] == 1) {
42-
t[v].root_of = t[u].root_of;
43-
t[a[v]].childs += v;
37+
root_of[v] = root_of[u];
38+
childs[a[v]] += v;
4439
state[v] = 2;
4540
v = a[v];
4641
}

tests/library_checker_aizu_tests/handmade_tests/functional_graph.test.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,26 +131,30 @@ int main() {
131131
int n = rnd(1, 1000);
132132
vector<int> a(n);
133133
for (int i = 0; i < n; i++) a[i] = rnd(0, n - 1);
134-
auto [t, cycle] = func_graph(a);
134+
auto [root_of, cycle, childs] = func_graph(a);
135135
functional_graph_processor fgp(a);
136-
assert(cycle == fgp.cycle);
136+
assert(sz(cycle) == sz(fgp.cycle));
137+
for (int i = 0; i < sz(cycle); i++) {
138+
assert(sz(cycle[i]) == sz(fgp.cycle[i]));
139+
for (int j = 0; j < sz(cycle[i]); j++)
140+
assert(cycle[i][j] == fgp.cycle[i][j]);
141+
}
137142
for (int i = 0; i < n; i++) {
138143
int root =
139-
cycle[t[i].root_of.first][t[i].root_of.second];
144+
cycle[root_of[i].first][root_of[i].second];
140145
assert(root == fgp.root_of[i]);
141-
assert(equal(t[i].childs, fgp.abr[i]));
146+
assert(equal(childs[i], fgp.abr[i]));
142147
assert((root == i) == (fgp.cycle_id[i] != -1));
143148
if (root == i) {
144-
assert(t[i].root_of.first == fgp.cycle_id[i]);
145-
assert(t[i].root_of.second == fgp.cycle_pos[i]);
146-
int cyc_len = ssize(cycle[t[i].root_of.first]);
147-
assert(
148-
cycle[t[i].root_of.first]
149-
[(t[i].root_of.second + 1) % cyc_len] ==
149+
assert(root_of[i].first == fgp.cycle_id[i]);
150+
assert(root_of[i].second == fgp.cycle_pos[i]);
151+
int cyc_len = ssize(cycle[root_of[i].first]);
152+
assert(cycle[root_of[i].first]
153+
[(root_of[i].second + 1) % cyc_len] ==
150154
a[i]);
151155
assert(fgp.cycle_prev[i] ==
152-
cycle[t[i].root_of.first]
153-
[(t[i].root_of.second - 1 + cyc_len) %
156+
cycle[root_of[i].first]
157+
[(root_of[i].second - 1 + cyc_len) %
154158
cyc_len]);
155159
} else {
156160
assert(fgp.cycle_prev[i] == -1);

0 commit comments

Comments
 (0)