Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .verify-helper/timestamps.remote.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"tests/library_checker_aizu_tests/handmade_tests/dsu_size.test.cpp": "2024-09-22 18:42:16 -0500",
"tests/library_checker_aizu_tests/handmade_tests/edge_cd_small_trees.test.cpp": "2024-09-21 13:04:45 -0500",
"tests/library_checker_aizu_tests/handmade_tests/fib_matrix_expo.test.cpp": "2024-09-23 11:20:46 -0500",
"tests/library_checker_aizu_tests/handmade_tests/lca_ladder_forest.test.cpp": "2024-10-07 00:04:04 -0700",
"tests/library_checker_aizu_tests/handmade_tests/lca_ladder_forest.test.cpp": "2024-10-16 11:17:12 -0700",
"tests/library_checker_aizu_tests/handmade_tests/manacher.test.cpp": "2024-10-06 21:59:37 -0700",
"tests/library_checker_aizu_tests/handmade_tests/merge_sort_trees.test.cpp": "2024-09-22 18:42:16 -0500",
"tests/library_checker_aizu_tests/handmade_tests/mobius.test.cpp": "2024-09-19 12:57:45 -0700",
Expand Down Expand Up @@ -138,8 +138,8 @@
"tests/library_checker_aizu_tests/trees/edge_cd_contour_range_update.test.cpp": "2024-09-22 18:42:16 -0500",
"tests/library_checker_aizu_tests/trees/edge_cd_count_paths_per_length.test.cpp": "2024-09-21 13:04:45 -0500",
"tests/library_checker_aizu_tests/trees/edge_cd_reroot_dp.test.cpp": "2024-09-21 13:04:45 -0500",
"tests/library_checker_aizu_tests/trees/kth_node_on_path.test.cpp": "2024-10-07 00:04:04 -0700",
"tests/library_checker_aizu_tests/trees/ladder_decomposition.test.cpp": "2024-09-27 10:44:49 -0500",
"tests/library_checker_aizu_tests/trees/kth_node_on_path.test.cpp": "2024-10-16 11:17:12 -0700",
"tests/library_checker_aizu_tests/trees/ladder_decomposition.test.cpp": "2024-10-16 11:17:12 -0700",
"tests/library_checker_aizu_tests/trees/lca_all_methods_aizu.test.cpp": "2024-10-07 00:04:04 -0700",
"tests/library_checker_aizu_tests/trees/lca_all_methods_lib_checker.test.cpp": "2024-10-07 00:04:04 -0700",
"tests/library_checker_aizu_tests/trees/subtree_isomorphism.test.cpp": "2024-09-19 12:57:45 -0700"
Expand Down
22 changes: 12 additions & 10 deletions library/trees/ladder_decomposition/linear_kth_par.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@
//! int kth_par = kp.kth_par(v, k);
//! @endcode
//! kth_par = a node k edges up from v
//! @time O(3.5*n + q)
//! @space O(3.5*n)
//! @time O(n + q)
//! @space O(n)
struct linear_kth_par {
struct node {
int d, p = -1, idx, dl;
vi lad;
};
vector<node> t;
vi j;
vector<pii> j;
linear_kth_par(const vector<vi>& adj):
t(sz(adj)), j(2 * sz(t)) {
vi st;
int pos = 1;
auto add_j = [&]() -> void {
j[pos] = st[max(0, sz(st) - 1 - 2 * (pos & -pos))];
j[pos] = {st[max(0, sz(st) - 1 - 2 * (pos & -pos))],
st[max(0, sz(st) - 1 - 4 * (pos & -pos))]};
pos++;
};
auto dfs = [&](auto&& self, int v) -> void {
Expand All @@ -37,26 +38,27 @@ struct linear_kth_par {
}
st.pop_back();
};
rep(i, 0, sz(t)) if (t[i].p == -1) t[i].p = i,
dfs(dfs, i);
rep(i, 0, sz(t)) if (t[i].p == -1) dfs(dfs, i);
rep(i, 0, sz(t)) if (
t[i].p == i || t[t[i].p].dl != t[i].dl) {
t[i].p == -1 || t[t[i].p].dl != t[i].dl) {
int leaf = t[i].dl;
vi& lad = t[leaf].lad;
lad.resize(
min((t[leaf].d - t[i].d) * 7 / 2, t[leaf].d + 1),
min((t[leaf].d - t[i].d) * 2, t[leaf].d + 1),
leaf);
rep(k, 1, sz(lad)) lad[k] = t[lad[k - 1]].p;
}
}
int kth_par(int v, int k) {
assert(0 <= k && k <= t[v].d);
switch (k) {
case 0: return v;
case 1: return t[v].p;
case 2: return t[t[v].p].p;
default:
int i = 1 << __lg(k / 3),
leaf = t[j[(t[v].idx & -i) | i]].dl;
int i = 1 << __lg(k / 3);
auto [j1, j2] = j[(t[v].idx & -i) | i];
int leaf = t[t[v].d - t[j2].d <= k ? j2 : j1].dl;
return t[leaf].lad[k + t[leaf].d - t[v].d];
}
}
Expand Down