#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct dat{
int x, y, z;
dat(){}
dat(int x, int y, int z): x(x), y(y), z(z){}
};
int n, k;
vector<int> link[100002];
int A[100002], B[100002], C[100002];
vector<dat> path[100002];
int par[100002], LCADB[100002][20], depth[100002];
int sps[100002][20];
int DP[100002], SUM[100002];
vector<pair<int, int> > spsList[100002];
void input(){
scanf("%d", &n);
for(int i=1; i<n; i++){
int x, y;
scanf("%d %d", &x, &y);
link[x].push_back(y);
link[y].push_back(x);
}
scanf("%d", &k);
for(int i=1; i<=k; i++){
scanf("%d %d %d", &A[i], &B[i], &C[i]);
}
}
void dfs(int x, int _p = 0){
par[x] = _p;
vector<int> tmpv;
for(auto &y: link[x]){
if(y == _p) continue;
tmpv.push_back(y);
depth[y] = depth[x]+1;
dfs(y, x);
}
link[x] = tmpv;
}
void generateTree(){
dfs(1);
for(int i=1; i<=n; i++) LCADB[i][0] = par[i];
for(int d=1; d<20; d++){
for(int i=1; i<=n; i++) LCADB[i][d] = LCADB[LCADB[i][d-1]][d-1];
}
for(int i=1; i<=n; i++){
for(int j=0; j<20; j++){
if(LCADB[i][j]) spsList[LCADB[i][j]].push_back({i, j});
}
}
}
int LCA(int x, int y){
if(depth[x]>depth[y]) swap(x, y);
for(int d=0; d<20; d++) if((depth[y] - depth[x]) & (1<<d)) y = LCADB[y][d];
if(x==y) return x;
for(int d=19; d>=0; d--) if(LCADB[x][d] != LCADB[y][d]) x = LCADB[x][d], y = LCADB[y][d];
return par[x];
}
void dividePaths(){
for(int i=1; i<=k; i++){
path[LCA(A[i], B[i])].push_back(dat(A[i], B[i], C[i]));
}
}
int kthParent(int x, int y){
for(int d=0; d<20; d++) if(y & (1<<d)) x = LCADB[x][d];
return x;
}
int whatSubtree(int x, int y){
return kthParent(y, depth[y] - depth[x] - 1);
}
int getSparse(int x, int y){
int ret = 0;
for(int d=19; d>=0; d--){
if(y & (1<<d)) ret += sps[x][d], x = LCADB[x][d];
}
return ret;
}
void calculate(int x){ /// Here is the place to calculate
int sum = 0;
for(auto &y: link[x]){
calculate(y);
sum += DP[y];
}
DP[x] = SUM[x] = sum;
sort(spsList[x].begin(), spsList[x].end(), [&](pair<int, int> _a, pair<int, int> _b){
return depth[_a.first] < depth[_b.first];
});
for(auto &p: spsList[x]){ /// make sparse table
int px = p.first, py = p.second;
if(py == 0) sps[px][py] = sum - DP[whatSubtree(x, px)];
else{
sps[px][py] = sps[px][py-1] + sps[LCADB[px][py-1]][py-1];
}
}
for(auto &d: path[x]){
int val = sum;
int a = d.x, b = d.y, c = d.z;
if(depth[a] > depth[b]) swap(a, b);
if(a==x){
val -= DP[whatSubtree(x, b)];
val += SUM[b] + getSparse(b, depth[b] - depth[x] - 1);
}
else{
val -= DP[whatSubtree(x, a)] + DP[whatSubtree(x, b)];
val += SUM[a] + getSparse(a, depth[a] - depth[x] - 1);
val += SUM[b] + getSparse(b, depth[b] - depth[x] - 1);
}
DP[x] = max(DP[x], val + c);
}
}
int main(){
input();
generateTree();
dividePaths();
calculate(1);
printf("%d", DP[1]);
}
Compilation message
election_campaign.cpp: In function 'void input()':
election_campaign.cpp:24:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
24 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
election_campaign.cpp:27:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
27 | scanf("%d %d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~
election_campaign.cpp:31:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
31 | scanf("%d", &k);
| ~~~~~^~~~~~~~~~
election_campaign.cpp:33:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
33 | scanf("%d %d %d", &A[i], &B[i], &C[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
6 ms |
7424 KB |
Output is correct |
4 |
Correct |
6 ms |
7680 KB |
Output is correct |
5 |
Correct |
188 ms |
33144 KB |
Output is correct |
6 |
Correct |
167 ms |
55508 KB |
Output is correct |
7 |
Correct |
377 ms |
53624 KB |
Output is correct |
8 |
Correct |
108 ms |
30816 KB |
Output is correct |
9 |
Correct |
403 ms |
51596 KB |
Output is correct |
10 |
Correct |
115 ms |
30812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
6 ms |
7808 KB |
Output is correct |
4 |
Correct |
273 ms |
58616 KB |
Output is correct |
5 |
Correct |
278 ms |
58744 KB |
Output is correct |
6 |
Correct |
250 ms |
58704 KB |
Output is correct |
7 |
Correct |
263 ms |
58616 KB |
Output is correct |
8 |
Correct |
262 ms |
58872 KB |
Output is correct |
9 |
Correct |
252 ms |
58776 KB |
Output is correct |
10 |
Correct |
273 ms |
58872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
6 ms |
7808 KB |
Output is correct |
4 |
Correct |
273 ms |
58616 KB |
Output is correct |
5 |
Correct |
278 ms |
58744 KB |
Output is correct |
6 |
Correct |
250 ms |
58704 KB |
Output is correct |
7 |
Correct |
263 ms |
58616 KB |
Output is correct |
8 |
Correct |
262 ms |
58872 KB |
Output is correct |
9 |
Correct |
252 ms |
58776 KB |
Output is correct |
10 |
Correct |
273 ms |
58872 KB |
Output is correct |
11 |
Correct |
21 ms |
8568 KB |
Output is correct |
12 |
Correct |
266 ms |
58624 KB |
Output is correct |
13 |
Correct |
281 ms |
58616 KB |
Output is correct |
14 |
Correct |
248 ms |
58744 KB |
Output is correct |
15 |
Correct |
268 ms |
58744 KB |
Output is correct |
16 |
Correct |
252 ms |
58708 KB |
Output is correct |
17 |
Correct |
271 ms |
58700 KB |
Output is correct |
18 |
Correct |
264 ms |
58704 KB |
Output is correct |
19 |
Correct |
248 ms |
58744 KB |
Output is correct |
20 |
Correct |
277 ms |
58616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
307 ms |
36400 KB |
Output is correct |
2 |
Correct |
269 ms |
58648 KB |
Output is correct |
3 |
Correct |
602 ms |
54712 KB |
Output is correct |
4 |
Correct |
187 ms |
33380 KB |
Output is correct |
5 |
Correct |
539 ms |
54072 KB |
Output is correct |
6 |
Correct |
182 ms |
33500 KB |
Output is correct |
7 |
Correct |
598 ms |
54104 KB |
Output is correct |
8 |
Correct |
283 ms |
36472 KB |
Output is correct |
9 |
Correct |
244 ms |
58744 KB |
Output is correct |
10 |
Correct |
607 ms |
53232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
6 ms |
7424 KB |
Output is correct |
4 |
Correct |
6 ms |
7680 KB |
Output is correct |
5 |
Correct |
188 ms |
33144 KB |
Output is correct |
6 |
Correct |
167 ms |
55508 KB |
Output is correct |
7 |
Correct |
377 ms |
53624 KB |
Output is correct |
8 |
Correct |
108 ms |
30816 KB |
Output is correct |
9 |
Correct |
403 ms |
51596 KB |
Output is correct |
10 |
Correct |
115 ms |
30812 KB |
Output is correct |
11 |
Correct |
7 ms |
7680 KB |
Output is correct |
12 |
Correct |
7 ms |
7808 KB |
Output is correct |
13 |
Correct |
7 ms |
7808 KB |
Output is correct |
14 |
Correct |
7 ms |
7680 KB |
Output is correct |
15 |
Correct |
8 ms |
7680 KB |
Output is correct |
16 |
Correct |
7 ms |
7808 KB |
Output is correct |
17 |
Correct |
7 ms |
7680 KB |
Output is correct |
18 |
Correct |
7 ms |
7808 KB |
Output is correct |
19 |
Correct |
6 ms |
7680 KB |
Output is correct |
20 |
Correct |
8 ms |
7808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
6 ms |
7424 KB |
Output is correct |
4 |
Correct |
6 ms |
7680 KB |
Output is correct |
5 |
Correct |
188 ms |
33144 KB |
Output is correct |
6 |
Correct |
167 ms |
55508 KB |
Output is correct |
7 |
Correct |
377 ms |
53624 KB |
Output is correct |
8 |
Correct |
108 ms |
30816 KB |
Output is correct |
9 |
Correct |
403 ms |
51596 KB |
Output is correct |
10 |
Correct |
115 ms |
30812 KB |
Output is correct |
11 |
Correct |
5 ms |
7424 KB |
Output is correct |
12 |
Correct |
5 ms |
7424 KB |
Output is correct |
13 |
Correct |
6 ms |
7808 KB |
Output is correct |
14 |
Correct |
273 ms |
58616 KB |
Output is correct |
15 |
Correct |
278 ms |
58744 KB |
Output is correct |
16 |
Correct |
250 ms |
58704 KB |
Output is correct |
17 |
Correct |
263 ms |
58616 KB |
Output is correct |
18 |
Correct |
262 ms |
58872 KB |
Output is correct |
19 |
Correct |
252 ms |
58776 KB |
Output is correct |
20 |
Correct |
273 ms |
58872 KB |
Output is correct |
21 |
Correct |
21 ms |
8568 KB |
Output is correct |
22 |
Correct |
266 ms |
58624 KB |
Output is correct |
23 |
Correct |
281 ms |
58616 KB |
Output is correct |
24 |
Correct |
248 ms |
58744 KB |
Output is correct |
25 |
Correct |
268 ms |
58744 KB |
Output is correct |
26 |
Correct |
252 ms |
58708 KB |
Output is correct |
27 |
Correct |
271 ms |
58700 KB |
Output is correct |
28 |
Correct |
264 ms |
58704 KB |
Output is correct |
29 |
Correct |
248 ms |
58744 KB |
Output is correct |
30 |
Correct |
277 ms |
58616 KB |
Output is correct |
31 |
Correct |
307 ms |
36400 KB |
Output is correct |
32 |
Correct |
269 ms |
58648 KB |
Output is correct |
33 |
Correct |
602 ms |
54712 KB |
Output is correct |
34 |
Correct |
187 ms |
33380 KB |
Output is correct |
35 |
Correct |
539 ms |
54072 KB |
Output is correct |
36 |
Correct |
182 ms |
33500 KB |
Output is correct |
37 |
Correct |
598 ms |
54104 KB |
Output is correct |
38 |
Correct |
283 ms |
36472 KB |
Output is correct |
39 |
Correct |
244 ms |
58744 KB |
Output is correct |
40 |
Correct |
607 ms |
53232 KB |
Output is correct |
41 |
Correct |
7 ms |
7680 KB |
Output is correct |
42 |
Correct |
7 ms |
7808 KB |
Output is correct |
43 |
Correct |
7 ms |
7808 KB |
Output is correct |
44 |
Correct |
7 ms |
7680 KB |
Output is correct |
45 |
Correct |
8 ms |
7680 KB |
Output is correct |
46 |
Correct |
7 ms |
7808 KB |
Output is correct |
47 |
Correct |
7 ms |
7680 KB |
Output is correct |
48 |
Correct |
7 ms |
7808 KB |
Output is correct |
49 |
Correct |
6 ms |
7680 KB |
Output is correct |
50 |
Correct |
8 ms |
7808 KB |
Output is correct |
51 |
Correct |
295 ms |
36600 KB |
Output is correct |
52 |
Correct |
271 ms |
58744 KB |
Output is correct |
53 |
Correct |
596 ms |
53364 KB |
Output is correct |
54 |
Correct |
180 ms |
33244 KB |
Output is correct |
55 |
Correct |
301 ms |
36144 KB |
Output is correct |
56 |
Correct |
281 ms |
58744 KB |
Output is correct |
57 |
Correct |
524 ms |
53436 KB |
Output is correct |
58 |
Correct |
190 ms |
33312 KB |
Output is correct |
59 |
Correct |
287 ms |
36348 KB |
Output is correct |
60 |
Correct |
269 ms |
58616 KB |
Output is correct |
61 |
Correct |
512 ms |
53436 KB |
Output is correct |
62 |
Correct |
184 ms |
33256 KB |
Output is correct |
63 |
Correct |
307 ms |
36244 KB |
Output is correct |
64 |
Correct |
271 ms |
58708 KB |
Output is correct |
65 |
Correct |
618 ms |
53872 KB |
Output is correct |
66 |
Correct |
189 ms |
33420 KB |
Output is correct |
67 |
Correct |
313 ms |
36204 KB |
Output is correct |
68 |
Correct |
287 ms |
58708 KB |
Output is correct |
69 |
Correct |
513 ms |
52156 KB |
Output is correct |
70 |
Correct |
202 ms |
33892 KB |
Output is correct |