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
2 changes: 1 addition & 1 deletion .verify-helper/timestamps.remote.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
"tests/library_checker_aizu_tests/trees/hld_lib_checker_path.test.cpp": "2026-04-12 16:21:27 -0600",
"tests/library_checker_aizu_tests/trees/hld_lib_checker_subtree_edges.test.cpp": "2026-04-12 16:21:27 -0600",
"tests/library_checker_aizu_tests/trees/hld_lib_checker_subtree_nodes.test.cpp": "2026-04-12 16:21:27 -0600",
"tests/library_checker_aizu_tests/trees/kth_path_ladder.test.cpp": "2026-04-25 22:43:50 -0600",
"tests/library_checker_aizu_tests/trees/kth_path_ladder.test.cpp": "2026-04-26 11:11:53 -0600",
"tests/library_checker_aizu_tests/trees/kth_path_linear.test.cpp": "2026-04-21 13:56:58 -0600",
"tests/library_checker_aizu_tests/trees/kth_path_tree_lift.test.cpp": "2026-04-12 16:21:27 -0600",
"tests/library_checker_aizu_tests/trees/lca_all_methods_aizu.test.cpp": "2026-04-21 13:56:58 -0600",
Expand Down
23 changes: 12 additions & 11 deletions library/trees/uncommon/ladder_decomposition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,42 @@
//! @endcode
struct ladder {
int n;
vi d, p, idx, l;
vi d, p, idx, lad;
vector<vi> jmp;
ladder(const auto& g):
n(sz(g)), d(n), p(n), idx(n), l(2 * n) {
n(sz(g)), d(n), p(n), idx(n), lad(2 * n) {
int i = 0;
vi s(n);
vi st(n);
auto dfs = [&](auto dfs, int u) -> vi {
vi path;
s[d[u]] = u;
st[d[u]] = u;
for (int v : g[u])
if (v != p[u]) {
d[v] = d[p[v] = u] + 1;
vi x = dfs(dfs, v);
if (sz(x) > sz(path)) swap(x, path);
for (int y : x) idx[y] = i;
for (int y : x) l[i++] = y;
rep(j, 0, min(sz(x), d[v])) l[i++] = s[d[u] - j];
for (int y : x) lad[i++] = y;
rep(j, 0, min<int>(sz(x), d[v])) lad[i++] =
st[d[u] - j];
}
path.push_back(u);
return path;
};
vi x = dfs(dfs, 0);
for (int y : x) idx[y] = i;
for (int y : x) l[i++] = y;
for (int y : x) lad[i++] = y;
jmp = treeJump(p);
}
int kth_par(int u, int k) {
assert(0 <= k && k <= d[u]);
if (k == 0) return u;
int bit = __lg(k);
u = jmp[bit][u], k -= (1 << bit);
int i = idx[u], j = i + d[l[i]] - d[u];
assert(l[j] == u);
// subarray [j, j+k] of l corresponds to the rest
int i = idx[u], j = i + d[lad[i]] - d[u];
assert(lad[j] == u);
// subarray [j, j+k] of lad corresponds to the rest
// of the jump
return l[j + k];
return lad[j + k];
}
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#define PROBLEM \
"https://judge.yosupo.jp/problem/jump_on_tree"
#include "../template.hpp"
#undef sz
#define sz(x) (int)ssize(x)
#include "../../../library/trees/uncommon/ladder_decomposition.hpp"
int main() {
cin.tie(0)->sync_with_stdio(0);
Expand Down
Loading