#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MN = 1e5+5;
int dp[MN][2], sp[MN][20][2], anc[MN][20], N, M, i, x, y, w, dep[MN];
struct qur{int x, y, w;}path[MN];
vector<int> adj[MN], id[MN];
vector<pair<int,int>> bld[MN];
void init(int n,int p,int d){
dep[n]=d; anc[n][0]=p;
for(auto v : adj[n])
if(v != p) init(v, n, d+1);
}
int lca(int x,int y){
if(dep[x]<dep[y]) swap(x,y);
for(int i=19;i>=0;i--){
if(dep[x]-dep[y]>=(1<<i))
x = anc[x][i];
}
if(x == y) return x;
for(int i=19;i>=0;i--){
if(anc[x][i]!=anc[y][i]){
x = anc[x][i];
y = anc[y][i];
}
}
return anc[x][0];
}
int qu(int x,int y,int id){
int res = 0;
if(dep[x]<dep[y]) swap(x,y);
for(int i=19;i>=0;i--){
if(dep[x]-dep[y]>=(1<<i))
res += sp[x][i][id], x = anc[x][i];
}
if(x == y) return res+dp[x][id];
for(int i=19;i>=0;i--){
if(anc[x][i]!=anc[y][i]){
res += sp[x][i][id]+sp[y][i][id];
x = anc[x][i], y = anc[y][i];
}
}
return res+sp[x][0][id]+sp[y][0][id]+dp[anc[x][0]][id];
}
int adv(int n,int d){
for(int i=19;i>=0;i--){
if((1<<i)<=d)
n=anc[n][i],d-=(1<<i);
}
return n;
}
void solve(int n,int p){
for(int i=1;i<20;i++)
bld[adv(n,(1<<i)-1)].push_back({n,i});
for(auto v : adj[n]){
if(v == p) continue;
solve(v, n);
dp[n][1] += dp[v][0];
}
dp[n][0]=dp[n][1];
for(auto i : id[n]){
int x = path[i].x, y = path[i].y, w = path[i].w;
dp[n][0]=max(dp[n][0],qu(x,y,1)-qu(x,y,0)+dp[n][0]+w);
}
sp[n][0][0]=dp[n][0];
sp[n][0][1]=dp[n][1];
sort(bld[n].begin(),bld[n].end(),[](pair<int,int>i,pair<int,int>j){return dep[i.first]<dep[j.first];});
for(auto v : bld[n]){
for(int i=0;i<2;i++)
sp[v.first][v.second][i]=sp[v.first][v.second-1][i]+sp[anc[v.first][v.second-1]][v.second-1][i];
}
}
signed main(){
for(scanf("%lld",&N),i=1;i<N;i++){
scanf("%lld%lld",&x,&y);
adj[x].push_back(y);
adj[y].push_back(x);
}
init(1, 0, 1);
for(int j=1;j<20;j++){
for(i=1;i<=N;i++)
anc[i][j]=anc[anc[i][j-1]][j-1];
}
for(scanf("%lld",&M),i=1;i<=M;i++){
scanf("%lld%lld%lld",&x,&y,&w);
path[i] = {x, y, w};
id[lca(x,y)].push_back(i);
}
solve(1, 0);
printf("%lld\n",dp[1][0]);
return 0;
}
Compilation message
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:75:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(scanf("%lld",&N),i=1;i<N;i++){
~~~~~~~~~~~~~~~~^~~~
election_campaign.cpp:76:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld",&x,&y);
~~~~~^~~~~~~~~~~~~~~~~~
election_campaign.cpp:85:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(scanf("%lld",&M),i=1;i<=M;i++){
~~~~~~~~~~~~~~~~^~~~
election_campaign.cpp:86:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld%lld",&x,&y,&w);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
Output is correct |
2 |
Correct |
8 ms |
7548 KB |
Output is correct |
3 |
Correct |
8 ms |
7584 KB |
Output is correct |
4 |
Correct |
10 ms |
8744 KB |
Output is correct |
5 |
Correct |
368 ms |
99808 KB |
Output is correct |
6 |
Correct |
513 ms |
108304 KB |
Output is correct |
7 |
Correct |
888 ms |
115692 KB |
Output is correct |
8 |
Correct |
312 ms |
115692 KB |
Output is correct |
9 |
Correct |
698 ms |
117612 KB |
Output is correct |
10 |
Correct |
314 ms |
117612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
117612 KB |
Output is correct |
2 |
Correct |
9 ms |
117612 KB |
Output is correct |
3 |
Correct |
10 ms |
117612 KB |
Output is correct |
4 |
Correct |
660 ms |
119392 KB |
Output is correct |
5 |
Correct |
650 ms |
121888 KB |
Output is correct |
6 |
Correct |
606 ms |
124468 KB |
Output is correct |
7 |
Correct |
638 ms |
126808 KB |
Output is correct |
8 |
Correct |
642 ms |
129376 KB |
Output is correct |
9 |
Correct |
650 ms |
131860 KB |
Output is correct |
10 |
Correct |
686 ms |
134308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
117612 KB |
Output is correct |
2 |
Correct |
9 ms |
117612 KB |
Output is correct |
3 |
Correct |
10 ms |
117612 KB |
Output is correct |
4 |
Correct |
660 ms |
119392 KB |
Output is correct |
5 |
Correct |
650 ms |
121888 KB |
Output is correct |
6 |
Correct |
606 ms |
124468 KB |
Output is correct |
7 |
Correct |
638 ms |
126808 KB |
Output is correct |
8 |
Correct |
642 ms |
129376 KB |
Output is correct |
9 |
Correct |
650 ms |
131860 KB |
Output is correct |
10 |
Correct |
686 ms |
134308 KB |
Output is correct |
11 |
Correct |
29 ms |
134308 KB |
Output is correct |
12 |
Correct |
668 ms |
137160 KB |
Output is correct |
13 |
Correct |
684 ms |
139936 KB |
Output is correct |
14 |
Correct |
630 ms |
142796 KB |
Output is correct |
15 |
Correct |
655 ms |
145544 KB |
Output is correct |
16 |
Correct |
641 ms |
148288 KB |
Output is correct |
17 |
Correct |
654 ms |
150896 KB |
Output is correct |
18 |
Correct |
774 ms |
153636 KB |
Output is correct |
19 |
Correct |
707 ms |
156428 KB |
Output is correct |
20 |
Correct |
697 ms |
159168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
524 ms |
159168 KB |
Output is correct |
2 |
Correct |
720 ms |
161716 KB |
Output is correct |
3 |
Execution timed out |
1034 ms |
176384 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
Output is correct |
2 |
Correct |
8 ms |
7548 KB |
Output is correct |
3 |
Correct |
8 ms |
7584 KB |
Output is correct |
4 |
Correct |
10 ms |
8744 KB |
Output is correct |
5 |
Correct |
368 ms |
99808 KB |
Output is correct |
6 |
Correct |
513 ms |
108304 KB |
Output is correct |
7 |
Correct |
888 ms |
115692 KB |
Output is correct |
8 |
Correct |
312 ms |
115692 KB |
Output is correct |
9 |
Correct |
698 ms |
117612 KB |
Output is correct |
10 |
Correct |
314 ms |
117612 KB |
Output is correct |
11 |
Correct |
11 ms |
176384 KB |
Output is correct |
12 |
Correct |
11 ms |
176384 KB |
Output is correct |
13 |
Correct |
11 ms |
176384 KB |
Output is correct |
14 |
Correct |
10 ms |
176384 KB |
Output is correct |
15 |
Correct |
11 ms |
176384 KB |
Output is correct |
16 |
Correct |
11 ms |
176384 KB |
Output is correct |
17 |
Correct |
11 ms |
176384 KB |
Output is correct |
18 |
Correct |
12 ms |
176384 KB |
Output is correct |
19 |
Correct |
11 ms |
176384 KB |
Output is correct |
20 |
Correct |
13 ms |
176384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
Output is correct |
2 |
Correct |
8 ms |
7548 KB |
Output is correct |
3 |
Correct |
8 ms |
7584 KB |
Output is correct |
4 |
Correct |
10 ms |
8744 KB |
Output is correct |
5 |
Correct |
368 ms |
99808 KB |
Output is correct |
6 |
Correct |
513 ms |
108304 KB |
Output is correct |
7 |
Correct |
888 ms |
115692 KB |
Output is correct |
8 |
Correct |
312 ms |
115692 KB |
Output is correct |
9 |
Correct |
698 ms |
117612 KB |
Output is correct |
10 |
Correct |
314 ms |
117612 KB |
Output is correct |
11 |
Correct |
8 ms |
117612 KB |
Output is correct |
12 |
Correct |
9 ms |
117612 KB |
Output is correct |
13 |
Correct |
10 ms |
117612 KB |
Output is correct |
14 |
Correct |
660 ms |
119392 KB |
Output is correct |
15 |
Correct |
650 ms |
121888 KB |
Output is correct |
16 |
Correct |
606 ms |
124468 KB |
Output is correct |
17 |
Correct |
638 ms |
126808 KB |
Output is correct |
18 |
Correct |
642 ms |
129376 KB |
Output is correct |
19 |
Correct |
650 ms |
131860 KB |
Output is correct |
20 |
Correct |
686 ms |
134308 KB |
Output is correct |
21 |
Correct |
29 ms |
134308 KB |
Output is correct |
22 |
Correct |
668 ms |
137160 KB |
Output is correct |
23 |
Correct |
684 ms |
139936 KB |
Output is correct |
24 |
Correct |
630 ms |
142796 KB |
Output is correct |
25 |
Correct |
655 ms |
145544 KB |
Output is correct |
26 |
Correct |
641 ms |
148288 KB |
Output is correct |
27 |
Correct |
654 ms |
150896 KB |
Output is correct |
28 |
Correct |
774 ms |
153636 KB |
Output is correct |
29 |
Correct |
707 ms |
156428 KB |
Output is correct |
30 |
Correct |
697 ms |
159168 KB |
Output is correct |
31 |
Correct |
524 ms |
159168 KB |
Output is correct |
32 |
Correct |
720 ms |
161716 KB |
Output is correct |
33 |
Execution timed out |
1034 ms |
176384 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |