Skip to content

Commit 194f736

Browse files
lrvideckisweb-flow
andauthored
Add test (#162)
* add another test * fix * [auto-verifier] verify commit c29f02d --------- Co-authored-by: GitHub <noreply@github.com>
1 parent 021ba44 commit 194f736

File tree

3 files changed

+96
-2
lines changed

3 files changed

+96
-2
lines changed

.verify-helper/timestamps.remote.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
"tests/library_checker_aizu_tests/trees/kth_path_tree_lift.test.cpp": "2025-08-14 10:27:46 -0600",
145145
"tests/library_checker_aizu_tests/trees/lca_all_methods_aizu.test.cpp": "2025-08-21 12:17:27 -0600",
146146
"tests/library_checker_aizu_tests/trees/lca_all_methods_lib_checker.test.cpp": "2025-08-21 12:17:27 -0600",
147-
"tests/library_checker_aizu_tests/trees/shallowest_aizu_tree_height.test.cpp": "2025-08-30 00:58:07 -0600",
147+
"tests/library_checker_aizu_tests/trees/shallowest_aizu_tree_height.test.cpp": "2025-09-03 10:28:33 -0600",
148+
"tests/library_checker_aizu_tests/trees/shallowest_lib_checker_tree_path_composite.test.cpp": "2025-09-03 10:28:33 -0600",
148149
"tests/library_checker_aizu_tests/trees/subtree_isomorphism.test.cpp": "2025-08-14 10:27:46 -0600"
149150
}

library/trees/shallowest_decomp_tree.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ void shallowest(auto& adj, auto f) {
2121
return dp;
2222
};
2323
dfs(dfs, 0, 0);
24-
for (vi vec : order | views::reverse)
24+
for (const vi& vec : order | views::reverse)
2525
for (int v : vec) {
2626
f(v);
2727
for (int u : adj[v])
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#define PROBLEM \
2+
"https://judge.yosupo.jp/problem/tree_path_composite_sum"
3+
#undef _GLIBCXX_DEBUG
4+
#include "../template.hpp"
5+
#include "../../../library/trees/shallowest_decomp_tree.hpp"
6+
#include "../../../library/math/mod_int.hpp"
7+
using line = array<mint, 2>;
8+
// returns f(g(x)) = f[0]*(g[0]*x+g[1]) + f[1]
9+
line compose(line f, line g) {
10+
return {f[0] * g[0], f[1] + f[0] * g[1]};
11+
}
12+
int main() {
13+
cin.tie(0)->sync_with_stdio(0);
14+
int n;
15+
cin >> n;
16+
vector<mint> a(n);
17+
for (int i = 0; i < n; i++) cin >> a[i].x;
18+
vector<vector<int>> adj(n);
19+
vector<vector<line>> weight(n);
20+
for (int i = 0; i < n - 1; i++) {
21+
int u, v, b, c;
22+
cin >> u >> v >> b >> c;
23+
adj[u].push_back(v);
24+
adj[v].push_back(u);
25+
weight[u].push_back({b, c});
26+
weight[v].push_back({b, c});
27+
}
28+
vector<mint> res(n);
29+
for (int i = 0; i < n; i++) res[i] = a[i];
30+
shallowest(adj, [&](int cent) {
31+
assert(ssize(adj[cent]) == ssize(weight[cent]));
32+
mint total_sum_evaluated = 0;
33+
int total_cnt_nodes = 0;
34+
mint curr_sum_evaluated = 0;
35+
int curr_cnt_nodes = 0;
36+
auto dfs = [&](auto&& self, int v, int p,
37+
line downwards, line upwards,
38+
bool forwards) -> void {
39+
// f(x) + f(y) + f(z) = b*x+c + b*y+c + b*z+c =
40+
// b*(x+y+z) + c*3
41+
res[v] = res[v] + upwards[0] * total_sum_evaluated +
42+
upwards[1] * total_cnt_nodes;
43+
if (forwards) {
44+
res[v] =
45+
res[v] + upwards[0] * a[cent] + upwards[1];
46+
res[cent] =
47+
res[cent] + downwards[0] * a[v] + downwards[1];
48+
}
49+
curr_cnt_nodes++;
50+
curr_sum_evaluated = curr_sum_evaluated +
51+
downwards[0] * a[v] + downwards[1];
52+
for (int i = 0; i < ssize(adj[v]); i++) {
53+
int u = adj[v][i];
54+
line curr_line = weight[v][i];
55+
if (u != p) {
56+
self(self, u, v, compose(downwards, curr_line),
57+
compose(curr_line, upwards), forwards);
58+
}
59+
}
60+
};
61+
for (int i = 0; i < ssize(adj[cent]); i++) {
62+
curr_sum_evaluated = 0;
63+
curr_cnt_nodes = 0;
64+
dfs(dfs, adj[cent][i], cent, weight[cent][i],
65+
weight[cent][i], 1);
66+
total_sum_evaluated =
67+
total_sum_evaluated + curr_sum_evaluated;
68+
total_cnt_nodes += curr_cnt_nodes;
69+
}
70+
total_sum_evaluated = 0;
71+
total_cnt_nodes = 0;
72+
for (int i = ssize(adj[cent]) - 1; i >= 0; i--) {
73+
curr_sum_evaluated = 0;
74+
curr_cnt_nodes = 0;
75+
dfs(dfs, adj[cent][i], cent, weight[cent][i],
76+
weight[cent][i], 0);
77+
total_sum_evaluated =
78+
total_sum_evaluated + curr_sum_evaluated;
79+
total_cnt_nodes += curr_cnt_nodes;
80+
}
81+
for (int v : adj[cent]) {
82+
for (int i = 0; i < ssize(adj[v]); i++) {
83+
if (adj[v][i] == cent) {
84+
swap(weight[v][i], weight[v].back());
85+
weight[v].pop_back();
86+
break;
87+
}
88+
}
89+
}
90+
});
91+
for (int i = 0; i < n; i++) cout << res[i].x << ' ';
92+
cout << '\n';
93+
}

0 commit comments

Comments
 (0)