#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){
vector<pii> vc;
while(1){
if(a==num)break;
if(col[a]==col[num]){
vc.eb(eul[num]+1, eul[a]);
break;
}
vc.eb(eul[ctp[col[a]]], eul[a]);
a=p[ctp[col[a]]];
}
while(1){
if(b==num)break;
if(col[b]==col[num]){
vc.eb(eul[num]+1, eul[b]);
break;
}
vc.eb(eul[ctp[col[b]]], eul[b]);
b=p[ctp[col[b]]];
}
int st=eul[num];
sort(all(vc));
LL ret=0;
for(auto i:vc){
ret+=fen.sum(i.F-1)-fen.sum(st);
st=i.S;
}
ret+=fen.sum(fin[num])-fen.sum(st);
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:119:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
119 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
election_campaign.cpp:122:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
122 | scanf("%d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~
election_campaign.cpp:129:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
129 | scanf("%d", &m);
| ~~~~~^~~~~~~~~~
election_campaign.cpp:132:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
132 | scanf("%d %d %lld", &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7552 KB |
Output is correct |
5 |
Correct |
115 ms |
17528 KB |
Output is correct |
6 |
Correct |
65 ms |
26488 KB |
Output is correct |
7 |
Correct |
101 ms |
23160 KB |
Output is correct |
8 |
Correct |
76 ms |
17016 KB |
Output is correct |
9 |
Correct |
103 ms |
21472 KB |
Output is correct |
10 |
Correct |
74 ms |
16888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7424 KB |
Output is correct |
2 |
Correct |
6 ms |
7424 KB |
Output is correct |
3 |
Correct |
6 ms |
7680 KB |
Output is correct |
4 |
Correct |
131 ms |
30456 KB |
Output is correct |
5 |
Correct |
131 ms |
30584 KB |
Output is correct |
6 |
Correct |
130 ms |
30456 KB |
Output is correct |
7 |
Correct |
132 ms |
30584 KB |
Output is correct |
8 |
Correct |
132 ms |
30456 KB |
Output is correct |
9 |
Correct |
132 ms |
30456 KB |
Output is correct |
10 |
Correct |
132 ms |
30328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7424 KB |
Output is correct |
2 |
Correct |
6 ms |
7424 KB |
Output is correct |
3 |
Correct |
6 ms |
7680 KB |
Output is correct |
4 |
Correct |
131 ms |
30456 KB |
Output is correct |
5 |
Correct |
131 ms |
30584 KB |
Output is correct |
6 |
Correct |
130 ms |
30456 KB |
Output is correct |
7 |
Correct |
132 ms |
30584 KB |
Output is correct |
8 |
Correct |
132 ms |
30456 KB |
Output is correct |
9 |
Correct |
132 ms |
30456 KB |
Output is correct |
10 |
Correct |
132 ms |
30328 KB |
Output is correct |
11 |
Correct |
19 ms |
8568 KB |
Output is correct |
12 |
Correct |
136 ms |
30712 KB |
Output is correct |
13 |
Correct |
135 ms |
30712 KB |
Output is correct |
14 |
Correct |
135 ms |
30844 KB |
Output is correct |
15 |
Correct |
138 ms |
30712 KB |
Output is correct |
16 |
Correct |
141 ms |
30784 KB |
Output is correct |
17 |
Correct |
133 ms |
30712 KB |
Output is correct |
18 |
Correct |
143 ms |
30660 KB |
Output is correct |
19 |
Correct |
135 ms |
30712 KB |
Output is correct |
20 |
Correct |
140 ms |
30712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
269 ms |
20968 KB |
Output is correct |
2 |
Correct |
129 ms |
30456 KB |
Output is correct |
3 |
Correct |
197 ms |
26612 KB |
Output is correct |
4 |
Correct |
169 ms |
20464 KB |
Output is correct |
5 |
Correct |
193 ms |
26100 KB |
Output is correct |
6 |
Correct |
163 ms |
20196 KB |
Output is correct |
7 |
Correct |
197 ms |
25968 KB |
Output is correct |
8 |
Correct |
229 ms |
21112 KB |
Output is correct |
9 |
Correct |
128 ms |
30456 KB |
Output is correct |
10 |
Correct |
189 ms |
24940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7552 KB |
Output is correct |
5 |
Correct |
115 ms |
17528 KB |
Output is correct |
6 |
Correct |
65 ms |
26488 KB |
Output is correct |
7 |
Correct |
101 ms |
23160 KB |
Output is correct |
8 |
Correct |
76 ms |
17016 KB |
Output is correct |
9 |
Correct |
103 ms |
21472 KB |
Output is correct |
10 |
Correct |
74 ms |
16888 KB |
Output is correct |
11 |
Correct |
6 ms |
7552 KB |
Output is correct |
12 |
Correct |
6 ms |
7680 KB |
Output is correct |
13 |
Correct |
7 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 |
7 ms |
7552 KB |
Output is correct |
19 |
Correct |
6 ms |
7552 KB |
Output is correct |
20 |
Correct |
6 ms |
7680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7424 KB |
Output is correct |
2 |
Correct |
5 ms |
7424 KB |
Output is correct |
3 |
Correct |
5 ms |
7424 KB |
Output is correct |
4 |
Correct |
5 ms |
7552 KB |
Output is correct |
5 |
Correct |
115 ms |
17528 KB |
Output is correct |
6 |
Correct |
65 ms |
26488 KB |
Output is correct |
7 |
Correct |
101 ms |
23160 KB |
Output is correct |
8 |
Correct |
76 ms |
17016 KB |
Output is correct |
9 |
Correct |
103 ms |
21472 KB |
Output is correct |
10 |
Correct |
74 ms |
16888 KB |
Output is correct |
11 |
Correct |
6 ms |
7424 KB |
Output is correct |
12 |
Correct |
6 ms |
7424 KB |
Output is correct |
13 |
Correct |
6 ms |
7680 KB |
Output is correct |
14 |
Correct |
131 ms |
30456 KB |
Output is correct |
15 |
Correct |
131 ms |
30584 KB |
Output is correct |
16 |
Correct |
130 ms |
30456 KB |
Output is correct |
17 |
Correct |
132 ms |
30584 KB |
Output is correct |
18 |
Correct |
132 ms |
30456 KB |
Output is correct |
19 |
Correct |
132 ms |
30456 KB |
Output is correct |
20 |
Correct |
132 ms |
30328 KB |
Output is correct |
21 |
Correct |
19 ms |
8568 KB |
Output is correct |
22 |
Correct |
136 ms |
30712 KB |
Output is correct |
23 |
Correct |
135 ms |
30712 KB |
Output is correct |
24 |
Correct |
135 ms |
30844 KB |
Output is correct |
25 |
Correct |
138 ms |
30712 KB |
Output is correct |
26 |
Correct |
141 ms |
30784 KB |
Output is correct |
27 |
Correct |
133 ms |
30712 KB |
Output is correct |
28 |
Correct |
143 ms |
30660 KB |
Output is correct |
29 |
Correct |
135 ms |
30712 KB |
Output is correct |
30 |
Correct |
140 ms |
30712 KB |
Output is correct |
31 |
Correct |
269 ms |
20968 KB |
Output is correct |
32 |
Correct |
129 ms |
30456 KB |
Output is correct |
33 |
Correct |
197 ms |
26612 KB |
Output is correct |
34 |
Correct |
169 ms |
20464 KB |
Output is correct |
35 |
Correct |
193 ms |
26100 KB |
Output is correct |
36 |
Correct |
163 ms |
20196 KB |
Output is correct |
37 |
Correct |
197 ms |
25968 KB |
Output is correct |
38 |
Correct |
229 ms |
21112 KB |
Output is correct |
39 |
Correct |
128 ms |
30456 KB |
Output is correct |
40 |
Correct |
189 ms |
24940 KB |
Output is correct |
41 |
Correct |
6 ms |
7552 KB |
Output is correct |
42 |
Correct |
6 ms |
7680 KB |
Output is correct |
43 |
Correct |
7 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 |
7 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 |
238 ms |
21752 KB |
Output is correct |
52 |
Correct |
132 ms |
30712 KB |
Output is correct |
53 |
Correct |
196 ms |
25356 KB |
Output is correct |
54 |
Correct |
156 ms |
20716 KB |
Output is correct |
55 |
Correct |
274 ms |
21096 KB |
Output is correct |
56 |
Correct |
139 ms |
30812 KB |
Output is correct |
57 |
Correct |
194 ms |
26100 KB |
Output is correct |
58 |
Correct |
162 ms |
20456 KB |
Output is correct |
59 |
Correct |
237 ms |
21488 KB |
Output is correct |
60 |
Correct |
135 ms |
30712 KB |
Output is correct |
61 |
Correct |
190 ms |
25972 KB |
Output is correct |
62 |
Correct |
174 ms |
20260 KB |
Output is correct |
63 |
Correct |
272 ms |
20856 KB |
Output is correct |
64 |
Correct |
137 ms |
30712 KB |
Output is correct |
65 |
Correct |
203 ms |
25864 KB |
Output is correct |
66 |
Correct |
157 ms |
20428 KB |
Output is correct |
67 |
Correct |
278 ms |
20964 KB |
Output is correct |
68 |
Correct |
136 ms |
30712 KB |
Output is correct |
69 |
Correct |
195 ms |
24768 KB |
Output is correct |
70 |
Correct |
160 ms |
20248 KB |
Output is correct |