#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5 + 5;
const int K = 17;
bool maximize(auto &a, auto b) {
if(a >= b) return true;
a = b;
return false;
}
int n, m, numDFS;
int dep[N], P[N][K], in[N], out[N];
ll dp[N], f[N], bit[N];
vector<int> g[N];
vector<tuple<int, int, int>> plans[N];
void update(int i, ll val) {
for(; i <= n; i += i & -i) bit[i] += val;
}
void update(int l, int r, ll val) {
update(l, val);
update(r + 1, -val);
}
ll get(int i) {
ll res = 0;
for(; i; i -= i & -i) res += bit[i];
return res;
}
void euler_tour(int u, int p = 0) {
in[u] = ++numDFS;
dep[u] = dep[p] + 1;
P[u][0] = p;
for(int i = 1; i < K; ++i) P[u][i] = P[P[u][i - 1]][i - 1];
for(int v : g[u]) if(v != p) {
euler_tour(v, u);
}
out[u] = numDFS;
}
int lca(int u, int v) {
if(dep[u] < dep[v]) swap(u, v);
for(int i = K - 1; i >= 0; --i) if(dep[P[u][i]] >= dep[v]) u = P[u][i];
for(int i = K - 1; i >= 0; --i) if(P[u][i] != P[v][i]) u = P[u][i], v = P[v][i];
return u == v ? u : P[u][0];
}
void dfs(int u, int p = 0) {
for(int v : g[u]) if(v != p) dfs(v, u);
for(auto [v1, v2, c] : plans[u]) {
maximize(dp[u], f[u] - get(in[v1]) - get(in[v2]) + c);
}
maximize(dp[u], f[u]);
update(in[u], out[u], dp[u] - f[u]);
f[p] += dp[u];
}
void solve() {
cin >> n;
for(int i = 1, u, v; i < n; ++i) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
euler_tour(1);
cin >> m;
for(int i = 1, u, v, c; i <= m; ++i) {
cin >> u >> v >> c;
int p = lca(u, v);
plans[p].emplace_back(u, v, c);
}
dfs(1);
cout << dp[1] << '\n';
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int test = 1;
// cin >> test;
while(test--) solve();
return 0;
}
// https://oj.uz/problem/view/JOI15_election_campaign
Compilation message
election_campaign.cpp:7:15: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
7 | bool maximize(auto &a, auto b) {
| ^~~~
election_campaign.cpp:7:24: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
7 | bool maximize(auto &a, auto b) {
| ^~~~
election_campaign.cpp: In function 'void dfs(int, int)':
election_campaign.cpp:54:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
54 | for(auto [v1, v2, c] : plans[u]) {
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
66 ms |
19600 KB |
Output is correct |
6 |
Correct |
33 ms |
25600 KB |
Output is correct |
7 |
Correct |
54 ms |
23636 KB |
Output is correct |
8 |
Correct |
36 ms |
20052 KB |
Output is correct |
9 |
Correct |
57 ms |
22588 KB |
Output is correct |
10 |
Correct |
38 ms |
20060 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
91 ms |
29012 KB |
Output is correct |
5 |
Correct |
89 ms |
29004 KB |
Output is correct |
6 |
Correct |
102 ms |
29012 KB |
Output is correct |
7 |
Correct |
90 ms |
29008 KB |
Output is correct |
8 |
Correct |
87 ms |
29068 KB |
Output is correct |
9 |
Correct |
84 ms |
29160 KB |
Output is correct |
10 |
Correct |
90 ms |
29424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8796 KB |
Output is correct |
4 |
Correct |
91 ms |
29012 KB |
Output is correct |
5 |
Correct |
89 ms |
29004 KB |
Output is correct |
6 |
Correct |
102 ms |
29012 KB |
Output is correct |
7 |
Correct |
90 ms |
29008 KB |
Output is correct |
8 |
Correct |
87 ms |
29068 KB |
Output is correct |
9 |
Correct |
84 ms |
29160 KB |
Output is correct |
10 |
Correct |
90 ms |
29424 KB |
Output is correct |
11 |
Correct |
10 ms |
9564 KB |
Output is correct |
12 |
Correct |
109 ms |
29240 KB |
Output is correct |
13 |
Correct |
95 ms |
29268 KB |
Output is correct |
14 |
Correct |
88 ms |
29524 KB |
Output is correct |
15 |
Correct |
94 ms |
29264 KB |
Output is correct |
16 |
Correct |
87 ms |
29472 KB |
Output is correct |
17 |
Correct |
88 ms |
29448 KB |
Output is correct |
18 |
Correct |
90 ms |
29408 KB |
Output is correct |
19 |
Correct |
91 ms |
29456 KB |
Output is correct |
20 |
Correct |
91 ms |
29264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
106 ms |
19976 KB |
Output is correct |
2 |
Correct |
88 ms |
29060 KB |
Output is correct |
3 |
Correct |
149 ms |
26576 KB |
Output is correct |
4 |
Correct |
81 ms |
22796 KB |
Output is correct |
5 |
Correct |
113 ms |
26196 KB |
Output is correct |
6 |
Correct |
77 ms |
22984 KB |
Output is correct |
7 |
Correct |
142 ms |
26256 KB |
Output is correct |
8 |
Correct |
99 ms |
22800 KB |
Output is correct |
9 |
Correct |
90 ms |
29000 KB |
Output is correct |
10 |
Correct |
115 ms |
25044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
66 ms |
19600 KB |
Output is correct |
6 |
Correct |
33 ms |
25600 KB |
Output is correct |
7 |
Correct |
54 ms |
23636 KB |
Output is correct |
8 |
Correct |
36 ms |
20052 KB |
Output is correct |
9 |
Correct |
57 ms |
22588 KB |
Output is correct |
10 |
Correct |
38 ms |
20060 KB |
Output is correct |
11 |
Correct |
2 ms |
8796 KB |
Output is correct |
12 |
Correct |
2 ms |
8792 KB |
Output is correct |
13 |
Correct |
2 ms |
8792 KB |
Output is correct |
14 |
Correct |
3 ms |
8796 KB |
Output is correct |
15 |
Correct |
2 ms |
8796 KB |
Output is correct |
16 |
Correct |
2 ms |
8796 KB |
Output is correct |
17 |
Correct |
3 ms |
8796 KB |
Output is correct |
18 |
Correct |
3 ms |
8796 KB |
Output is correct |
19 |
Correct |
2 ms |
8796 KB |
Output is correct |
20 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8796 KB |
Output is correct |
2 |
Correct |
2 ms |
8796 KB |
Output is correct |
3 |
Correct |
2 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
5 |
Correct |
66 ms |
19600 KB |
Output is correct |
6 |
Correct |
33 ms |
25600 KB |
Output is correct |
7 |
Correct |
54 ms |
23636 KB |
Output is correct |
8 |
Correct |
36 ms |
20052 KB |
Output is correct |
9 |
Correct |
57 ms |
22588 KB |
Output is correct |
10 |
Correct |
38 ms |
20060 KB |
Output is correct |
11 |
Correct |
2 ms |
8792 KB |
Output is correct |
12 |
Correct |
2 ms |
8796 KB |
Output is correct |
13 |
Correct |
2 ms |
8796 KB |
Output is correct |
14 |
Correct |
91 ms |
29012 KB |
Output is correct |
15 |
Correct |
89 ms |
29004 KB |
Output is correct |
16 |
Correct |
102 ms |
29012 KB |
Output is correct |
17 |
Correct |
90 ms |
29008 KB |
Output is correct |
18 |
Correct |
87 ms |
29068 KB |
Output is correct |
19 |
Correct |
84 ms |
29160 KB |
Output is correct |
20 |
Correct |
90 ms |
29424 KB |
Output is correct |
21 |
Correct |
10 ms |
9564 KB |
Output is correct |
22 |
Correct |
109 ms |
29240 KB |
Output is correct |
23 |
Correct |
95 ms |
29268 KB |
Output is correct |
24 |
Correct |
88 ms |
29524 KB |
Output is correct |
25 |
Correct |
94 ms |
29264 KB |
Output is correct |
26 |
Correct |
87 ms |
29472 KB |
Output is correct |
27 |
Correct |
88 ms |
29448 KB |
Output is correct |
28 |
Correct |
90 ms |
29408 KB |
Output is correct |
29 |
Correct |
91 ms |
29456 KB |
Output is correct |
30 |
Correct |
91 ms |
29264 KB |
Output is correct |
31 |
Correct |
106 ms |
19976 KB |
Output is correct |
32 |
Correct |
88 ms |
29060 KB |
Output is correct |
33 |
Correct |
149 ms |
26576 KB |
Output is correct |
34 |
Correct |
81 ms |
22796 KB |
Output is correct |
35 |
Correct |
113 ms |
26196 KB |
Output is correct |
36 |
Correct |
77 ms |
22984 KB |
Output is correct |
37 |
Correct |
142 ms |
26256 KB |
Output is correct |
38 |
Correct |
99 ms |
22800 KB |
Output is correct |
39 |
Correct |
90 ms |
29000 KB |
Output is correct |
40 |
Correct |
115 ms |
25044 KB |
Output is correct |
41 |
Correct |
2 ms |
8796 KB |
Output is correct |
42 |
Correct |
2 ms |
8792 KB |
Output is correct |
43 |
Correct |
2 ms |
8792 KB |
Output is correct |
44 |
Correct |
3 ms |
8796 KB |
Output is correct |
45 |
Correct |
2 ms |
8796 KB |
Output is correct |
46 |
Correct |
2 ms |
8796 KB |
Output is correct |
47 |
Correct |
3 ms |
8796 KB |
Output is correct |
48 |
Correct |
3 ms |
8796 KB |
Output is correct |
49 |
Correct |
2 ms |
8796 KB |
Output is correct |
50 |
Correct |
2 ms |
8796 KB |
Output is correct |
51 |
Correct |
105 ms |
22868 KB |
Output is correct |
52 |
Correct |
89 ms |
29264 KB |
Output is correct |
53 |
Correct |
177 ms |
25608 KB |
Output is correct |
54 |
Correct |
77 ms |
23088 KB |
Output is correct |
55 |
Correct |
98 ms |
22796 KB |
Output is correct |
56 |
Correct |
89 ms |
29264 KB |
Output is correct |
57 |
Correct |
123 ms |
26196 KB |
Output is correct |
58 |
Correct |
76 ms |
23012 KB |
Output is correct |
59 |
Correct |
106 ms |
22888 KB |
Output is correct |
60 |
Correct |
99 ms |
29348 KB |
Output is correct |
61 |
Correct |
115 ms |
26192 KB |
Output is correct |
62 |
Correct |
81 ms |
23220 KB |
Output is correct |
63 |
Correct |
103 ms |
22760 KB |
Output is correct |
64 |
Correct |
99 ms |
29312 KB |
Output is correct |
65 |
Correct |
127 ms |
26032 KB |
Output is correct |
66 |
Correct |
81 ms |
23072 KB |
Output is correct |
67 |
Correct |
97 ms |
22972 KB |
Output is correct |
68 |
Correct |
89 ms |
29400 KB |
Output is correct |
69 |
Correct |
115 ms |
25192 KB |
Output is correct |
70 |
Correct |
81 ms |
23352 KB |
Output is correct |