#include <bits/stdc++.h>
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
struct FENWICK{
LL tree[100010];
LL sum(int i){
LL ans=0;
while(i){
ans+=tree[i];
i-=(i&-i);
}
return ans;
}
void update(int i, LL num){
while(i<=100000){
tree[i]+=num;
i+=(i&-i);
}
}
}fen;
int n, m;
vector<int> inp[100010], link[100010];
vector<pair<pii, LL> > qu[100010];
int sz[100010], d[100010], p[100010];
void dfs1(int num, int par){
sz[num]=1;
p[num]=par;
d[num]=d[par]+1;
for(auto i:inp[num]){
if(i==par)continue;
dfs1(i, num);
link[num].eb(i);
sz[num]+=sz[i];
}
}
int eul[100010], fin[100010], re;
int col[100010], ctp[100010], cre;
LL dp[100010];
bool cmp(int a, int b){return sz[a]>sz[b];}
void dfs2(int num, int c){
sort(all(link[num]), cmp);
eul[num]=fin[num]=++re;
col[num]=c;
for(int i=0; i<link[num].size(); i++){
if(i){
ctp[++cre]=link[num][i];
dfs2(link[num][i], cre);
}
else dfs2(link[num][i], c);
fin[num]=fin[link[num][i]];
}
}
int lca(int a, int b){
if(col[a]==col[b]){
if(eul[a]<eul[b])return a;
return b;
}
if(col[a]>col[b])swap(a, b);
return lca(a, p[ctp[col[b]]]);
}
LL query(int num, int a, int b){
LL ret=fen.sum(fin[num])-fen.sum(eul[num]);
while(1){
if(a==num)break;
if(col[a]==col[num]){
ret-=fen.sum(eul[a])-fen.sum(eul[num]);
break;
}
ret-=fen.sum(eul[a])-fen.sum(eul[ctp[col[a]]]-1);
a=p[ctp[col[a]]];
}
while(1){
if(b==num)break;
if(col[b]==col[num]){
ret-=fen.sum(eul[b])-fen.sum(eul[num]);
break;
}
ret-=fen.sum(eul[b])-fen.sum(eul[ctp[col[b]]]-1);
b=p[ctp[col[b]]];
}
return ret;
}
void dfs3(int num){
LL tmp=0;
for(auto i:link[num]){
dfs3(i);
tmp+=dp[i];
}
dp[num]=tmp;
for(auto i:qu[num]){
LL tmp2=query(num, i.F.F, i.F.S);
dp[num]=max(dp[num], tmp2+i.S);
}
fen.update(eul[num], dp[num]-tmp);
}
int main(){
scanf("%d", &n);
for(int i=1; i<n; i++){
int a, b;
scanf("%d %d", &a, &b);
inp[a].eb(b);
inp[b].eb(a);
}
dfs1(1, 0);
ctp[++cre]=1;
dfs2(1, cre);
scanf("%d", &m);
for(int i=1; i<=m; i++){
int a, b; LL c;
scanf("%d %d %lld", &a, &b, &c);
qu[lca(a, b)].eb(mp(a, b), c);
}
dfs3(1);
printf("%lld", dp[1]);
}
Compilation message
election_campaign.cpp: In function 'void dfs2(int, int)':
election_campaign.cpp:55:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i=0; i<link[num].size(); i++){
| ~^~~~~~~~~~~~~~~~~
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:111:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
111 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
election_campaign.cpp:114:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
114 | scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
election_campaign.cpp:121:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
121 | scanf("%d", &m);
| ~~~~~^~~~~~~~~~
election_campaign.cpp:124:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
124 | scanf("%d %d %lld", &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
6 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
6 ms |
7552 KB |
Output is correct |
5 |
Correct |
116 ms |
16324 KB |
Output is correct |
6 |
Correct |
65 ms |
25336 KB |
Output is correct |
7 |
Correct |
105 ms |
22008 KB |
Output is correct |
8 |
Correct |
75 ms |
15736 KB |
Output is correct |
9 |
Correct |
105 ms |
20472 KB |
Output is correct |
10 |
Correct |
80 ms |
15812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
6 ms |
7552 KB |
Output is correct |
4 |
Correct |
127 ms |
27896 KB |
Output is correct |
5 |
Correct |
125 ms |
28280 KB |
Output is correct |
6 |
Correct |
125 ms |
28280 KB |
Output is correct |
7 |
Correct |
126 ms |
28280 KB |
Output is correct |
8 |
Correct |
123 ms |
28280 KB |
Output is correct |
9 |
Correct |
120 ms |
28408 KB |
Output is correct |
10 |
Correct |
120 ms |
28280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
6 ms |
7552 KB |
Output is correct |
4 |
Correct |
127 ms |
27896 KB |
Output is correct |
5 |
Correct |
125 ms |
28280 KB |
Output is correct |
6 |
Correct |
125 ms |
28280 KB |
Output is correct |
7 |
Correct |
126 ms |
28280 KB |
Output is correct |
8 |
Correct |
123 ms |
28280 KB |
Output is correct |
9 |
Correct |
120 ms |
28408 KB |
Output is correct |
10 |
Correct |
120 ms |
28280 KB |
Output is correct |
11 |
Correct |
16 ms |
8448 KB |
Output is correct |
12 |
Correct |
124 ms |
28280 KB |
Output is correct |
13 |
Correct |
134 ms |
28280 KB |
Output is correct |
14 |
Correct |
127 ms |
28408 KB |
Output is correct |
15 |
Correct |
132 ms |
28384 KB |
Output is correct |
16 |
Correct |
123 ms |
28408 KB |
Output is correct |
17 |
Correct |
129 ms |
28280 KB |
Output is correct |
18 |
Correct |
129 ms |
28280 KB |
Output is correct |
19 |
Correct |
131 ms |
28408 KB |
Output is correct |
20 |
Correct |
126 ms |
28364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
219 ms |
18412 KB |
Output is correct |
2 |
Correct |
120 ms |
28024 KB |
Output is correct |
3 |
Correct |
182 ms |
24564 KB |
Output is correct |
4 |
Correct |
137 ms |
18288 KB |
Output is correct |
5 |
Correct |
178 ms |
24128 KB |
Output is correct |
6 |
Correct |
137 ms |
18068 KB |
Output is correct |
7 |
Correct |
179 ms |
23792 KB |
Output is correct |
8 |
Correct |
212 ms |
19064 KB |
Output is correct |
9 |
Correct |
128 ms |
28280 KB |
Output is correct |
10 |
Correct |
174 ms |
22760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
6 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
6 ms |
7552 KB |
Output is correct |
5 |
Correct |
116 ms |
16324 KB |
Output is correct |
6 |
Correct |
65 ms |
25336 KB |
Output is correct |
7 |
Correct |
105 ms |
22008 KB |
Output is correct |
8 |
Correct |
75 ms |
15736 KB |
Output is correct |
9 |
Correct |
105 ms |
20472 KB |
Output is correct |
10 |
Correct |
80 ms |
15812 KB |
Output is correct |
11 |
Correct |
6 ms |
7552 KB |
Output is correct |
12 |
Correct |
7 ms |
7680 KB |
Output is correct |
13 |
Correct |
6 ms |
7552 KB |
Output is correct |
14 |
Correct |
6 ms |
7552 KB |
Output is correct |
15 |
Correct |
7 ms |
7552 KB |
Output is correct |
16 |
Correct |
6 ms |
7552 KB |
Output is correct |
17 |
Correct |
6 ms |
7552 KB |
Output is correct |
18 |
Correct |
6 ms |
7552 KB |
Output is correct |
19 |
Correct |
6 ms |
7552 KB |
Output is correct |
20 |
Correct |
6 ms |
7680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
7424 KB |
Output is correct |
2 |
Correct |
6 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
6 ms |
7552 KB |
Output is correct |
5 |
Correct |
116 ms |
16324 KB |
Output is correct |
6 |
Correct |
65 ms |
25336 KB |
Output is correct |
7 |
Correct |
105 ms |
22008 KB |
Output is correct |
8 |
Correct |
75 ms |
15736 KB |
Output is correct |
9 |
Correct |
105 ms |
20472 KB |
Output is correct |
10 |
Correct |
80 ms |
15812 KB |
Output is correct |
11 |
Correct |
6 ms |
7424 KB |
Output is correct |
12 |
Correct |
5 ms |
7424 KB |
Output is correct |
13 |
Correct |
6 ms |
7552 KB |
Output is correct |
14 |
Correct |
127 ms |
27896 KB |
Output is correct |
15 |
Correct |
125 ms |
28280 KB |
Output is correct |
16 |
Correct |
125 ms |
28280 KB |
Output is correct |
17 |
Correct |
126 ms |
28280 KB |
Output is correct |
18 |
Correct |
123 ms |
28280 KB |
Output is correct |
19 |
Correct |
120 ms |
28408 KB |
Output is correct |
20 |
Correct |
120 ms |
28280 KB |
Output is correct |
21 |
Correct |
16 ms |
8448 KB |
Output is correct |
22 |
Correct |
124 ms |
28280 KB |
Output is correct |
23 |
Correct |
134 ms |
28280 KB |
Output is correct |
24 |
Correct |
127 ms |
28408 KB |
Output is correct |
25 |
Correct |
132 ms |
28384 KB |
Output is correct |
26 |
Correct |
123 ms |
28408 KB |
Output is correct |
27 |
Correct |
129 ms |
28280 KB |
Output is correct |
28 |
Correct |
129 ms |
28280 KB |
Output is correct |
29 |
Correct |
131 ms |
28408 KB |
Output is correct |
30 |
Correct |
126 ms |
28364 KB |
Output is correct |
31 |
Correct |
219 ms |
18412 KB |
Output is correct |
32 |
Correct |
120 ms |
28024 KB |
Output is correct |
33 |
Correct |
182 ms |
24564 KB |
Output is correct |
34 |
Correct |
137 ms |
18288 KB |
Output is correct |
35 |
Correct |
178 ms |
24128 KB |
Output is correct |
36 |
Correct |
137 ms |
18068 KB |
Output is correct |
37 |
Correct |
179 ms |
23792 KB |
Output is correct |
38 |
Correct |
212 ms |
19064 KB |
Output is correct |
39 |
Correct |
128 ms |
28280 KB |
Output is correct |
40 |
Correct |
174 ms |
22760 KB |
Output is correct |
41 |
Correct |
6 ms |
7552 KB |
Output is correct |
42 |
Correct |
7 ms |
7680 KB |
Output is correct |
43 |
Correct |
6 ms |
7552 KB |
Output is correct |
44 |
Correct |
6 ms |
7552 KB |
Output is correct |
45 |
Correct |
7 ms |
7552 KB |
Output is correct |
46 |
Correct |
6 ms |
7552 KB |
Output is correct |
47 |
Correct |
6 ms |
7552 KB |
Output is correct |
48 |
Correct |
6 ms |
7552 KB |
Output is correct |
49 |
Correct |
6 ms |
7552 KB |
Output is correct |
50 |
Correct |
6 ms |
7680 KB |
Output is correct |
51 |
Correct |
211 ms |
19132 KB |
Output is correct |
52 |
Correct |
128 ms |
28280 KB |
Output is correct |
53 |
Correct |
188 ms |
22892 KB |
Output is correct |
54 |
Correct |
140 ms |
18160 KB |
Output is correct |
55 |
Correct |
224 ms |
18796 KB |
Output is correct |
56 |
Correct |
129 ms |
28340 KB |
Output is correct |
57 |
Correct |
192 ms |
23540 KB |
Output is correct |
58 |
Correct |
139 ms |
18280 KB |
Output is correct |
59 |
Correct |
208 ms |
19056 KB |
Output is correct |
60 |
Correct |
128 ms |
28280 KB |
Output is correct |
61 |
Correct |
180 ms |
23796 KB |
Output is correct |
62 |
Correct |
142 ms |
17844 KB |
Output is correct |
63 |
Correct |
218 ms |
18424 KB |
Output is correct |
64 |
Correct |
126 ms |
28280 KB |
Output is correct |
65 |
Correct |
180 ms |
23528 KB |
Output is correct |
66 |
Correct |
143 ms |
18024 KB |
Output is correct |
67 |
Correct |
229 ms |
18660 KB |
Output is correct |
68 |
Correct |
126 ms |
28280 KB |
Output is correct |
69 |
Correct |
178 ms |
22428 KB |
Output is correct |
70 |
Correct |
140 ms |
17944 KB |
Output is correct |