Skip to content

Commit c0fa4a0

Browse files
committed
Revert "Ladder optimization (#83)"
This reverts commit a9ee2fa.
1 parent 82341e1 commit c0fa4a0

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

.verify-helper/timestamps.remote.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
"tests/library_checker_aizu_tests/handmade_tests/dsu_size.test.cpp": "2024-09-22 18:42:16 -0500",
7171
"tests/library_checker_aizu_tests/handmade_tests/edge_cd_small_trees.test.cpp": "2024-09-21 13:04:45 -0500",
7272
"tests/library_checker_aizu_tests/handmade_tests/fib_matrix_expo.test.cpp": "2024-09-23 11:20:46 -0500",
73-
"tests/library_checker_aizu_tests/handmade_tests/lca_ladder_forest.test.cpp": "2024-10-17 18:24:12 -0700",
7473
"tests/library_checker_aizu_tests/handmade_tests/manacher.test.cpp": "2024-10-06 21:59:37 -0700",
7574
"tests/library_checker_aizu_tests/handmade_tests/merge_sort_trees.test.cpp": "2024-09-22 18:42:16 -0500",
7675
"tests/library_checker_aizu_tests/handmade_tests/mobius.test.cpp": "2024-09-19 12:57:45 -0700",
@@ -138,8 +137,6 @@
138137
"tests/library_checker_aizu_tests/trees/edge_cd_contour_range_update.test.cpp": "2024-09-22 18:42:16 -0500",
139138
"tests/library_checker_aizu_tests/trees/edge_cd_count_paths_per_length.test.cpp": "2024-09-21 13:04:45 -0500",
140139
"tests/library_checker_aizu_tests/trees/edge_cd_reroot_dp.test.cpp": "2024-09-21 13:04:45 -0500",
141-
"tests/library_checker_aizu_tests/trees/kth_node_on_path.test.cpp": "2024-10-17 18:24:12 -0700",
142-
"tests/library_checker_aizu_tests/trees/ladder_decomposition.test.cpp": "2024-10-17 18:24:12 -0700",
143140
"tests/library_checker_aizu_tests/trees/lca_all_methods_aizu.test.cpp": "2024-10-07 00:04:04 -0700",
144141
"tests/library_checker_aizu_tests/trees/lca_all_methods_lib_checker.test.cpp": "2024-10-07 00:04:04 -0700",
145142
"tests/library_checker_aizu_tests/trees/subtree_isomorphism.test.cpp": "2024-09-19 12:57:45 -0700"

library/trees/ladder_decomposition/ladder_decomposition.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
//! @endcode
1111
struct ladder {
1212
int n;
13-
vector<vi> b_tbl;
14-
vi d, p, dl, idx_l, l_tbl;
13+
vector<vi> b_tbl, l_tbl;
14+
vi d, p, dl; //!< deepest leaf
1515
//! @param adj forest (rooted or unrooted)
1616
//! @time O(n log n)
1717
//! @space O(n log n) for b_tbl. Everything else is O(n)
1818
ladder(const vector<vi>& adj):
19-
n(sz(adj)), d(n), p(n, -1), dl(n), idx_l(n) {
19+
n(sz(adj)), l_tbl(n), d(n), p(n, -1), dl(n) {
2020
auto dfs = [&](auto&& self, int v) -> void {
2121
dl[v] = v;
2222
for (int u : adj[v])
@@ -29,10 +29,11 @@ struct ladder {
2929
rep(i, 0, n) {
3030
if (p[i] == -1) p[i] = i, dfs(dfs, i);
3131
if (p[i] == i || dl[p[i]] != dl[i]) {
32-
int v = dl[i], len = (d[v] - d[i]) * 2;
33-
idx_l[v] = sz(l_tbl) + d[v];
34-
for (; v != -1 && len--; v = p[v])
35-
l_tbl.push_back(v);
32+
int leaf = dl[i];
33+
vi& lad = l_tbl[leaf];
34+
lad.resize(min(2 * (d[leaf] - d[i]), d[leaf] + 1),
35+
leaf);
36+
rep(j, 1, sz(lad)) lad[j] = p[lad[j - 1]];
3637
}
3738
}
3839
b_tbl = treeJump(p);
@@ -49,6 +50,6 @@ struct ladder {
4950
int bit = __lg(k);
5051
v = b_tbl[bit][v], k -= (1 << bit);
5152
int leaf = dl[v];
52-
return l_tbl[idx_l[leaf] + k - d[v]];
53+
return l_tbl[leaf][k + d[leaf] - d[v]];
5354
}
5455
};

library/trees/ladder_decomposition/linear_kth_par.hpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
//! @space O(n)
1010
struct linear_kth_par {
1111
struct node {
12-
int d, p = -1, dl, idx_j, idx_l;
12+
int d, p = -1, idx, dl;
13+
vi lad;
1314
};
1415
vector<node> t;
1516
vector<pii> j;
16-
vi l_tbl;
1717
linear_kth_par(const vector<vi>& adj):
1818
t(sz(adj)), j(2 * sz(t)) {
1919
vi st;
@@ -25,7 +25,7 @@ struct linear_kth_par {
2525
};
2626
auto dfs = [&](auto&& self, int v) -> void {
2727
st.push_back(v);
28-
t[v].idx_j = pos, t[v].dl = v;
28+
t[v].idx = pos, t[v].dl = v;
2929
add_j();
3030
for (int u : adj[v])
3131
if (u != t[v].p) {
@@ -40,10 +40,12 @@ struct linear_kth_par {
4040
rep(i, 0, sz(t)) {
4141
if (t[i].p == -1) dfs(dfs, i);
4242
if (t[i].p == -1 || t[t[i].p].dl != t[i].dl) {
43-
int v = t[i].dl, len = (t[v].d - t[i].d) * 2;
44-
t[v].idx_l = sz(l_tbl) + t[v].d;
45-
for (; v != -1 && len--; v = t[v].p)
46-
l_tbl.push_back(v);
43+
int leaf = t[i].dl;
44+
vi& lad = t[leaf].lad;
45+
lad.resize(
46+
min((t[leaf].d - t[i].d) * 2, t[leaf].d + 1),
47+
leaf);
48+
rep(k, 1, sz(lad)) lad[k] = t[lad[k - 1]].p;
4749
}
4850
}
4951
}
@@ -55,9 +57,9 @@ struct linear_kth_par {
5557
case 2: return t[t[v].p].p;
5658
default:
5759
int i = 1 << __lg(k / 3);
58-
auto [j1, j2] = j[(t[v].idx_j & -i) | i];
59-
int up = t[v].d - t[j2].d <= k ? j2 : j1;
60-
return l_tbl[t[t[up].dl].idx_l + k - t[v].d];
60+
auto [j1, j2] = j[(t[v].idx & -i) | i];
61+
int leaf = t[t[v].d - t[j2].d <= k ? j2 : j1].dl;
62+
return t[leaf].lad[k + t[leaf].d - t[v].d];
6163
}
6264
}
6365
};

0 commit comments

Comments
 (0)