#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define MAXN 100005
typedef long long ll;
using namespace std;
int n,m,vreme;
vector<int> niz[MAXN];
vector<pair<int,int> > ono;
pair<int,int> seg[8*MAXN];
int ulaz[MAXN],izlaz[MAXN];
ll dp[MAXN][2];
struct put{
int a,b,c;
};
vector<put> svi[MAXN];
put make_put(int a,int b,int c){
put p;
p.a=a;
p.b=b;
p.c=c;
return p;
}
void dfs1(int t,int o,int h){
vreme++;
ulaz[t]=vreme;
ono.pb(mp(h,t));
for(auto x:niz[t]){
if(x==o)continue;
dfs1(x,t,h+1);
ono.pb(mp(h,t));
vreme++;
}
}
void izgradi(int l,int d,int k){
if(l==d){
seg[k]=ono[l];
return;
}
int mid=(l+d)/2;
izgradi(l,mid,k+k);
izgradi(mid+1,d,k+k+1);
seg[k]=min(seg[k+k],seg[k+k+1]);
}
pair<int,int> dobij(int l,int d,int tl,int td,int k){
if(l>td || d<tl)return ono[0];
if(l>=tl && d<=td)return seg[k];
int mid=(l+d)/2;
return min(dobij(l,mid,tl,td,k+k),dobij(mid+1,d,tl,td,k+k+1));
}
ll bit[MAXN];
void dod(int p, ll x){
for(;p<MAXN;p+=(-p)&p)
bit[p]+=x;
}
ll dob(ll p){
ll z=0;
for(;p>0;p-=(-p)&p)
z+=bit[p];
return z;
}
void dfs(int t,int o){
vreme++;
ulaz[t]=vreme;
for(auto x:niz[t]){
if(x==o)continue;
dfs(x,t);
dp[t][0]+=dp[x][1];
}
for(auto x:niz[t]){
if(x==o)continue;
dod(ulaz[x],dp[t][0]-dp[x][1]);
dod(izlaz[x]+1,-dp[t][0]+dp[x][1]);
}
dp[t][1]=dp[t][0];
for(auto x:svi[t]){
dp[t][1]=max(dp[t][1],dob(ulaz[x.a])+dob(ulaz[x.b])+dp[x.a][0]+dp[x.b][0]+x.c-dp[t][0]);
//printf(" %d %lld %lld %d %d\n",t,dob(ulaz[x.a]),dob(ulaz[x.b]),x.a,x.b);
}
//printf(" %d %lld %lld\n",t,dp[t][0],dp[t][1]);
izlaz[t]=vreme;
}
int main()
{
scanf("%d", &n);
for(int i=1;i<n;i++){
int a,b;
scanf("%d %d", &a, &b);
niz[a].pb(b);
niz[b].pb(a);
}
ono.pb(mp(MAXN,0));
dfs1(1,0,0);
izgradi(1,n+n-1,1);
scanf("%d", &m);
for(int i=1;i<=m;i++){
int a,b,c;
scanf("%d %d %d", &a, &b, &c);
if(ulaz[a]>ulaz[b])swap(a,b);
//printf("%d\n",dobij(1,n+n-1,ulaz[a],ulaz[b],1).second);
svi[dobij(1,n+n-1,ulaz[a],ulaz[b],1).second].pb(make_put(a,b,c));
}
vreme=0;
memset(ulaz,0,sizeof(ulaz));
dfs(1,0);
printf("%lld",dp[1][1]);
return 0;
}
Compilation message
election_campaign.cpp: In function 'int main()':
election_campaign.cpp:108:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
election_campaign.cpp:111:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
election_campaign.cpp:118:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &m);
~~~~~^~~~~~~~~~
election_campaign.cpp:121:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &a, &b, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5624 KB |
Output is correct |
2 |
Correct |
8 ms |
5624 KB |
Output is correct |
3 |
Correct |
7 ms |
5644 KB |
Output is correct |
4 |
Correct |
7 ms |
5796 KB |
Output is correct |
5 |
Correct |
125 ms |
18712 KB |
Output is correct |
6 |
Correct |
86 ms |
28972 KB |
Output is correct |
7 |
Correct |
116 ms |
28972 KB |
Output is correct |
8 |
Correct |
94 ms |
28972 KB |
Output is correct |
9 |
Correct |
112 ms |
28972 KB |
Output is correct |
10 |
Correct |
94 ms |
28972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
28972 KB |
Output is correct |
2 |
Correct |
6 ms |
28972 KB |
Output is correct |
3 |
Correct |
7 ms |
28972 KB |
Output is correct |
4 |
Correct |
182 ms |
38380 KB |
Output is correct |
5 |
Correct |
197 ms |
40816 KB |
Output is correct |
6 |
Correct |
182 ms |
43192 KB |
Output is correct |
7 |
Correct |
188 ms |
45760 KB |
Output is correct |
8 |
Correct |
182 ms |
48252 KB |
Output is correct |
9 |
Correct |
190 ms |
50552 KB |
Output is correct |
10 |
Correct |
170 ms |
53064 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
28972 KB |
Output is correct |
2 |
Correct |
6 ms |
28972 KB |
Output is correct |
3 |
Correct |
7 ms |
28972 KB |
Output is correct |
4 |
Correct |
182 ms |
38380 KB |
Output is correct |
5 |
Correct |
197 ms |
40816 KB |
Output is correct |
6 |
Correct |
182 ms |
43192 KB |
Output is correct |
7 |
Correct |
188 ms |
45760 KB |
Output is correct |
8 |
Correct |
182 ms |
48252 KB |
Output is correct |
9 |
Correct |
190 ms |
50552 KB |
Output is correct |
10 |
Correct |
170 ms |
53064 KB |
Output is correct |
11 |
Correct |
23 ms |
53064 KB |
Output is correct |
12 |
Correct |
183 ms |
56092 KB |
Output is correct |
13 |
Correct |
174 ms |
58868 KB |
Output is correct |
14 |
Correct |
174 ms |
61696 KB |
Output is correct |
15 |
Correct |
174 ms |
64296 KB |
Output is correct |
16 |
Correct |
168 ms |
67180 KB |
Output is correct |
17 |
Correct |
175 ms |
69780 KB |
Output is correct |
18 |
Correct |
186 ms |
72568 KB |
Output is correct |
19 |
Correct |
206 ms |
75344 KB |
Output is correct |
20 |
Correct |
180 ms |
77992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
244 ms |
77992 KB |
Output is correct |
2 |
Correct |
186 ms |
82936 KB |
Output is correct |
3 |
Correct |
256 ms |
82936 KB |
Output is correct |
4 |
Correct |
202 ms |
82936 KB |
Output is correct |
5 |
Correct |
264 ms |
86056 KB |
Output is correct |
6 |
Correct |
202 ms |
86056 KB |
Output is correct |
7 |
Correct |
233 ms |
90688 KB |
Output is correct |
8 |
Correct |
253 ms |
90688 KB |
Output is correct |
9 |
Correct |
206 ms |
100240 KB |
Output is correct |
10 |
Correct |
265 ms |
100240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5624 KB |
Output is correct |
2 |
Correct |
8 ms |
5624 KB |
Output is correct |
3 |
Correct |
7 ms |
5644 KB |
Output is correct |
4 |
Correct |
7 ms |
5796 KB |
Output is correct |
5 |
Correct |
125 ms |
18712 KB |
Output is correct |
6 |
Correct |
86 ms |
28972 KB |
Output is correct |
7 |
Correct |
116 ms |
28972 KB |
Output is correct |
8 |
Correct |
94 ms |
28972 KB |
Output is correct |
9 |
Correct |
112 ms |
28972 KB |
Output is correct |
10 |
Correct |
94 ms |
28972 KB |
Output is correct |
11 |
Correct |
10 ms |
100240 KB |
Output is correct |
12 |
Correct |
8 ms |
100240 KB |
Output is correct |
13 |
Correct |
8 ms |
100240 KB |
Output is correct |
14 |
Correct |
10 ms |
100240 KB |
Output is correct |
15 |
Correct |
8 ms |
100240 KB |
Output is correct |
16 |
Correct |
9 ms |
100240 KB |
Output is correct |
17 |
Correct |
8 ms |
100240 KB |
Output is correct |
18 |
Correct |
9 ms |
100240 KB |
Output is correct |
19 |
Correct |
10 ms |
100240 KB |
Output is correct |
20 |
Correct |
8 ms |
100240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5624 KB |
Output is correct |
2 |
Correct |
8 ms |
5624 KB |
Output is correct |
3 |
Correct |
7 ms |
5644 KB |
Output is correct |
4 |
Correct |
7 ms |
5796 KB |
Output is correct |
5 |
Correct |
125 ms |
18712 KB |
Output is correct |
6 |
Correct |
86 ms |
28972 KB |
Output is correct |
7 |
Correct |
116 ms |
28972 KB |
Output is correct |
8 |
Correct |
94 ms |
28972 KB |
Output is correct |
9 |
Correct |
112 ms |
28972 KB |
Output is correct |
10 |
Correct |
94 ms |
28972 KB |
Output is correct |
11 |
Correct |
7 ms |
28972 KB |
Output is correct |
12 |
Correct |
6 ms |
28972 KB |
Output is correct |
13 |
Correct |
7 ms |
28972 KB |
Output is correct |
14 |
Correct |
182 ms |
38380 KB |
Output is correct |
15 |
Correct |
197 ms |
40816 KB |
Output is correct |
16 |
Correct |
182 ms |
43192 KB |
Output is correct |
17 |
Correct |
188 ms |
45760 KB |
Output is correct |
18 |
Correct |
182 ms |
48252 KB |
Output is correct |
19 |
Correct |
190 ms |
50552 KB |
Output is correct |
20 |
Correct |
170 ms |
53064 KB |
Output is correct |
21 |
Correct |
23 ms |
53064 KB |
Output is correct |
22 |
Correct |
183 ms |
56092 KB |
Output is correct |
23 |
Correct |
174 ms |
58868 KB |
Output is correct |
24 |
Correct |
174 ms |
61696 KB |
Output is correct |
25 |
Correct |
174 ms |
64296 KB |
Output is correct |
26 |
Correct |
168 ms |
67180 KB |
Output is correct |
27 |
Correct |
175 ms |
69780 KB |
Output is correct |
28 |
Correct |
186 ms |
72568 KB |
Output is correct |
29 |
Correct |
206 ms |
75344 KB |
Output is correct |
30 |
Correct |
180 ms |
77992 KB |
Output is correct |
31 |
Correct |
244 ms |
77992 KB |
Output is correct |
32 |
Correct |
186 ms |
82936 KB |
Output is correct |
33 |
Correct |
256 ms |
82936 KB |
Output is correct |
34 |
Correct |
202 ms |
82936 KB |
Output is correct |
35 |
Correct |
264 ms |
86056 KB |
Output is correct |
36 |
Correct |
202 ms |
86056 KB |
Output is correct |
37 |
Correct |
233 ms |
90688 KB |
Output is correct |
38 |
Correct |
253 ms |
90688 KB |
Output is correct |
39 |
Correct |
206 ms |
100240 KB |
Output is correct |
40 |
Correct |
265 ms |
100240 KB |
Output is correct |
41 |
Correct |
10 ms |
100240 KB |
Output is correct |
42 |
Correct |
8 ms |
100240 KB |
Output is correct |
43 |
Correct |
8 ms |
100240 KB |
Output is correct |
44 |
Correct |
10 ms |
100240 KB |
Output is correct |
45 |
Correct |
8 ms |
100240 KB |
Output is correct |
46 |
Correct |
9 ms |
100240 KB |
Output is correct |
47 |
Correct |
8 ms |
100240 KB |
Output is correct |
48 |
Correct |
9 ms |
100240 KB |
Output is correct |
49 |
Correct |
10 ms |
100240 KB |
Output is correct |
50 |
Correct |
8 ms |
100240 KB |
Output is correct |
51 |
Correct |
256 ms |
100240 KB |
Output is correct |
52 |
Correct |
187 ms |
108508 KB |
Output is correct |
53 |
Correct |
253 ms |
108508 KB |
Output is correct |
54 |
Correct |
203 ms |
108508 KB |
Output is correct |
55 |
Correct |
249 ms |
108508 KB |
Output is correct |
56 |
Correct |
199 ms |
119408 KB |
Output is correct |
57 |
Correct |
238 ms |
119408 KB |
Output is correct |
58 |
Correct |
203 ms |
119408 KB |
Output is correct |
59 |
Correct |
249 ms |
119408 KB |
Output is correct |
60 |
Correct |
259 ms |
130428 KB |
Output is correct |
61 |
Correct |
244 ms |
130428 KB |
Output is correct |
62 |
Correct |
210 ms |
130428 KB |
Output is correct |
63 |
Correct |
238 ms |
130428 KB |
Output is correct |
64 |
Correct |
178 ms |
141492 KB |
Output is correct |
65 |
Correct |
248 ms |
141492 KB |
Output is correct |
66 |
Correct |
197 ms |
141492 KB |
Output is correct |
67 |
Correct |
244 ms |
141492 KB |
Output is correct |
68 |
Correct |
180 ms |
152484 KB |
Output is correct |
69 |
Correct |
241 ms |
152484 KB |
Output is correct |
70 |
Correct |
275 ms |
152484 KB |
Output is correct |