#include <bits/stdc++.h>
using namespace std;
struct Q{ int x, y, c; };
int n, m, d[100010], dp[100010], ps[100010], pe[100010], cnt, sp[17][100010];
vector<int> e[100010], ch[100010];
vector<Q> q[100010];
const int sz = 131072;
struct Seg{
int dat[2 * sz];
void upd(int s, int e, int x){
for(s += sz, e += sz; s <= e; s /= 2, e /= 2){
if(s % 2 == 1) dat[s++] += x;
if(e % 2 == 0) dat[e--] += x;
}
}
int get(int x){
int ret = 0;
for(x += sz; x; x /= 2) ret += dat[x];
return ret;
}
} S, T;
void g(int x, int p){
ps[x] = ++cnt;
sp[0][x] = p;
d[x] = d[p] + 1;
for(int i = 1; i < 17; i++) sp[i][x] = sp[i - 1][sp[i - 1][x]];
for(auto &i : e[x]){
if(i != p){
ch[x].push_back(i);
g(i, x);
}
}
pe[x] = cnt;
}
int lca(int x, int y){
if(d[x] < d[y]) swap(x, y);
for(int i = 16; i >= 0; i--) if(d[sp[i][x]] >= d[y]) x = sp[i][x];
if(x == y) return x;
for(int i = 16; i >= 0; i--){
if(sp[i][x] != sp[i][y]){ x = sp[i][x]; y = sp[i][y]; }
}
return sp[0][x];
}
void f(int x){
int cs = 0;
for(auto &i : ch[x]){
f(i);
cs += dp[i];
}
for(auto &i : ch[x]) T.upd(ps[i], pe[i], i);
int ret = cs;
for(auto &i : q[x]){
int cret = cs;
if(i.x != x) cret += S.get(ps[i.x]) - dp[T.get(ps[i.x])];
if(i.y != x) cret += S.get(ps[i.y]) - dp[T.get(ps[i.y])];
ret = max(ret, cret + i.c);
}
for(auto &i : ch[x]){
S.upd(ps[x] + 1, ps[i] - 1, dp[i]);
S.upd(pe[i] + 1, pe[x], dp[i]);
T.upd(ps[i], pe[i], -i);
}
S.upd(ps[x], ps[x], cs);
dp[x] = ret;
}
int main(){
scanf("%d", &n);
for(int i = 0, x, y; i < n - 1; i++){
scanf("%d%d", &x, &y);
e[x].push_back(y);
e[y].push_back(x);
}
g(1, 1);
scanf("%d", &m);
for(int x, y, z; m--; ){
scanf("%d%d%d", &x, &y, &z);
q[lca(x, y)].push_back({x, y, z});
}
f(1);
printf("%d\n", dp[1]);
}
Compilation message
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:74:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
^
election_campaign.cpp:76:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y);
^
election_campaign.cpp:81:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &m);
^
election_campaign.cpp:83:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &x, &y, &z);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
19308 KB |
Output is correct |
2 |
Correct |
0 ms |
19308 KB |
Output is correct |
3 |
Correct |
3 ms |
19308 KB |
Output is correct |
4 |
Correct |
0 ms |
19308 KB |
Output is correct |
5 |
Correct |
126 ms |
24192 KB |
Output is correct |
6 |
Correct |
66 ms |
33196 KB |
Output is correct |
7 |
Correct |
179 ms |
29844 KB |
Output is correct |
8 |
Correct |
146 ms |
23856 KB |
Output is correct |
9 |
Correct |
213 ms |
28288 KB |
Output is correct |
10 |
Correct |
136 ms |
23792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19308 KB |
Output is correct |
2 |
Correct |
0 ms |
19308 KB |
Output is correct |
3 |
Correct |
3 ms |
19440 KB |
Output is correct |
4 |
Correct |
269 ms |
35316 KB |
Output is correct |
5 |
Correct |
263 ms |
35312 KB |
Output is correct |
6 |
Correct |
283 ms |
35308 KB |
Output is correct |
7 |
Correct |
269 ms |
35308 KB |
Output is correct |
8 |
Correct |
309 ms |
35308 KB |
Output is correct |
9 |
Correct |
363 ms |
35312 KB |
Output is correct |
10 |
Correct |
296 ms |
35312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19308 KB |
Output is correct |
2 |
Correct |
0 ms |
19308 KB |
Output is correct |
3 |
Correct |
3 ms |
19440 KB |
Output is correct |
4 |
Correct |
269 ms |
35316 KB |
Output is correct |
5 |
Correct |
263 ms |
35312 KB |
Output is correct |
6 |
Correct |
283 ms |
35308 KB |
Output is correct |
7 |
Correct |
269 ms |
35308 KB |
Output is correct |
8 |
Correct |
309 ms |
35308 KB |
Output is correct |
9 |
Correct |
363 ms |
35312 KB |
Output is correct |
10 |
Correct |
296 ms |
35312 KB |
Output is correct |
11 |
Correct |
16 ms |
19836 KB |
Output is correct |
12 |
Correct |
326 ms |
35312 KB |
Output is correct |
13 |
Correct |
309 ms |
35316 KB |
Output is correct |
14 |
Correct |
349 ms |
35312 KB |
Output is correct |
15 |
Correct |
313 ms |
35308 KB |
Output is correct |
16 |
Correct |
293 ms |
35308 KB |
Output is correct |
17 |
Correct |
329 ms |
35316 KB |
Output is correct |
18 |
Correct |
303 ms |
35312 KB |
Output is correct |
19 |
Correct |
333 ms |
35308 KB |
Output is correct |
20 |
Correct |
296 ms |
35316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
376 ms |
25888 KB |
Output is correct |
2 |
Correct |
323 ms |
35308 KB |
Output is correct |
3 |
Correct |
396 ms |
31480 KB |
Output is correct |
4 |
Correct |
276 ms |
25684 KB |
Output is correct |
5 |
Correct |
526 ms |
31020 KB |
Output is correct |
6 |
Correct |
269 ms |
26112 KB |
Output is correct |
7 |
Correct |
603 ms |
30672 KB |
Output is correct |
8 |
Correct |
523 ms |
25944 KB |
Output is correct |
9 |
Correct |
309 ms |
35312 KB |
Output is correct |
10 |
Correct |
456 ms |
29936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
19308 KB |
Output is correct |
2 |
Correct |
0 ms |
19308 KB |
Output is correct |
3 |
Correct |
3 ms |
19308 KB |
Output is correct |
4 |
Correct |
0 ms |
19308 KB |
Output is correct |
5 |
Correct |
126 ms |
24192 KB |
Output is correct |
6 |
Correct |
66 ms |
33196 KB |
Output is correct |
7 |
Correct |
179 ms |
29844 KB |
Output is correct |
8 |
Correct |
146 ms |
23856 KB |
Output is correct |
9 |
Correct |
213 ms |
28288 KB |
Output is correct |
10 |
Correct |
136 ms |
23792 KB |
Output is correct |
11 |
Correct |
0 ms |
19440 KB |
Output is correct |
12 |
Correct |
3 ms |
19440 KB |
Output is correct |
13 |
Correct |
9 ms |
19440 KB |
Output is correct |
14 |
Correct |
3 ms |
19440 KB |
Output is correct |
15 |
Correct |
3 ms |
19440 KB |
Output is correct |
16 |
Correct |
6 ms |
19440 KB |
Output is correct |
17 |
Correct |
3 ms |
19440 KB |
Output is correct |
18 |
Correct |
0 ms |
19440 KB |
Output is correct |
19 |
Correct |
0 ms |
19440 KB |
Output is correct |
20 |
Correct |
0 ms |
19440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
19308 KB |
Output is correct |
2 |
Correct |
0 ms |
19308 KB |
Output is correct |
3 |
Correct |
3 ms |
19308 KB |
Output is correct |
4 |
Correct |
0 ms |
19308 KB |
Output is correct |
5 |
Correct |
126 ms |
24192 KB |
Output is correct |
6 |
Correct |
66 ms |
33196 KB |
Output is correct |
7 |
Correct |
179 ms |
29844 KB |
Output is correct |
8 |
Correct |
146 ms |
23856 KB |
Output is correct |
9 |
Correct |
213 ms |
28288 KB |
Output is correct |
10 |
Correct |
136 ms |
23792 KB |
Output is correct |
11 |
Correct |
3 ms |
19308 KB |
Output is correct |
12 |
Correct |
0 ms |
19308 KB |
Output is correct |
13 |
Correct |
3 ms |
19440 KB |
Output is correct |
14 |
Correct |
269 ms |
35316 KB |
Output is correct |
15 |
Correct |
263 ms |
35312 KB |
Output is correct |
16 |
Correct |
283 ms |
35308 KB |
Output is correct |
17 |
Correct |
269 ms |
35308 KB |
Output is correct |
18 |
Correct |
309 ms |
35308 KB |
Output is correct |
19 |
Correct |
363 ms |
35312 KB |
Output is correct |
20 |
Correct |
296 ms |
35312 KB |
Output is correct |
21 |
Correct |
16 ms |
19836 KB |
Output is correct |
22 |
Correct |
326 ms |
35312 KB |
Output is correct |
23 |
Correct |
309 ms |
35316 KB |
Output is correct |
24 |
Correct |
349 ms |
35312 KB |
Output is correct |
25 |
Correct |
313 ms |
35308 KB |
Output is correct |
26 |
Correct |
293 ms |
35308 KB |
Output is correct |
27 |
Correct |
329 ms |
35316 KB |
Output is correct |
28 |
Correct |
303 ms |
35312 KB |
Output is correct |
29 |
Correct |
333 ms |
35308 KB |
Output is correct |
30 |
Correct |
296 ms |
35316 KB |
Output is correct |
31 |
Correct |
376 ms |
25888 KB |
Output is correct |
32 |
Correct |
323 ms |
35308 KB |
Output is correct |
33 |
Correct |
396 ms |
31480 KB |
Output is correct |
34 |
Correct |
276 ms |
25684 KB |
Output is correct |
35 |
Correct |
526 ms |
31020 KB |
Output is correct |
36 |
Correct |
269 ms |
26112 KB |
Output is correct |
37 |
Correct |
603 ms |
30672 KB |
Output is correct |
38 |
Correct |
523 ms |
25944 KB |
Output is correct |
39 |
Correct |
309 ms |
35312 KB |
Output is correct |
40 |
Correct |
456 ms |
29936 KB |
Output is correct |
41 |
Correct |
0 ms |
19440 KB |
Output is correct |
42 |
Correct |
3 ms |
19440 KB |
Output is correct |
43 |
Correct |
9 ms |
19440 KB |
Output is correct |
44 |
Correct |
3 ms |
19440 KB |
Output is correct |
45 |
Correct |
3 ms |
19440 KB |
Output is correct |
46 |
Correct |
6 ms |
19440 KB |
Output is correct |
47 |
Correct |
3 ms |
19440 KB |
Output is correct |
48 |
Correct |
0 ms |
19440 KB |
Output is correct |
49 |
Correct |
0 ms |
19440 KB |
Output is correct |
50 |
Correct |
0 ms |
19440 KB |
Output is correct |
51 |
Correct |
476 ms |
25964 KB |
Output is correct |
52 |
Correct |
323 ms |
35312 KB |
Output is correct |
53 |
Correct |
589 ms |
30056 KB |
Output is correct |
54 |
Correct |
453 ms |
25608 KB |
Output is correct |
55 |
Correct |
386 ms |
26084 KB |
Output is correct |
56 |
Correct |
343 ms |
35308 KB |
Output is correct |
57 |
Correct |
606 ms |
30524 KB |
Output is correct |
58 |
Correct |
279 ms |
25528 KB |
Output is correct |
59 |
Correct |
363 ms |
26024 KB |
Output is correct |
60 |
Correct |
309 ms |
35308 KB |
Output is correct |
61 |
Correct |
509 ms |
30644 KB |
Output is correct |
62 |
Correct |
246 ms |
26148 KB |
Output is correct |
63 |
Correct |
236 ms |
26196 KB |
Output is correct |
64 |
Correct |
309 ms |
35312 KB |
Output is correct |
65 |
Correct |
493 ms |
30804 KB |
Output is correct |
66 |
Correct |
436 ms |
25748 KB |
Output is correct |
67 |
Correct |
456 ms |
26616 KB |
Output is correct |
68 |
Correct |
323 ms |
35316 KB |
Output is correct |
69 |
Correct |
596 ms |
29264 KB |
Output is correct |
70 |
Correct |
313 ms |
25772 KB |
Output is correct |