Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lrvideckis committed Nov 28, 2024
1 parent 6c68dc3 commit 1385bf2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
12 changes: 6 additions & 6 deletions library/graphs/functional_graph_processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
//! @code
//! // 0 <= a[i] < n
//! auto [t2, cycle] = func_graph(a);
//! int root = cycle[t2[v].i][t2[v].j];
//! auto [cyc_id, cyc_pos] = t2[v].root_of;
//! int root = cycle[cyc_id][cyc_pos];
//! bool is_on_cycle = (v == root);
//! @endcode
//! root = first reachable node on cycle
Expand All @@ -12,7 +13,7 @@
//! @space O(n)
struct func_graph {
struct node {
int i, j;
pii root_of;
vi childs;
};
vector<node> t;
Expand All @@ -29,17 +30,16 @@ struct func_graph {
if (state[u] == 1) {
cycle.emplace_back();
while (state[u] == 1) {
t[u].i = sz(cycle) - 1;
t[u].j = sz(cycle.back());
t[u].root_of = {
sz(cycle) - 1, sz(cycle.back())};
cycle.back().push_back(u);
state[u] = 2;
u = a[u];
}
}
int v = i;
while (state[v] == 1) {
t[v].i = t[u].i;
t[v].j = t[u].j;
t[v].root_of = t[u].root_of;
t[a[v]].childs.push_back(v);
state[v] = 2;
v = a[v];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,22 @@ int main() {
functional_graph_processor fgp(a);
assert(cycle == fgp.cycle);
for (int i = 0; i < n; i++) {
int root = cycle[t[i].i][t[i].j];
int root =
cycle[t[i].root_of.first][t[i].root_of.second];
assert(t[i].childs == fgp.abr[i]);
assert((root == i) == (fgp.cycle_id[i] != -1));
if (root == i) {
assert(t[i].i == fgp.cycle_id[i]);
assert(t[i].j == fgp.cycle_pos[i]);
int cyc_len = ssize(cycle[t[i].i]);
assert(t[i].root_of.first == fgp.cycle_id[i]);
assert(t[i].root_of.second == fgp.cycle_pos[i]);
int cyc_len = ssize(cycle[t[i].root_of.first]);
assert(
cycle[t[i].i][(t[i].j + 1) % cyc_len] == a[i]);
cycle[t[i].root_of.first]
[(t[i].root_of.second + 1) % cyc_len] ==
a[i]);
assert(fgp.cycle_prev[i] ==
cycle[t[i].i][(t[i].j - 1 + cyc_len) % cyc_len]);
cycle[t[i].root_of.first]
[(t[i].root_of.second - 1 + cyc_len) %
cyc_len]);
} else {
assert(fgp.cycle_prev[i] == -1);
}
Expand Down

0 comments on commit 1385bf2

Please sign in to comment.