Skip to content

Commit 1dcf679

Browse files
committed
changes
1 parent 19596e6 commit 1dcf679

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

library/graphs/functional_graph_processor.hpp

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,26 +131,31 @@ 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]);
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]);
147152
assert(
148-
cycle[t[i].root_of.first]
149-
[(t[i].root_of.second + 1) % cyc_len] ==
153+
cycle[root_of[i].first]
154+
[(root_of[i].second + 1) % cyc_len] ==
150155
a[i]);
151156
assert(fgp.cycle_prev[i] ==
152-
cycle[t[i].root_of.first]
153-
[(t[i].root_of.second - 1 + cyc_len) %
157+
cycle[root_of[i].first]
158+
[(root_of[i].second - 1 + cyc_len) %
154159
cyc_len]);
155160
} else {
156161
assert(fgp.cycle_prev[i] == -1);

0 commit comments

Comments
 (0)