#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int N = 1e5 + 7;
int n, q, u, v, w, cnt = 0, st[N], ed[N], level[N], dp[N], sum[N];
int par[18][N], it[4 * N], lz[4 * N];
vector <int> edge[N];
vector < pair <int, pair <int, int> > > query[N];
void predfs(int u, int p){
st[u] = ++cnt;
for(int v : edge[u]){
if(v == p) continue;
level[v] = level[u] + 1;
par[0][v] = u;
predfs(v, u);
}
ed[u] = cnt;
}
int lca(int u, int v){
if(level[u] > level[v]) swap(u, v);
for(int i = 17; i >= 0; i--) if(level[par[i][v]] >= level[u]) v = par[i][v];
for(int i = 17; i >= 0; i--) if(par[i][u] != par[i][v]) u = par[i][u], v = par[i][v];
return (u == v ? u : par[0][u]);
}
void lazy(int k, int l, int r){
if(lz[k] == 0) return;
it[k] += lz[k];
if(l != r){
lz[k << 1] += lz[k];
lz[k << 1 | 1] += lz[k];
}
lz[k] = 0;
}
void update(int k, int l, int r, int L, int R, int val){
lazy(k, l, r);
if(l > R || r < L) return;
if(l >= L && r <= R){
it[k] += val;
if(l != r){
lz[k << 1] += val;
lz[k << 1 | 1] += val;
}
return;
}
int mid = (l + r) >> 1;
update(k << 1, l, mid, L, R, val);
update(k << 1 | 1, mid + 1, r, L, R, val);
it[k] = it[k << 1] + it[k << 1 | 1];
}
int get(int k, int l, int r, int pos){
lazy(k, l, r);
if(l > pos || r < pos) return 0;
if(l == r) return it[k];
int mid = (l + r) >> 1;
return get(k << 1, l, mid, pos) + get(k << 1 | 1, mid + 1, r, pos);
}
void dfs(int u, int p){
for(int v : edge[u]){
if(v == p) continue;
dfs(v, u);
sum[u] += dp[v];
}
dp[u] = sum[u];
for(int v : edge[u]){
if(v == p) continue;
update(1, 1, n, st[v], ed[v], sum[u] - dp[v]);
}
for(int i = 0; i < (int)query[u].size(); i++){
int a = query[u][i].se.fi, b = query[u][i].se.se;
int w = query[u][i].fi;
int lef = get(1, 1, n, st[a]);
int rig = get(1, 1, n, st[b]);
dp[u] = max(dp[u], lef + rig + w + sum[a] + sum[b] - sum[u]);
}
}
int main(){
scanf("%d", &n);
for(int i = 1; i < n; i++){
scanf("%d %d", &u, &v);
edge[u].pb(v);
edge[v].pb(u);
}
level[1] = 1;
par[0][1] = 1;
predfs(1, 1);
for(int i = 1; i <= 17; i++){
for(int j = 1; j <= n; j++){
par[i][j] = par[i - 1][par[i - 1][j]];
}
}
scanf("%d", &q);
for(int i = 1; i <= q; i++){
scanf("%d %d %d", &u, &v, &w);
int lc = lca(u, v);
query[lc].pb(mp(w, mp(u, v)));
}
dfs(1, 1);
printf("%d", dp[1]);
}
Compilation message
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:89:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
election_campaign.cpp:91:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~~
election_campaign.cpp:103:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &q);
~~~~~^~~~~~~~~~
election_campaign.cpp:105:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &u, &v, &w);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5240 KB |
Output is correct |
2 |
Correct |
5 ms |
5112 KB |
Output is correct |
3 |
Correct |
5 ms |
5116 KB |
Output is correct |
4 |
Correct |
6 ms |
5240 KB |
Output is correct |
5 |
Correct |
121 ms |
20508 KB |
Output is correct |
6 |
Correct |
73 ms |
28152 KB |
Output is correct |
7 |
Correct |
131 ms |
25464 KB |
Output is correct |
8 |
Correct |
78 ms |
19832 KB |
Output is correct |
9 |
Correct |
127 ms |
23936 KB |
Output is correct |
10 |
Correct |
81 ms |
19832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5240 KB |
Output is correct |
3 |
Correct |
6 ms |
5368 KB |
Output is correct |
4 |
Correct |
289 ms |
31524 KB |
Output is correct |
5 |
Correct |
266 ms |
31652 KB |
Output is correct |
6 |
Correct |
224 ms |
31608 KB |
Output is correct |
7 |
Correct |
257 ms |
31480 KB |
Output is correct |
8 |
Correct |
256 ms |
31560 KB |
Output is correct |
9 |
Correct |
239 ms |
31608 KB |
Output is correct |
10 |
Correct |
267 ms |
31608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5240 KB |
Output is correct |
3 |
Correct |
6 ms |
5368 KB |
Output is correct |
4 |
Correct |
289 ms |
31524 KB |
Output is correct |
5 |
Correct |
266 ms |
31652 KB |
Output is correct |
6 |
Correct |
224 ms |
31608 KB |
Output is correct |
7 |
Correct |
257 ms |
31480 KB |
Output is correct |
8 |
Correct |
256 ms |
31560 KB |
Output is correct |
9 |
Correct |
239 ms |
31608 KB |
Output is correct |
10 |
Correct |
267 ms |
31608 KB |
Output is correct |
11 |
Correct |
25 ms |
6136 KB |
Output is correct |
12 |
Correct |
255 ms |
31828 KB |
Output is correct |
13 |
Correct |
271 ms |
31864 KB |
Output is correct |
14 |
Correct |
247 ms |
31992 KB |
Output is correct |
15 |
Correct |
250 ms |
31788 KB |
Output is correct |
16 |
Correct |
214 ms |
31736 KB |
Output is correct |
17 |
Correct |
258 ms |
31900 KB |
Output is correct |
18 |
Correct |
252 ms |
31864 KB |
Output is correct |
19 |
Correct |
240 ms |
31864 KB |
Output is correct |
20 |
Correct |
250 ms |
31992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
301 ms |
23344 KB |
Output is correct |
2 |
Correct |
231 ms |
31564 KB |
Output is correct |
3 |
Correct |
525 ms |
28536 KB |
Output is correct |
4 |
Correct |
265 ms |
23256 KB |
Output is correct |
5 |
Correct |
646 ms |
28016 KB |
Output is correct |
6 |
Correct |
253 ms |
23152 KB |
Output is correct |
7 |
Correct |
547 ms |
27700 KB |
Output is correct |
8 |
Correct |
466 ms |
23696 KB |
Output is correct |
9 |
Correct |
227 ms |
31608 KB |
Output is correct |
10 |
Correct |
569 ms |
26796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5240 KB |
Output is correct |
2 |
Correct |
5 ms |
5112 KB |
Output is correct |
3 |
Correct |
5 ms |
5116 KB |
Output is correct |
4 |
Correct |
6 ms |
5240 KB |
Output is correct |
5 |
Correct |
121 ms |
20508 KB |
Output is correct |
6 |
Correct |
73 ms |
28152 KB |
Output is correct |
7 |
Correct |
131 ms |
25464 KB |
Output is correct |
8 |
Correct |
78 ms |
19832 KB |
Output is correct |
9 |
Correct |
127 ms |
23936 KB |
Output is correct |
10 |
Correct |
81 ms |
19832 KB |
Output is correct |
11 |
Correct |
7 ms |
5372 KB |
Output is correct |
12 |
Correct |
7 ms |
5368 KB |
Output is correct |
13 |
Correct |
7 ms |
5368 KB |
Output is correct |
14 |
Correct |
7 ms |
5368 KB |
Output is correct |
15 |
Correct |
7 ms |
5368 KB |
Output is correct |
16 |
Correct |
7 ms |
5368 KB |
Output is correct |
17 |
Correct |
7 ms |
5240 KB |
Output is correct |
18 |
Correct |
8 ms |
5344 KB |
Output is correct |
19 |
Correct |
6 ms |
5368 KB |
Output is correct |
20 |
Correct |
8 ms |
5368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5240 KB |
Output is correct |
2 |
Correct |
5 ms |
5112 KB |
Output is correct |
3 |
Correct |
5 ms |
5116 KB |
Output is correct |
4 |
Correct |
6 ms |
5240 KB |
Output is correct |
5 |
Correct |
121 ms |
20508 KB |
Output is correct |
6 |
Correct |
73 ms |
28152 KB |
Output is correct |
7 |
Correct |
131 ms |
25464 KB |
Output is correct |
8 |
Correct |
78 ms |
19832 KB |
Output is correct |
9 |
Correct |
127 ms |
23936 KB |
Output is correct |
10 |
Correct |
81 ms |
19832 KB |
Output is correct |
11 |
Correct |
5 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
5240 KB |
Output is correct |
13 |
Correct |
6 ms |
5368 KB |
Output is correct |
14 |
Correct |
289 ms |
31524 KB |
Output is correct |
15 |
Correct |
266 ms |
31652 KB |
Output is correct |
16 |
Correct |
224 ms |
31608 KB |
Output is correct |
17 |
Correct |
257 ms |
31480 KB |
Output is correct |
18 |
Correct |
256 ms |
31560 KB |
Output is correct |
19 |
Correct |
239 ms |
31608 KB |
Output is correct |
20 |
Correct |
267 ms |
31608 KB |
Output is correct |
21 |
Correct |
25 ms |
6136 KB |
Output is correct |
22 |
Correct |
255 ms |
31828 KB |
Output is correct |
23 |
Correct |
271 ms |
31864 KB |
Output is correct |
24 |
Correct |
247 ms |
31992 KB |
Output is correct |
25 |
Correct |
250 ms |
31788 KB |
Output is correct |
26 |
Correct |
214 ms |
31736 KB |
Output is correct |
27 |
Correct |
258 ms |
31900 KB |
Output is correct |
28 |
Correct |
252 ms |
31864 KB |
Output is correct |
29 |
Correct |
240 ms |
31864 KB |
Output is correct |
30 |
Correct |
250 ms |
31992 KB |
Output is correct |
31 |
Correct |
301 ms |
23344 KB |
Output is correct |
32 |
Correct |
231 ms |
31564 KB |
Output is correct |
33 |
Correct |
525 ms |
28536 KB |
Output is correct |
34 |
Correct |
265 ms |
23256 KB |
Output is correct |
35 |
Correct |
646 ms |
28016 KB |
Output is correct |
36 |
Correct |
253 ms |
23152 KB |
Output is correct |
37 |
Correct |
547 ms |
27700 KB |
Output is correct |
38 |
Correct |
466 ms |
23696 KB |
Output is correct |
39 |
Correct |
227 ms |
31608 KB |
Output is correct |
40 |
Correct |
569 ms |
26796 KB |
Output is correct |
41 |
Correct |
7 ms |
5372 KB |
Output is correct |
42 |
Correct |
7 ms |
5368 KB |
Output is correct |
43 |
Correct |
7 ms |
5368 KB |
Output is correct |
44 |
Correct |
7 ms |
5368 KB |
Output is correct |
45 |
Correct |
7 ms |
5368 KB |
Output is correct |
46 |
Correct |
7 ms |
5368 KB |
Output is correct |
47 |
Correct |
7 ms |
5240 KB |
Output is correct |
48 |
Correct |
8 ms |
5344 KB |
Output is correct |
49 |
Correct |
6 ms |
5368 KB |
Output is correct |
50 |
Correct |
8 ms |
5368 KB |
Output is correct |
51 |
Correct |
545 ms |
24160 KB |
Output is correct |
52 |
Correct |
273 ms |
31868 KB |
Output is correct |
53 |
Correct |
590 ms |
27180 KB |
Output is correct |
54 |
Correct |
384 ms |
23604 KB |
Output is correct |
55 |
Correct |
363 ms |
23728 KB |
Output is correct |
56 |
Correct |
330 ms |
31828 KB |
Output is correct |
57 |
Correct |
419 ms |
27768 KB |
Output is correct |
58 |
Correct |
243 ms |
23472 KB |
Output is correct |
59 |
Correct |
465 ms |
23900 KB |
Output is correct |
60 |
Correct |
247 ms |
31864 KB |
Output is correct |
61 |
Correct |
528 ms |
28024 KB |
Output is correct |
62 |
Correct |
260 ms |
23528 KB |
Output is correct |
63 |
Correct |
324 ms |
23520 KB |
Output is correct |
64 |
Correct |
254 ms |
31864 KB |
Output is correct |
65 |
Correct |
458 ms |
27684 KB |
Output is correct |
66 |
Correct |
283 ms |
23472 KB |
Output is correct |
67 |
Correct |
341 ms |
23728 KB |
Output is correct |
68 |
Correct |
271 ms |
31864 KB |
Output is correct |
69 |
Correct |
610 ms |
26696 KB |
Output is correct |
70 |
Correct |
262 ms |
23228 KB |
Output is correct |