Skip to content

Commit 47030d9

Browse files
lrvideckisweb-flow
andauthored
Ladder nit (#213)
* update ladder decomp * add source too * trying this * [auto-verifier] verify commit ed2680f --------- Co-authored-by: GitHub <noreply@github.com>
1 parent 804ce52 commit 47030d9

3 files changed

Lines changed: 28 additions & 26 deletions

File tree

.verify-helper/timestamps.remote.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
"tests/library_checker_aizu_tests/trees/hld_lib_checker_path.test.cpp": "2026-04-12 16:21:27 -0600",
142142
"tests/library_checker_aizu_tests/trees/hld_lib_checker_subtree_edges.test.cpp": "2026-04-12 16:21:27 -0600",
143143
"tests/library_checker_aizu_tests/trees/hld_lib_checker_subtree_nodes.test.cpp": "2026-04-12 16:21:27 -0600",
144-
"tests/library_checker_aizu_tests/trees/kth_path_ladder.test.cpp": "2026-04-15 11:43:31 -0600",
144+
"tests/library_checker_aizu_tests/trees/kth_path_ladder.test.cpp": "2026-04-25 21:16:20 -0600",
145145
"tests/library_checker_aizu_tests/trees/kth_path_linear.test.cpp": "2026-04-21 13:56:58 -0600",
146146
"tests/library_checker_aizu_tests/trees/kth_path_tree_lift.test.cpp": "2026-04-12 16:21:27 -0600",
147147
"tests/library_checker_aizu_tests/trees/lca_all_methods_aizu.test.cpp": "2026-04-21 13:56:58 -0600",

library/trees/uncommon/ladder_decomposition.hpp

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,56 @@
22
#include "../../../kactl/content/graph/BinaryLifting.h"
33
//! https://codeforces.com/blog/entry/71567#comment-559299
44
//! https://youtu.be/0rCFkuQS968
5+
//! https://codeforces.com/blog/entry/153250
56
//! @code
67
//! ladder ld(g);
8+
//! ld.kth_par(u, k); // kth parent of u
9+
//! ld.kth_par(u, 0); // u
10+
//! ld.kth_par(u, 1); // p[u]
711
//! // KACTL functions
812
//! int kth_par = jmp(ld.jmp, u, k);
913
//! int curr_lca = lca(ld.jmp, ld.d, u, v);
1014
//! @endcode
1115
struct ladder {
1216
int n;
13-
vi d, p, leaf, idx, lad;
17+
vi d, p, idx, l;
1418
vector<vi> jmp;
1519
//! @param g forest (rooted or unrooted)
1620
//! @time O(n log n)
1721
//! @space O(n log n) for jmp. Everything else is O(n)
1822
ladder(const auto& g):
19-
n(sz(g)), d(n), p(n), leaf(n), idx(n), lad(2 * n) {
20-
auto dfs = [&](auto dfs, int u) -> void {
21-
leaf[u] = u;
23+
n(sz(g)), d(n), p(n), idx(n), l(2 * n) {
24+
int i = 0;
25+
vi s(n);
26+
auto dfs = [&](auto dfs, int u) -> vi {
27+
vi path;
28+
s[d[u]] = u;
2229
for (int v : g[u])
2330
if (v != p[u]) {
2431
d[v] = d[p[v] = u] + 1;
25-
dfs(dfs, v);
26-
if (d[leaf[u]] < d[leaf[v]]) leaf[u] = leaf[v];
32+
vi x = dfs(dfs, v);
33+
if (sz(x) > sz(path)) swap(x, path);
34+
for (int y : x) idx[y] = i;
35+
for (int y : x) l[i++] = y;
36+
rep(j, 0, min(sz(x), d[v])) l[i++] = s[d[u] - j];
2737
}
38+
path.push_back(u);
39+
return path;
2840
};
29-
dfs(dfs, 0);
30-
int pos = 0;
31-
rep(i, 0, n) if (p[i] == i || leaf[p[i]] != leaf[i]) {
32-
int l = leaf[i];
33-
int len = min((d[l] - d[i]) * 2, d[l] + 1);
34-
idx[l] = pos;
35-
for (; len--; l = p[l]) lad[pos++] = l;
36-
}
41+
vi x = dfs(dfs, 0);
42+
for (int y : x) idx[y] = i;
43+
for (int y : x) l[i++] = y;
3744
jmp = treeJump(p);
3845
}
39-
//! @param u query node
40-
//! @param k number of edges
41-
//! @returns a node k edges up from u. With k=1, this
42-
//! returns u's parent.
43-
//! @time O(1)
44-
//! @space O(1)
4546
int kth_par(int u, int k) {
4647
assert(0 <= k && k <= d[u]);
4748
if (k == 0) return u;
4849
int bit = __lg(k);
4950
u = jmp[bit][u], k -= (1 << bit);
50-
int l = idx[leaf[u]] + d[leaf[u]] - d[u];
51-
assert(lad[l] == u);
52-
// subarray [l, l+k] of lad corresponds to the rest
51+
int i = idx[u], j = i + d[l[i]] - d[u];
52+
assert(l[j] == u);
53+
// subarray [j, j+k] of l corresponds to the rest
5354
// of the jump
54-
return lad[l + k];
55+
return l[j + k];
5556
}
5657
};

tests/library_checker_aizu_tests/trees/kth_path_ladder.test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#define PROBLEM \
22
"https://judge.yosupo.jp/problem/jump_on_tree"
33
#include "../template.hpp"
4+
#undef sz
5+
#define sz(x) (int)ssize(x)
46
#include "../../../library/trees/uncommon/ladder_decomposition.hpp"
5-
#include "../../../library/trees/uncommon/linear_kth_par.hpp"
67
int main() {
78
cin.tie(0)->sync_with_stdio(0);
89
int n, q;

0 commit comments

Comments
 (0)