#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])];
//printf("+ %d - %d -> %d\n", i.x, i.y, cret + i.c);
ret = max(ret, cret + i.c);
}
//printf("// %d : %d\n", x, ret);
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_campagin.cpp: In function 'int main()':
election_campagin.cpp:76:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
^
election_campagin.cpp:78:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &x, &y);
^
election_campagin.cpp:83:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &m);
^
election_campagin.cpp:85: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 |
3 ms |
19308 KB |
Output is correct |
2 |
Correct |
0 ms |
19308 KB |
Output is correct |
3 |
Correct |
0 ms |
19308 KB |
Output is correct |
4 |
Correct |
0 ms |
19308 KB |
Output is correct |
5 |
Correct |
156 ms |
24192 KB |
Output is correct |
6 |
Correct |
76 ms |
33200 KB |
Output is correct |
7 |
Correct |
233 ms |
29840 KB |
Output is correct |
8 |
Correct |
149 ms |
23856 KB |
Output is correct |
9 |
Correct |
209 ms |
28292 KB |
Output is correct |
10 |
Correct |
143 ms |
23792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
19308 KB |
Output is correct |
2 |
Correct |
3 ms |
19308 KB |
Output is correct |
3 |
Correct |
6 ms |
19440 KB |
Output is correct |
4 |
Correct |
306 ms |
35312 KB |
Output is correct |
5 |
Correct |
303 ms |
35316 KB |
Output is correct |
6 |
Correct |
339 ms |
35312 KB |
Output is correct |
7 |
Correct |
319 ms |
35308 KB |
Output is correct |
8 |
Correct |
333 ms |
35312 KB |
Output is correct |
9 |
Correct |
306 ms |
35308 KB |
Output is correct |
10 |
Correct |
313 ms |
35308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
19308 KB |
Output is correct |
2 |
Correct |
3 ms |
19308 KB |
Output is correct |
3 |
Correct |
6 ms |
19440 KB |
Output is correct |
4 |
Correct |
306 ms |
35312 KB |
Output is correct |
5 |
Correct |
303 ms |
35316 KB |
Output is correct |
6 |
Correct |
339 ms |
35312 KB |
Output is correct |
7 |
Correct |
319 ms |
35308 KB |
Output is correct |
8 |
Correct |
333 ms |
35312 KB |
Output is correct |
9 |
Correct |
306 ms |
35308 KB |
Output is correct |
10 |
Correct |
313 ms |
35308 KB |
Output is correct |
11 |
Correct |
26 ms |
19836 KB |
Output is correct |
12 |
Correct |
346 ms |
35308 KB |
Output is correct |
13 |
Correct |
356 ms |
35316 KB |
Output is correct |
14 |
Correct |
316 ms |
35308 KB |
Output is correct |
15 |
Correct |
293 ms |
35312 KB |
Output is correct |
16 |
Correct |
326 ms |
35308 KB |
Output is correct |
17 |
Correct |
319 ms |
35312 KB |
Output is correct |
18 |
Correct |
366 ms |
35308 KB |
Output is correct |
19 |
Correct |
363 ms |
35316 KB |
Output is correct |
20 |
Correct |
339 ms |
35312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
333 ms |
25888 KB |
Output is correct |
2 |
Correct |
193 ms |
35312 KB |
Output is correct |
3 |
Correct |
523 ms |
31476 KB |
Output is correct |
4 |
Correct |
246 ms |
25684 KB |
Output is correct |
5 |
Correct |
546 ms |
31020 KB |
Output is correct |
6 |
Correct |
319 ms |
26112 KB |
Output is correct |
7 |
Correct |
496 ms |
30668 KB |
Output is correct |
8 |
Correct |
469 ms |
25944 KB |
Output is correct |
9 |
Correct |
323 ms |
35308 KB |
Output is correct |
10 |
Correct |
576 ms |
29936 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 |
0 ms |
19308 KB |
Output is correct |
4 |
Correct |
0 ms |
19308 KB |
Output is correct |
5 |
Correct |
156 ms |
24192 KB |
Output is correct |
6 |
Correct |
76 ms |
33200 KB |
Output is correct |
7 |
Correct |
233 ms |
29840 KB |
Output is correct |
8 |
Correct |
149 ms |
23856 KB |
Output is correct |
9 |
Correct |
209 ms |
28292 KB |
Output is correct |
10 |
Correct |
143 ms |
23792 KB |
Output is correct |
11 |
Correct |
3 ms |
19440 KB |
Output is correct |
12 |
Correct |
6 ms |
19440 KB |
Output is correct |
13 |
Correct |
6 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 |
3 ms |
19440 KB |
Output is correct |
17 |
Correct |
3 ms |
19440 KB |
Output is correct |
18 |
Correct |
6 ms |
19440 KB |
Output is correct |
19 |
Correct |
6 ms |
19440 KB |
Output is correct |
20 |
Correct |
3 ms |
19440 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 |
0 ms |
19308 KB |
Output is correct |
4 |
Correct |
0 ms |
19308 KB |
Output is correct |
5 |
Correct |
156 ms |
24192 KB |
Output is correct |
6 |
Correct |
76 ms |
33200 KB |
Output is correct |
7 |
Correct |
233 ms |
29840 KB |
Output is correct |
8 |
Correct |
149 ms |
23856 KB |
Output is correct |
9 |
Correct |
209 ms |
28292 KB |
Output is correct |
10 |
Correct |
143 ms |
23792 KB |
Output is correct |
11 |
Correct |
0 ms |
19308 KB |
Output is correct |
12 |
Correct |
3 ms |
19308 KB |
Output is correct |
13 |
Correct |
6 ms |
19440 KB |
Output is correct |
14 |
Correct |
306 ms |
35312 KB |
Output is correct |
15 |
Correct |
303 ms |
35316 KB |
Output is correct |
16 |
Correct |
339 ms |
35312 KB |
Output is correct |
17 |
Correct |
319 ms |
35308 KB |
Output is correct |
18 |
Correct |
333 ms |
35312 KB |
Output is correct |
19 |
Correct |
306 ms |
35308 KB |
Output is correct |
20 |
Correct |
313 ms |
35308 KB |
Output is correct |
21 |
Correct |
26 ms |
19836 KB |
Output is correct |
22 |
Correct |
346 ms |
35308 KB |
Output is correct |
23 |
Correct |
356 ms |
35316 KB |
Output is correct |
24 |
Correct |
316 ms |
35308 KB |
Output is correct |
25 |
Correct |
293 ms |
35312 KB |
Output is correct |
26 |
Correct |
326 ms |
35308 KB |
Output is correct |
27 |
Correct |
319 ms |
35312 KB |
Output is correct |
28 |
Correct |
366 ms |
35308 KB |
Output is correct |
29 |
Correct |
363 ms |
35316 KB |
Output is correct |
30 |
Correct |
339 ms |
35312 KB |
Output is correct |
31 |
Correct |
333 ms |
25888 KB |
Output is correct |
32 |
Correct |
193 ms |
35312 KB |
Output is correct |
33 |
Correct |
523 ms |
31476 KB |
Output is correct |
34 |
Correct |
246 ms |
25684 KB |
Output is correct |
35 |
Correct |
546 ms |
31020 KB |
Output is correct |
36 |
Correct |
319 ms |
26112 KB |
Output is correct |
37 |
Correct |
496 ms |
30668 KB |
Output is correct |
38 |
Correct |
469 ms |
25944 KB |
Output is correct |
39 |
Correct |
323 ms |
35308 KB |
Output is correct |
40 |
Correct |
576 ms |
29936 KB |
Output is correct |
41 |
Correct |
3 ms |
19440 KB |
Output is correct |
42 |
Correct |
6 ms |
19440 KB |
Output is correct |
43 |
Correct |
6 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 |
3 ms |
19440 KB |
Output is correct |
47 |
Correct |
3 ms |
19440 KB |
Output is correct |
48 |
Correct |
6 ms |
19440 KB |
Output is correct |
49 |
Correct |
6 ms |
19440 KB |
Output is correct |
50 |
Correct |
3 ms |
19440 KB |
Output is correct |
51 |
Correct |
469 ms |
25964 KB |
Output is correct |
52 |
Correct |
299 ms |
35316 KB |
Output is correct |
53 |
Correct |
506 ms |
30060 KB |
Output is correct |
54 |
Correct |
373 ms |
25608 KB |
Output is correct |
55 |
Correct |
356 ms |
26084 KB |
Output is correct |
56 |
Correct |
293 ms |
35312 KB |
Output is correct |
57 |
Correct |
613 ms |
30516 KB |
Output is correct |
58 |
Correct |
313 ms |
25528 KB |
Output is correct |
59 |
Correct |
453 ms |
26024 KB |
Output is correct |
60 |
Correct |
323 ms |
35316 KB |
Output is correct |
61 |
Correct |
596 ms |
30652 KB |
Output is correct |
62 |
Correct |
289 ms |
26148 KB |
Output is correct |
63 |
Correct |
316 ms |
26196 KB |
Output is correct |
64 |
Correct |
269 ms |
35316 KB |
Output is correct |
65 |
Correct |
556 ms |
30796 KB |
Output is correct |
66 |
Correct |
373 ms |
25748 KB |
Output is correct |
67 |
Correct |
336 ms |
26616 KB |
Output is correct |
68 |
Correct |
323 ms |
35308 KB |
Output is correct |
69 |
Correct |
419 ms |
29264 KB |
Output is correct |
70 |
Correct |
299 ms |
25772 KB |
Output is correct |