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
4 changes: 2 additions & 2 deletions .verify-helper/timestamps.remote.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"tests/library_checker_aizu_tests/graphs/strongly_connected_components_aizu.test.cpp": "2026-04-12 16:21:27 -0600",
"tests/library_checker_aizu_tests/graphs/strongly_connected_components_lib_checker.test.cpp": "2026-04-12 16:21:27 -0600",
"tests/library_checker_aizu_tests/graphs/two_edge_components.test.cpp": "2026-04-12 16:21:27 -0600",
"tests/library_checker_aizu_tests/handmade_tests/count_paths.test.cpp": "2026-04-12 16:21:27 -0600",
"tests/library_checker_aizu_tests/handmade_tests/count_paths.test.cpp": "2026-04-26 01:58:54 -0600",
"tests/library_checker_aizu_tests/handmade_tests/dsu.test.cpp": "2026-01-22 10:08:22 -0700",
"tests/library_checker_aizu_tests/handmade_tests/edge_cd_small_trees.test.cpp": "2026-04-12 16:21:27 -0600",
"tests/library_checker_aizu_tests/handmade_tests/fib_matrix_expo.test.cpp": "2026-01-28 21:48:16 -0700",
Expand Down Expand Up @@ -131,7 +131,7 @@
"tests/library_checker_aizu_tests/strings/suffix_array_short.test.cpp": "2026-04-12 06:15:25 -0600",
"tests/library_checker_aizu_tests/strings/trie.test.cpp": "2026-01-17 12:38:18 -0700",
"tests/library_checker_aizu_tests/strings/wildcard_pattern_matching.test.cpp": "2025-08-05 19:19:23 -0600",
"tests/library_checker_aizu_tests/trees/cd_count_paths_per_length.test.cpp": "2026-04-12 17:51:15 -0400",
"tests/library_checker_aizu_tests/trees/cd_count_paths_per_length.test.cpp": "2026-04-26 01:58:54 -0600",
"tests/library_checker_aizu_tests/trees/edge_cd_contour_range_query.test.cpp": "2026-04-12 16:21:27 -0600",
"tests/library_checker_aizu_tests/trees/edge_cd_contour_range_update.test.cpp": "2026-04-12 16:21:27 -0600",
"tests/library_checker_aizu_tests/trees/edge_cd_count_paths_per_length.test.cpp": "2026-04-12 16:21:27 -0600",
Expand Down
22 changes: 11 additions & 11 deletions library/trees/centroid_decomp.hpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
#pragma once
//! @code
//! vector<basic_string<int>> g(n);
//! centroid(g, [&](int cent, int par_cent) {});
//! vi par = cd(g, [&](int cent) {});
//! @endcode
//! @time O(n log n)
//! @space O(n)
void centroid(auto& g, auto f) {
vi siz(sz(g));
vi cd(auto& g, auto f) {
vi p(sz(g), -1), s(sz(g), sz(g));
auto ctd = [&](auto ctd, int u, int p, int n) -> int {
siz[u] = 1;
s[u] = 1;
for (int v : g[u])
if (v != p) {
if (int c = ctd(ctd, v, u, n); c != -1) return c;
siz[u] += siz[v];
s[u] += s[v];
}
return 2 * siz[u] >= n ? siz[p] = n - siz[u], u : -1;
return 2 * s[u] >= n ? s[p] = n - s[u], u : -1;
};
auto dfs = [&](auto dfs, int u, int p, int n) -> void {
f(u = ctd(ctd, u, u, n), p);
for (int v : g[u])
erase(g[v], u), dfs(dfs, v, u, siz[v]);
auto dfs = [&](auto dfs, int u) -> int {
f(u = ctd(ctd, u, u, s[u]));
for (int v : g[u]) erase(g[v], u), p[dfs(dfs, v)] = u;
return u;
};
dfs(dfs, 0, -1, sz(g));
return dfs(dfs, 0), p;
}
17 changes: 8 additions & 9 deletions tests/library_checker_aizu_tests/cd_asserts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@
void cd_asserts(vector<vector<int>> adj) {
vector<int> decomp_size(sz(adj), -1);
vector<int> naive_par_decomp(sz(adj), -1);
centroid(adj, [&](int cent, int par_cent) -> void {
assert(naive_par_decomp[cent] == par_cent);
vi par = cd(adj, [&](int cent) -> void {
assert(decomp_size[cent] == -1);
auto dfs = [&](auto&& self, int u, int p) -> int {
naive_par_decomp[u] = cent;
if (p != -1) naive_par_decomp[u] = cent;
int sub_size = 1;
for (int v : adj[u])
if (v != p) sub_size += self(self, v, u);
return sub_size;
};
decomp_size[cent] = dfs(dfs, cent, -1);
if (par_cent != -1)
assert(1 <= decomp_size[cent] &&
2 * decomp_size[cent] <= decomp_size[par_cent]);
for (int u : adj[cent]) {
int sz_subtree = dfs(dfs, u, cent);
assert(1 <= sz_subtree &&
2 * sz_subtree <= decomp_size[cent]);
assert(1 <= sz_subtree && 2 * sz_subtree <= decomp_size[cent]);
}
});
rep(i, 0, sz(adj)) assert(decomp_size[i] >= 1);
assert(par == naive_par_decomp);
rep(i, 0, sz(adj)) {
assert(decomp_size[i] >= 1);
if (par[i] != -1) assert(2 * decomp_size[i] <= decomp_size[par[i]]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! which are all O(n)
vector<ll> count_paths_per_node(vector<vi> adj, int k) {
vector<ll> num_paths(sz(adj));
centroid(adj, [&](int cent, int) {
cd(adj, [&](int cent) {
vector pre_d{1}, cur_d{0};
auto dfs = [&](auto&& self, int u, int p,
int d) -> ll {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! which are each O(n)
vector<ll> count_paths_per_length(vector<vi> adj) {
vector<ll> num_paths(sz(adj));
centroid(adj, [&](int cent, int) {
cd(adj, [&](int cent) {
vector<vector<double>> child_depths;
for (int v : adj[cent]) {
child_depths.emplace_back(1, 0.0);
Expand Down
Loading