#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,ssse3,sse3,sse4,popcnt,avx,mmx,abm,tune=native")
#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);
}
inline 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];
}
inline 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];
}
inline 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){
int idk;
for(int i=1;i<20;i++){
idk = adv(n, (1<<i)-1);
bld[idk].push_back({n,i});
if(!idk) break;
}
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:82: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:83: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:92: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:93: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);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7416 KB |
Output is correct |
2 |
Correct |
9 ms |
7420 KB |
Output is correct |
3 |
Correct |
11 ms |
7480 KB |
Output is correct |
4 |
Correct |
10 ms |
8196 KB |
Output is correct |
5 |
Correct |
268 ms |
70040 KB |
Output is correct |
6 |
Correct |
602 ms |
103920 KB |
Output is correct |
7 |
Correct |
809 ms |
107728 KB |
Output is correct |
8 |
Correct |
201 ms |
107728 KB |
Output is correct |
9 |
Correct |
718 ms |
107728 KB |
Output is correct |
10 |
Correct |
205 ms |
107728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
107728 KB |
Output is correct |
2 |
Correct |
9 ms |
107728 KB |
Output is correct |
3 |
Correct |
11 ms |
107728 KB |
Output is correct |
4 |
Correct |
688 ms |
107808 KB |
Output is correct |
5 |
Correct |
733 ms |
108000 KB |
Output is correct |
6 |
Correct |
606 ms |
108000 KB |
Output is correct |
7 |
Correct |
679 ms |
108000 KB |
Output is correct |
8 |
Correct |
761 ms |
108000 KB |
Output is correct |
9 |
Correct |
674 ms |
108072 KB |
Output is correct |
10 |
Correct |
678 ms |
108072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
107728 KB |
Output is correct |
2 |
Correct |
9 ms |
107728 KB |
Output is correct |
3 |
Correct |
11 ms |
107728 KB |
Output is correct |
4 |
Correct |
688 ms |
107808 KB |
Output is correct |
5 |
Correct |
733 ms |
108000 KB |
Output is correct |
6 |
Correct |
606 ms |
108000 KB |
Output is correct |
7 |
Correct |
679 ms |
108000 KB |
Output is correct |
8 |
Correct |
761 ms |
108000 KB |
Output is correct |
9 |
Correct |
674 ms |
108072 KB |
Output is correct |
10 |
Correct |
678 ms |
108072 KB |
Output is correct |
11 |
Correct |
30 ms |
108072 KB |
Output is correct |
12 |
Correct |
711 ms |
108072 KB |
Output is correct |
13 |
Correct |
656 ms |
108072 KB |
Output is correct |
14 |
Correct |
614 ms |
108072 KB |
Output is correct |
15 |
Correct |
706 ms |
108072 KB |
Output is correct |
16 |
Correct |
644 ms |
108136 KB |
Output is correct |
17 |
Correct |
705 ms |
108136 KB |
Output is correct |
18 |
Correct |
737 ms |
108136 KB |
Output is correct |
19 |
Correct |
588 ms |
108136 KB |
Output is correct |
20 |
Correct |
671 ms |
108136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
426 ms |
108136 KB |
Output is correct |
2 |
Correct |
624 ms |
108136 KB |
Output is correct |
3 |
Execution timed out |
1084 ms |
109352 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7416 KB |
Output is correct |
2 |
Correct |
9 ms |
7420 KB |
Output is correct |
3 |
Correct |
11 ms |
7480 KB |
Output is correct |
4 |
Correct |
10 ms |
8196 KB |
Output is correct |
5 |
Correct |
268 ms |
70040 KB |
Output is correct |
6 |
Correct |
602 ms |
103920 KB |
Output is correct |
7 |
Correct |
809 ms |
107728 KB |
Output is correct |
8 |
Correct |
201 ms |
107728 KB |
Output is correct |
9 |
Correct |
718 ms |
107728 KB |
Output is correct |
10 |
Correct |
205 ms |
107728 KB |
Output is correct |
11 |
Correct |
11 ms |
109352 KB |
Output is correct |
12 |
Correct |
11 ms |
109352 KB |
Output is correct |
13 |
Correct |
11 ms |
109352 KB |
Output is correct |
14 |
Correct |
11 ms |
109352 KB |
Output is correct |
15 |
Correct |
11 ms |
109352 KB |
Output is correct |
16 |
Correct |
11 ms |
109352 KB |
Output is correct |
17 |
Correct |
11 ms |
109352 KB |
Output is correct |
18 |
Correct |
11 ms |
109352 KB |
Output is correct |
19 |
Correct |
14 ms |
109352 KB |
Output is correct |
20 |
Correct |
11 ms |
109352 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7416 KB |
Output is correct |
2 |
Correct |
9 ms |
7420 KB |
Output is correct |
3 |
Correct |
11 ms |
7480 KB |
Output is correct |
4 |
Correct |
10 ms |
8196 KB |
Output is correct |
5 |
Correct |
268 ms |
70040 KB |
Output is correct |
6 |
Correct |
602 ms |
103920 KB |
Output is correct |
7 |
Correct |
809 ms |
107728 KB |
Output is correct |
8 |
Correct |
201 ms |
107728 KB |
Output is correct |
9 |
Correct |
718 ms |
107728 KB |
Output is correct |
10 |
Correct |
205 ms |
107728 KB |
Output is correct |
11 |
Correct |
9 ms |
107728 KB |
Output is correct |
12 |
Correct |
9 ms |
107728 KB |
Output is correct |
13 |
Correct |
11 ms |
107728 KB |
Output is correct |
14 |
Correct |
688 ms |
107808 KB |
Output is correct |
15 |
Correct |
733 ms |
108000 KB |
Output is correct |
16 |
Correct |
606 ms |
108000 KB |
Output is correct |
17 |
Correct |
679 ms |
108000 KB |
Output is correct |
18 |
Correct |
761 ms |
108000 KB |
Output is correct |
19 |
Correct |
674 ms |
108072 KB |
Output is correct |
20 |
Correct |
678 ms |
108072 KB |
Output is correct |
21 |
Correct |
30 ms |
108072 KB |
Output is correct |
22 |
Correct |
711 ms |
108072 KB |
Output is correct |
23 |
Correct |
656 ms |
108072 KB |
Output is correct |
24 |
Correct |
614 ms |
108072 KB |
Output is correct |
25 |
Correct |
706 ms |
108072 KB |
Output is correct |
26 |
Correct |
644 ms |
108136 KB |
Output is correct |
27 |
Correct |
705 ms |
108136 KB |
Output is correct |
28 |
Correct |
737 ms |
108136 KB |
Output is correct |
29 |
Correct |
588 ms |
108136 KB |
Output is correct |
30 |
Correct |
671 ms |
108136 KB |
Output is correct |
31 |
Correct |
426 ms |
108136 KB |
Output is correct |
32 |
Correct |
624 ms |
108136 KB |
Output is correct |
33 |
Execution timed out |
1084 ms |
109352 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |