// https://oj.uz/problem/view/JOI15_election_campaign
#include <bits/stdc++.h>
using namespace std;
namespace std {
template <int D, typename T>
struct Vec : public vector<Vec<D - 1, T>> {
static_assert(D >= 1, "Dimension must be positive");
template <typename... Args>
Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) {}
};
template <typename T>
struct Vec<1, T> : public vector<T> {
Vec(int n = 0, T val = T()) : std::vector<T>(n, val) {}
};
/* Example
Vec<4, int64_t> f(n, k, 2, 2); // = f[n][k][2][2];
Vec<2, int> adj(n); // graph
*/
template <class Fun>
class y_combinator_result {
Fun fun_;
public:
template <class T>
explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {}
template <class... Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template <class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
/* Example
auto fun = y_combinator([&](auto self, int x) -> void {
self(x + 1);
});
*/
} // namespace std
class segtree_t {
public:
segtree_t *left, *right;
int l, r, m, val, lazy;
segtree_t(int l, int r) : l(l), r(r), m(l + r >> 1), val(0), lazy(0) {
if (l == r) return;
left = new segtree_t(l, m);
right = new segtree_t(m + 1, r);
}
void Update(int s, int t, int x) {
if (r < s || l > t) return;
if (s <= l && r <= t) {
val += x;
lazy += x;
return;
}
Down();
left->Update(s, t, x);
right->Update(s, t, x);
}
int Get(int p) {
if (l == r) return val;
Down();
if (p <= m) return left->Get(p);
return right->Get(p);
}
void Down() {
left->lazy += lazy;
right->lazy += lazy;
right->val += lazy;
left->val += lazy;
lazy = 0;
}
};
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<vector<int>> adj(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].emplace_back(v);
adj[v].emplace_back(u);
}
vector<vector<int>> up(__lg(n) + 1, vector<int>(n));
vector<int> depth(n);
vector<int> tin(n), tout(n);
int timer = 0;
auto dfs = y_combinator([&](auto dfs, int u, int p) -> void {
tin[u] = timer++;
up[0][u] = p;
for (int i = 1; i <= __lg(n); i++) up[i][u] = up[i - 1][up[i - 1][u]];
for (int v : adj[u]) {
if (v == p) continue;
depth[v] = depth[u] + 1;
dfs(v, u);
}
tout[u] = timer;
});
dfs(0, 0);
auto lca = [&](int x, int y) {
if (depth[x] > depth[y]) swap(x, y);
for (int i = __lg(n); i >= 0; i--) {
if (depth[up[i][y]] >= depth[x]) y = up[i][y];
}
if (x == y) return x;
for (int i = __lg(n); i >= 0; i--) {
if (up[i][x] == up[i][y]) continue;
x = up[i][x], y = up[i][y];
}
return up[0][x];
};
int m;
cin >> m;
vector<int> x(m), y(m), c(m);
vector<vector<int>> path(n);
for (int i = 0; i < m; i++) cin >> x[i] >> y[i] >> c[i], x[i]--, y[i]--;
for (int i = 0; i < m; i++) path[lca(x[i], y[i])].emplace_back(i);
segtree_t *tree = new segtree_t(0, n);
vector<int> dp(n);
auto dfs_ans = y_combinator([&](auto dfs_ans, int u, int p) -> void {
int sum = 0;
for (int v : adj[u]) {
if (v == p) continue;
dfs_ans(v, u);
sum += dp[v];
tree->Update(tin[v], tout[v] - 1, -dp[v]);
}
tree->Update(tin[u], tin[u], sum);
dp[u] = sum;
for (int i : path[u]) {
int f = sum + c[i];
if (x[i] != u) f += tree->Get(tin[x[i]]);
if (y[i] != u) f += tree->Get(tin[y[i]]);
dp[u] = max(dp[u], f);
}
for (int v : adj[u]) {
if (v == p) continue;
tree->Update(tin[v], tout[v] - 1, sum);
}
});
dfs_ans(0, 0);
cout << *max_element(dp.begin(), dp.end());
}
Compilation message
election_campaign.cpp: In constructor 'segtree_t::segtree_t(int, int)':
election_campaign.cpp:57:51: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
57 | segtree_t(int l, int r) : l(l), r(r), m(l + r >> 1), val(0), lazy(0) {
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
150 ms |
27392 KB |
Output is correct |
6 |
Correct |
101 ms |
37940 KB |
Output is correct |
7 |
Correct |
130 ms |
33996 KB |
Output is correct |
8 |
Correct |
99 ms |
27584 KB |
Output is correct |
9 |
Correct |
174 ms |
31948 KB |
Output is correct |
10 |
Correct |
101 ms |
27584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
198 ms |
41440 KB |
Output is correct |
5 |
Correct |
175 ms |
41576 KB |
Output is correct |
6 |
Correct |
165 ms |
41676 KB |
Output is correct |
7 |
Correct |
178 ms |
41672 KB |
Output is correct |
8 |
Correct |
169 ms |
41420 KB |
Output is correct |
9 |
Correct |
167 ms |
41492 KB |
Output is correct |
10 |
Correct |
167 ms |
41488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
198 ms |
41440 KB |
Output is correct |
5 |
Correct |
175 ms |
41576 KB |
Output is correct |
6 |
Correct |
165 ms |
41676 KB |
Output is correct |
7 |
Correct |
178 ms |
41672 KB |
Output is correct |
8 |
Correct |
169 ms |
41420 KB |
Output is correct |
9 |
Correct |
167 ms |
41492 KB |
Output is correct |
10 |
Correct |
167 ms |
41488 KB |
Output is correct |
11 |
Correct |
7 ms |
1296 KB |
Output is correct |
12 |
Correct |
171 ms |
41828 KB |
Output is correct |
13 |
Correct |
186 ms |
41672 KB |
Output is correct |
14 |
Correct |
190 ms |
41956 KB |
Output is correct |
15 |
Correct |
169 ms |
41860 KB |
Output is correct |
16 |
Correct |
167 ms |
41888 KB |
Output is correct |
17 |
Correct |
174 ms |
41676 KB |
Output is correct |
18 |
Correct |
170 ms |
41608 KB |
Output is correct |
19 |
Correct |
166 ms |
41648 KB |
Output is correct |
20 |
Correct |
169 ms |
41676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
230 ms |
30156 KB |
Output is correct |
2 |
Correct |
164 ms |
41416 KB |
Output is correct |
3 |
Correct |
262 ms |
37348 KB |
Output is correct |
4 |
Correct |
188 ms |
30412 KB |
Output is correct |
5 |
Correct |
275 ms |
36768 KB |
Output is correct |
6 |
Correct |
218 ms |
30528 KB |
Output is correct |
7 |
Correct |
244 ms |
36300 KB |
Output is correct |
8 |
Correct |
200 ms |
30452 KB |
Output is correct |
9 |
Correct |
168 ms |
41616 KB |
Output is correct |
10 |
Correct |
325 ms |
35120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
150 ms |
27392 KB |
Output is correct |
6 |
Correct |
101 ms |
37940 KB |
Output is correct |
7 |
Correct |
130 ms |
33996 KB |
Output is correct |
8 |
Correct |
99 ms |
27584 KB |
Output is correct |
9 |
Correct |
174 ms |
31948 KB |
Output is correct |
10 |
Correct |
101 ms |
27584 KB |
Output is correct |
11 |
Correct |
2 ms |
600 KB |
Output is correct |
12 |
Correct |
2 ms |
856 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
724 KB |
Output is correct |
15 |
Correct |
2 ms |
600 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
150 ms |
27392 KB |
Output is correct |
6 |
Correct |
101 ms |
37940 KB |
Output is correct |
7 |
Correct |
130 ms |
33996 KB |
Output is correct |
8 |
Correct |
99 ms |
27584 KB |
Output is correct |
9 |
Correct |
174 ms |
31948 KB |
Output is correct |
10 |
Correct |
101 ms |
27584 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
198 ms |
41440 KB |
Output is correct |
15 |
Correct |
175 ms |
41576 KB |
Output is correct |
16 |
Correct |
165 ms |
41676 KB |
Output is correct |
17 |
Correct |
178 ms |
41672 KB |
Output is correct |
18 |
Correct |
169 ms |
41420 KB |
Output is correct |
19 |
Correct |
167 ms |
41492 KB |
Output is correct |
20 |
Correct |
167 ms |
41488 KB |
Output is correct |
21 |
Correct |
7 ms |
1296 KB |
Output is correct |
22 |
Correct |
171 ms |
41828 KB |
Output is correct |
23 |
Correct |
186 ms |
41672 KB |
Output is correct |
24 |
Correct |
190 ms |
41956 KB |
Output is correct |
25 |
Correct |
169 ms |
41860 KB |
Output is correct |
26 |
Correct |
167 ms |
41888 KB |
Output is correct |
27 |
Correct |
174 ms |
41676 KB |
Output is correct |
28 |
Correct |
170 ms |
41608 KB |
Output is correct |
29 |
Correct |
166 ms |
41648 KB |
Output is correct |
30 |
Correct |
169 ms |
41676 KB |
Output is correct |
31 |
Correct |
230 ms |
30156 KB |
Output is correct |
32 |
Correct |
164 ms |
41416 KB |
Output is correct |
33 |
Correct |
262 ms |
37348 KB |
Output is correct |
34 |
Correct |
188 ms |
30412 KB |
Output is correct |
35 |
Correct |
275 ms |
36768 KB |
Output is correct |
36 |
Correct |
218 ms |
30528 KB |
Output is correct |
37 |
Correct |
244 ms |
36300 KB |
Output is correct |
38 |
Correct |
200 ms |
30452 KB |
Output is correct |
39 |
Correct |
168 ms |
41616 KB |
Output is correct |
40 |
Correct |
325 ms |
35120 KB |
Output is correct |
41 |
Correct |
2 ms |
600 KB |
Output is correct |
42 |
Correct |
2 ms |
856 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
724 KB |
Output is correct |
45 |
Correct |
2 ms |
600 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
600 KB |
Output is correct |
48 |
Correct |
2 ms |
604 KB |
Output is correct |
49 |
Correct |
1 ms |
604 KB |
Output is correct |
50 |
Correct |
1 ms |
604 KB |
Output is correct |
51 |
Correct |
223 ms |
30952 KB |
Output is correct |
52 |
Correct |
176 ms |
41736 KB |
Output is correct |
53 |
Correct |
284 ms |
35804 KB |
Output is correct |
54 |
Correct |
160 ms |
30768 KB |
Output is correct |
55 |
Correct |
203 ms |
30408 KB |
Output is correct |
56 |
Correct |
167 ms |
41868 KB |
Output is correct |
57 |
Correct |
243 ms |
36384 KB |
Output is correct |
58 |
Correct |
171 ms |
30692 KB |
Output is correct |
59 |
Correct |
212 ms |
30892 KB |
Output is correct |
60 |
Correct |
165 ms |
41676 KB |
Output is correct |
61 |
Correct |
292 ms |
36576 KB |
Output is correct |
62 |
Correct |
208 ms |
30748 KB |
Output is correct |
63 |
Correct |
209 ms |
30200 KB |
Output is correct |
64 |
Correct |
166 ms |
41644 KB |
Output is correct |
65 |
Correct |
263 ms |
36296 KB |
Output is correct |
66 |
Correct |
206 ms |
30668 KB |
Output is correct |
67 |
Correct |
226 ms |
30668 KB |
Output is correct |
68 |
Correct |
171 ms |
41676 KB |
Output is correct |
69 |
Correct |
228 ms |
34816 KB |
Output is correct |
70 |
Correct |
195 ms |
30664 KB |
Output is correct |