#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define fore(a, b, c) for(int a=b; a<c; ++a)
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define F first
#define S second
#define ii pair<int,int>
#define dbg(x) cerr << #x << ": " << x << endl
#define raya cerr << "=============" << endl
const int N = 2e5+5;
const int LG = 20;
struct fenwick {
int n;
vi t;
fenwick(int n) : n(n) {
t.assign(n+5, 0);
}
void add(int i, int x){
for(; i<=n; i+=i&-i){
t[i] += x;
}
}
int query(int i){
int r = 0;
for(; i>0; i-=i&-i){
r += t[i];
}
return r;
}
} bit(N-5);
vi adj[N];
vector<array<int,3>> ways[N];
int dp[N][2];
int pa[N][LG];
int dep[N];
int timer;
int entro[N], salgo[N];
int n, m;
void dfs(int at, int ante=0){
entro[at] = ++timer;
pa[at][0] = ante;
for(int u : adj[at]){
if(u == ante) continue;
dep[u] = dep[at] + 1;
dfs(u, at);
}
salgo[at] = timer;
}
int jump(int a, int k){
fore(i, 0, LG){
if((k >> i) & 1){
a = pa[a][i];
}
}
return a;
}
int lca(int a, int b){
if(dep[a] > dep[b]) swap(a, b);
b = jump(b, dep[b] - dep[a]);
if(a == b) return a;
for(int i=LG-1; i>=0; --i){
if(pa[a][i] != pa[b][i]){
a = pa[a][i];
b = pa[b][i];
}
}
return pa[a][0];
}
int f(int at, int tomo){
int &res = dp[at][tomo];
if(res > -1) return res;
res = 0;
if(tomo == 0){
int cur = 0;
for(int u : adj[at]){
if(u == pa[at][0]) continue;
cur += max(f(u, 0), f(u, 1));
}
res = cur;
}
if(tomo == 1){
int sintomar = f(at, 0);
int mx = sintomar;
for(auto [a, b, c] : ways[at]){
int tmp = bit.query(entro[a]) + bit.query(entro[b]) + c;
mx = max(mx, sintomar + tmp);
}
res = mx; // tomando
// es posible que sintomar = res, es decir que lo mejor es no tomar nada.
bit.add(entro[at], sintomar - res);
bit.add(salgo[at]+1, res - sintomar);
}
return res;
}
signed main(){
memset(dp, -1, sizeof dp);
cin >> n;
fore(i, 1, n){
int u, v;
cin >> u >> v;
adj[u].pb(v);
adj[v].pb(u);
}
dfs(1);
for(int i=1; i<LG; ++i){
for(int j=1; j<=n; ++j){
pa[j][i] = pa[pa[j][i-1]][i-1];
}
}
cin >> m;
fore(i, 0, m){
int a, b, c;
cin >> a >> b >> c;
ways[lca(a, b)].pb({a, b, c});
}
int ans = max(f(1, 0), f(1, 1));
cout << ans << endl;
}
Compilation message
election_campaign.cpp: In function 'll f(ll, ll)':
election_campaign.cpp:102:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
102 | for(auto [a, b, c] : ways[at]){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
5 ms |
14764 KB |
Output is correct |
5 |
Correct |
70 ms |
37384 KB |
Output is correct |
6 |
Correct |
62 ms |
52304 KB |
Output is correct |
7 |
Correct |
79 ms |
47448 KB |
Output is correct |
8 |
Correct |
61 ms |
37604 KB |
Output is correct |
9 |
Correct |
70 ms |
44112 KB |
Output is correct |
10 |
Correct |
60 ms |
37696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14424 KB |
Output is correct |
2 |
Correct |
5 ms |
14428 KB |
Output is correct |
3 |
Correct |
7 ms |
14940 KB |
Output is correct |
4 |
Correct |
120 ms |
57196 KB |
Output is correct |
5 |
Correct |
130 ms |
57100 KB |
Output is correct |
6 |
Correct |
114 ms |
57172 KB |
Output is correct |
7 |
Correct |
119 ms |
57144 KB |
Output is correct |
8 |
Correct |
126 ms |
57168 KB |
Output is correct |
9 |
Correct |
120 ms |
57172 KB |
Output is correct |
10 |
Correct |
120 ms |
57192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
14424 KB |
Output is correct |
2 |
Correct |
5 ms |
14428 KB |
Output is correct |
3 |
Correct |
7 ms |
14940 KB |
Output is correct |
4 |
Correct |
120 ms |
57196 KB |
Output is correct |
5 |
Correct |
130 ms |
57100 KB |
Output is correct |
6 |
Correct |
114 ms |
57172 KB |
Output is correct |
7 |
Correct |
119 ms |
57144 KB |
Output is correct |
8 |
Correct |
126 ms |
57168 KB |
Output is correct |
9 |
Correct |
120 ms |
57172 KB |
Output is correct |
10 |
Correct |
120 ms |
57192 KB |
Output is correct |
11 |
Correct |
15 ms |
16100 KB |
Output is correct |
12 |
Correct |
127 ms |
57468 KB |
Output is correct |
13 |
Correct |
135 ms |
57428 KB |
Output is correct |
14 |
Correct |
131 ms |
57620 KB |
Output is correct |
15 |
Correct |
127 ms |
57376 KB |
Output is correct |
16 |
Correct |
122 ms |
57536 KB |
Output is correct |
17 |
Correct |
128 ms |
57540 KB |
Output is correct |
18 |
Correct |
125 ms |
57424 KB |
Output is correct |
19 |
Correct |
120 ms |
57544 KB |
Output is correct |
20 |
Correct |
124 ms |
57424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
41736 KB |
Output is correct |
2 |
Correct |
112 ms |
57428 KB |
Output is correct |
3 |
Correct |
153 ms |
51648 KB |
Output is correct |
4 |
Correct |
110 ms |
42248 KB |
Output is correct |
5 |
Correct |
136 ms |
50320 KB |
Output is correct |
6 |
Correct |
121 ms |
42184 KB |
Output is correct |
7 |
Correct |
161 ms |
49932 KB |
Output is correct |
8 |
Correct |
127 ms |
41808 KB |
Output is correct |
9 |
Correct |
109 ms |
57240 KB |
Output is correct |
10 |
Correct |
152 ms |
48132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
5 ms |
14764 KB |
Output is correct |
5 |
Correct |
70 ms |
37384 KB |
Output is correct |
6 |
Correct |
62 ms |
52304 KB |
Output is correct |
7 |
Correct |
79 ms |
47448 KB |
Output is correct |
8 |
Correct |
61 ms |
37604 KB |
Output is correct |
9 |
Correct |
70 ms |
44112 KB |
Output is correct |
10 |
Correct |
60 ms |
37696 KB |
Output is correct |
11 |
Correct |
6 ms |
14684 KB |
Output is correct |
12 |
Correct |
6 ms |
14940 KB |
Output is correct |
13 |
Correct |
6 ms |
14684 KB |
Output is correct |
14 |
Correct |
6 ms |
14684 KB |
Output is correct |
15 |
Correct |
10 ms |
14844 KB |
Output is correct |
16 |
Correct |
7 ms |
14940 KB |
Output is correct |
17 |
Correct |
6 ms |
14644 KB |
Output is correct |
18 |
Correct |
6 ms |
14684 KB |
Output is correct |
19 |
Correct |
6 ms |
14684 KB |
Output is correct |
20 |
Correct |
6 ms |
14940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14428 KB |
Output is correct |
2 |
Correct |
5 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14428 KB |
Output is correct |
4 |
Correct |
5 ms |
14764 KB |
Output is correct |
5 |
Correct |
70 ms |
37384 KB |
Output is correct |
6 |
Correct |
62 ms |
52304 KB |
Output is correct |
7 |
Correct |
79 ms |
47448 KB |
Output is correct |
8 |
Correct |
61 ms |
37604 KB |
Output is correct |
9 |
Correct |
70 ms |
44112 KB |
Output is correct |
10 |
Correct |
60 ms |
37696 KB |
Output is correct |
11 |
Correct |
5 ms |
14424 KB |
Output is correct |
12 |
Correct |
5 ms |
14428 KB |
Output is correct |
13 |
Correct |
7 ms |
14940 KB |
Output is correct |
14 |
Correct |
120 ms |
57196 KB |
Output is correct |
15 |
Correct |
130 ms |
57100 KB |
Output is correct |
16 |
Correct |
114 ms |
57172 KB |
Output is correct |
17 |
Correct |
119 ms |
57144 KB |
Output is correct |
18 |
Correct |
126 ms |
57168 KB |
Output is correct |
19 |
Correct |
120 ms |
57172 KB |
Output is correct |
20 |
Correct |
120 ms |
57192 KB |
Output is correct |
21 |
Correct |
15 ms |
16100 KB |
Output is correct |
22 |
Correct |
127 ms |
57468 KB |
Output is correct |
23 |
Correct |
135 ms |
57428 KB |
Output is correct |
24 |
Correct |
131 ms |
57620 KB |
Output is correct |
25 |
Correct |
127 ms |
57376 KB |
Output is correct |
26 |
Correct |
122 ms |
57536 KB |
Output is correct |
27 |
Correct |
128 ms |
57540 KB |
Output is correct |
28 |
Correct |
125 ms |
57424 KB |
Output is correct |
29 |
Correct |
120 ms |
57544 KB |
Output is correct |
30 |
Correct |
124 ms |
57424 KB |
Output is correct |
31 |
Correct |
125 ms |
41736 KB |
Output is correct |
32 |
Correct |
112 ms |
57428 KB |
Output is correct |
33 |
Correct |
153 ms |
51648 KB |
Output is correct |
34 |
Correct |
110 ms |
42248 KB |
Output is correct |
35 |
Correct |
136 ms |
50320 KB |
Output is correct |
36 |
Correct |
121 ms |
42184 KB |
Output is correct |
37 |
Correct |
161 ms |
49932 KB |
Output is correct |
38 |
Correct |
127 ms |
41808 KB |
Output is correct |
39 |
Correct |
109 ms |
57240 KB |
Output is correct |
40 |
Correct |
152 ms |
48132 KB |
Output is correct |
41 |
Correct |
6 ms |
14684 KB |
Output is correct |
42 |
Correct |
6 ms |
14940 KB |
Output is correct |
43 |
Correct |
6 ms |
14684 KB |
Output is correct |
44 |
Correct |
6 ms |
14684 KB |
Output is correct |
45 |
Correct |
10 ms |
14844 KB |
Output is correct |
46 |
Correct |
7 ms |
14940 KB |
Output is correct |
47 |
Correct |
6 ms |
14644 KB |
Output is correct |
48 |
Correct |
6 ms |
14684 KB |
Output is correct |
49 |
Correct |
6 ms |
14684 KB |
Output is correct |
50 |
Correct |
6 ms |
14940 KB |
Output is correct |
51 |
Correct |
138 ms |
42128 KB |
Output is correct |
52 |
Correct |
125 ms |
57428 KB |
Output is correct |
53 |
Correct |
154 ms |
48672 KB |
Output is correct |
54 |
Correct |
115 ms |
42088 KB |
Output is correct |
55 |
Correct |
134 ms |
41900 KB |
Output is correct |
56 |
Correct |
132 ms |
57500 KB |
Output is correct |
57 |
Correct |
153 ms |
49812 KB |
Output is correct |
58 |
Correct |
117 ms |
42252 KB |
Output is correct |
59 |
Correct |
127 ms |
42188 KB |
Output is correct |
60 |
Correct |
121 ms |
57376 KB |
Output is correct |
61 |
Correct |
149 ms |
50060 KB |
Output is correct |
62 |
Correct |
117 ms |
42448 KB |
Output is correct |
63 |
Correct |
132 ms |
42104 KB |
Output is correct |
64 |
Correct |
125 ms |
57420 KB |
Output is correct |
65 |
Correct |
155 ms |
49836 KB |
Output is correct |
66 |
Correct |
112 ms |
42016 KB |
Output is correct |
67 |
Correct |
131 ms |
42692 KB |
Output is correct |
68 |
Correct |
124 ms |
57428 KB |
Output is correct |
69 |
Correct |
152 ms |
47504 KB |
Output is correct |
70 |
Correct |
127 ms |
42368 KB |
Output is correct |