|
7 | 7 | //! @time O(n log^2 n) |
8 | 8 | //! @space this function allocates/returns various vectors |
9 | 9 | //! 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) { |
11 | 11 | 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); |
27 | 25 | } |
28 | | - swap(q, new_q); |
29 | 26 | } |
| 27 | + swap(q, new_q); |
30 | 28 | } |
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 | + }); |
43 | 41 | return num_paths; |
44 | 42 | } |
0 commit comments