#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;
false;
}
int n, m, numDFS;
int dep[N], P[N][K], in[N], out[N];
int dp[N], f[N], bit[N];
vector<int> g[N];
vector<tuple<int, int, int>> plans[N];
void update(int i, int val) {
for(; i <= n; i += i & -i) bit[i] += val;
}
void update(int l, int r, int val) {
update(l, val);
update(r + 1, -val);
}
int get(int i) {
int 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]) {
| ^
election_campaign.cpp: In instantiation of 'bool maximize(auto:1&, auto:2) [with auto:1 = int; auto:2 = int]':
election_campaign.cpp:55:61: required from here
election_campaign.cpp:10:5: warning: statement has no effect [-Wunused-value]
10 | false;
| ^~~~~
election_campaign.cpp: In function 'bool maximize(auto:1&, auto:2) [with auto:1 = int; auto:2 = int]':
election_campaign.cpp:9:7: warning: control reaches end of non-void function [-Wreturn-type]
9 | a = b;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
10 ms |
17500 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
9 ms |
17548 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
9 ms |
17548 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
89 ms |
40432 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
10 ms |
17500 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
10 ms |
17500 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |