Skip to content

Commit f3269ae

Browse files
committed
tree dir reorg
1 parent b579b48 commit f3269ae

File tree

11 files changed

+253
-235
lines changed

11 files changed

+253
-235
lines changed
File renamed without changes.

library/trees/uncommon/contour_range_query.hpp

Lines changed: 0 additions & 62 deletions
This file was deleted.

library/trees/uncommon/contour_range_update.hpp

Lines changed: 0 additions & 59 deletions
This file was deleted.

library/trees/uncommon/count_paths_per_length.hpp

Lines changed: 0 additions & 29 deletions
This file was deleted.

library/trees/uncommon/count_paths_per_node.hpp

Lines changed: 0 additions & 43 deletions
This file was deleted.

library/trees/uncommon/sum_adjacent.hpp

Lines changed: 0 additions & 37 deletions
This file was deleted.

tests/library_checker_aizu_tests/handmade_tests/count_paths.test.cpp

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,48 @@
22
"https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ITP1_1_A"
33
#include "../template.hpp"
44
#include "../../../library/contest/random.hpp"
5-
#include "../../../library/trees/uncommon/count_paths_per_node.hpp"
5+
//! @param adj unrooted, connected forest
6+
//! @param k number of edges
7+
//! @returns array `num_paths` where `num_paths[i]` =
8+
//! number of paths with k edges where node `i` is on the
9+
//! path. 0-based nodes.
10+
//! @time O(n log n)
11+
//! @space this function allocates/returns various vectors
12+
//! which are all O(n)
13+
vector<ll> count_paths_per_node(const vector<vi>& adj,
14+
int k) {
15+
vector<ll> num_paths(sz(adj));
16+
centroid(adj,
17+
[&](const vector<vi>& cd_adj, int cent, int) {
18+
vector pre_d{1}, cur_d{0};
19+
auto dfs = [&](auto&& self, int u, int p,
20+
int d) -> ll {
21+
if (d > k) return 0LL;
22+
if (sz(cur_d) <= d) cur_d.push_back(0);
23+
cur_d[d]++;
24+
ll cnt = 0;
25+
if (k - d < sz(pre_d)) cnt += pre_d[k - d];
26+
for (int c : cd_adj[u])
27+
if (c != p) cnt += self(self, c, u, d + 1);
28+
num_paths[u] += cnt;
29+
return cnt;
30+
};
31+
auto dfs_child = [&](int child) -> ll {
32+
ll cnt = dfs(dfs, child, cent, 1);
33+
pre_d.resize(sz(cur_d));
34+
for (int i = 1; i < sz(cur_d) && cur_d[i]; i++)
35+
pre_d[i] += cur_d[i], cur_d[i] = 0;
36+
return cnt;
37+
};
38+
for (int child : cd_adj[cent])
39+
num_paths[cent] += dfs_child(child);
40+
pre_d = cur_d = {0};
41+
for (int child : cd_adj[cent] | views::reverse)
42+
dfs_child(child);
43+
});
44+
return num_paths;
45+
}
646
#include "../../../library/trees/lca_rmq.hpp"
7-
#include "../../../library/trees/uncommon/count_paths_per_length.hpp"
847
#include "../cd_asserts.hpp"
948
vector<vector<ll>> naive(const vector<vi>& adj) {
1049
LCA lc(adj);

tests/library_checker_aizu_tests/trees/count_paths_per_length.test.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,34 @@
11
#define PROBLEM \
22
"https://judge.yosupo.jp/problem/frequency_table_of_tree_distance"
33
#include "../template.hpp"
4-
#include "../../../library/trees/uncommon/count_paths_per_length.hpp"
4+
#include "../../../kactl/content/numerical/FastFourierTransform.h"
5+
#include "../../../library/trees/edge_cd.hpp"
6+
//! @param adj unrooted, connected tree
7+
//! @returns array `num_paths` where `num_paths[i]` = # of
8+
//! paths in tree with `i` edges. `num_paths[1]` = # edges
9+
//! @time O(n * logφ(n) * log2(n))
10+
//! @space this function allocates/returns various vectors
11+
//! which are each O(n)
12+
vector<ll> count_paths_per_length(const vector<vi>& adj) {
13+
vector<ll> num_paths(sz(adj));
14+
if (sz(adj) >= 2) num_paths[1] = sz(adj) - 1;
15+
edge_cd(adj,
16+
[&](const vector<vi>& cd_adj, int cent, int split) {
17+
vector<vector<double>> cnt(2, vector<double>(1));
18+
auto dfs = [&](auto&& self, int u, int p, int d,
19+
int side) -> void {
20+
if (sz(cnt[side]) == d) cnt[side].push_back(0.0);
21+
cnt[side][d]++;
22+
for (int c : cd_adj[u])
23+
if (c != p) self(self, c, u, 1 + d, side);
24+
};
25+
rep(i, 0, sz(cd_adj[cent]))
26+
dfs(dfs, cd_adj[cent][i], cent, 1, i < split);
27+
vector<double> prod = conv(cnt[0], cnt[1]);
28+
rep(i, 0, sz(prod)) num_paths[i] += llround(prod[i]);
29+
});
30+
return num_paths;
31+
}
532
int main() {
633
cin.tie(0)->sync_with_stdio(0);
734
int n;

0 commit comments

Comments
 (0)