Skip to content

Commit bc0a0b7

Browse files
committed
seeing if this is enough
1 parent 7c9ce51 commit bc0a0b7

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

library/trees/centroid_decomp_uncommon/count_paths_per_length.hpp

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,36 @@
77
//! @time O(n log^2 n)
88
//! @space this function allocates/returns various vectors
99
//! which are each O(n)
10-
vector<ll> count_paths_per_length(const vector<vi>& adj) {
10+
vector<ll> count_paths_per_length(const auto& adj) {
1111
vector<ll> num_paths(sz(adj));
12-
centroid(adj,
13-
[&](const vector<vi>& cd_adj, int cent, int) {
14-
vector<vector<double>> child_depths;
15-
for (int v : cd_adj[cent]) {
16-
child_depths.emplace_back(1, 0.0);
17-
for (queue<pii> q({{v, cent}}); !empty(q);) {
18-
child_depths.back().push_back(sz(q));
19-
queue<pii> new_q;
20-
while (!empty(q)) {
21-
auto [u, p] = q.front();
22-
q.pop();
23-
for (int w : cd_adj[u]) {
24-
if (w == p) continue;
25-
new_q.emplace(w, u);
26-
}
12+
centroid(adj, [&](const auto& cd_adj, int cent, int) {
13+
vector<vector<double>> child_depths;
14+
for (int v : cd_adj[cent]) {
15+
child_depths.emplace_back(1, 0.0);
16+
for (queue<pii> q({{v, cent}}); !empty(q);) {
17+
child_depths.back().push_back(sz(q));
18+
queue<pii> new_q;
19+
while (!empty(q)) {
20+
auto [u, p] = q.front();
21+
q.pop();
22+
for (int w : cd_adj[u]) {
23+
if (w == p) continue;
24+
new_q.emplace(w, u);
2725
}
28-
swap(q, new_q);
2926
}
27+
swap(q, new_q);
3028
}
31-
ranges::sort(child_depths, {},
32-
[&](auto& x) { return sz(x); });
33-
vector total_depth(1, 1.0);
34-
for (auto& cnt_depth : child_depths) {
35-
auto prod = conv(total_depth, cnt_depth);
36-
rep(i, 1, sz(prod)) num_paths[i] +=
37-
llround(prod[i]);
38-
total_depth.resize(sz(cnt_depth));
39-
rep(i, 1, sz(cnt_depth)) total_depth[i] +=
40-
cnt_depth[i];
41-
}
42-
});
29+
}
30+
ranges::sort(child_depths, {},
31+
[&](auto& x) { return sz(x); });
32+
vector total_depth(1, 1.0);
33+
for (auto& cnt_depth : child_depths) {
34+
auto prod = conv(total_depth, cnt_depth);
35+
rep(i, 1, sz(prod)) num_paths[i] += llround(prod[i]);
36+
total_depth.resize(sz(cnt_depth));
37+
rep(i, 1, sz(cnt_depth)) total_depth[i] +=
38+
cnt_depth[i];
39+
}
40+
});
4341
return num_paths;
4442
}

tests/library_checker_aizu_tests/trees/count_paths_per_length.test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ int main() {
66
cin.tie(0)->sync_with_stdio(0);
77
int n;
88
cin >> n;
9-
vector<vector<int>> adj(n);
9+
vector<basic_string<int>> adj(n);
1010
for (int i = 0; i < n - 1; i++) {
1111
int u, v;
1212
cin >> u >> v;

0 commit comments

Comments
 (0)