// __builtin_popcount(x) broj bitova
// __builtin_popcountll(x) long long
#include "bits/stdc++.h"
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define ld double
#define ll long long
#define ull unsigned long long
#define llinf 100000000000000000LL // 10^17
#define iinf 2000000000 // 2*10^9
#define pb push_back
#define popb pop_back
#define fi first
#define sc second
#define endl '\n'
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pld pair<ld,ld>
#define sz(a) int(a.size())
#define all(a) a.begin(),a.end()
#define rall(a) a.begin(),a.end(),greater<int>()
#define getunique(v) {sort(all(v)); v.erase(unique(all(v)), v.end());}
#define pi 3.14159265358979323846
#define here cerr<<"---------------------------\n"
#define ceri(a,l,r) {for(ll i = l;i<=r;i++) cerr<<a[i]<< " ";cerr<<endl;}
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
void setIO(string inoutname)
{
freopen((inoutname+".in").c_str(),"r",stdin);
freopen((inoutname+".out").c_str(),"w",stdout);
}
#define mod 1
ll gcd(ll a, ll b)
{
if(b==0) return a;
if(a==0) return b;
if(a>=b) return gcd(a%b,b);
return gcd(a,b%a);
}
ll lcm(ll a,ll b){
return (a/gcd(a,b))*b;
}
ll add(ll a,ll b){
a+=b;
a+=mod;
if(a>=mod) a%=mod;
return a;
}
ll mul(ll a,ll b){return(a*b)%mod;}
#define maxn 1000005
#define lg 25
ll n,m;
vector<ll> g[maxn];
vector<ll> a[maxn],b[maxn],c[maxn];
ll t[4*maxn];
ll st[maxn][lg];
ll in[maxn],out[maxn];
ll dp[maxn];
ll ti = 0;
void dfs(ll u,ll par){
in[u] = ++ti; st[u][0] = par;
for(ll s : g[u]) if(s!=par) dfs(s,u);
out[u] = ++ti;
}
void init_lca(){for(ll j = 1;j<lg;j++) for(ll i = 1;i<=n;i++) st[i][j] = st[st[i][j-1]][j-1];}
bool in_t(ll v,ll u){return in[u]<=in[v]&&out[u]>=out[v];}
ll lca(ll v,ll u){
if(in_t(v,u)) return u;
if(in_t(u,v)) return v;
for(ll j = lg-1;j>=0;j--) if(!in_t(v,st[u][j])) u = st[u][j];
return st[u][0];
}
void upd(ll v,ll tl,ll tr,ll i,ll x){
if(tl==tr){
t[v]+=x;
return;
}
ll mid = (tl+tr)/2;
if(i<=mid) upd(2*v,tl,mid,i,x);
else upd(2*v+1,mid+1,tr,i,x);
t[v] = t[2*v] + t[2*v+1];
}
ll get(ll v,ll tl,ll tr,ll l,ll r){
if(l>r) return 0;
if(tl==l&&tr==r) return t[v];
ll mid = (tl+tr)/2;
return get(2*v,tl,mid,l,min(mid,r)) + get(2*v+1,mid+1,tr,max(l,mid+1),r);
}
void reshi(ll u,ll par){
for(ll s : g[u]) if(s!=par) reshi(s,u);
ll x = get(1,1,ti,in[u],in[u]);
dp[u] = x;
for(ll i = 0;i<sz(a[u]);i++) dp[u] = max(dp[u],get(1,1,ti,in[u],in[a[u][i]])+get(1,1,ti,in[u],in[b[u][i]])+c[u][i]-x);
upd(1,1,ti,in[par],dp[u]);
upd(1,1,ti,in[u],-dp[u]);
upd(1,1,ti,out[par],-dp[u]);
upd(1,1,ti,out[u],dp[u]);
}
void tc(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
cin >> n;
for(ll i = 1;i<=n-1;i++){
ll x,y; cin >> x >> y;
g[x].pb(y);
g[y].pb(x);
}
dfs(1,1);
init_lca();
cin >> m;
for(ll i = 1;i<=m;i++){
ll ai,bi,ci;
cin >> ai >> bi >> ci;
ll x = lca(ai,bi);
a[x].pb(ai);
b[x].pb(bi);
c[x].pb(ci);
}
reshi(1,1);
cout<<dp[1]<<endl;
}
int main(){
ios_base::sync_with_stdio(false);cerr.tie(0);cout.tie(0);cin.tie(0);
//setIO("lol");
int t; t = 1;
while(t--){
tc();
}
return 0;
}
Compilation message
election_campaign.cpp: In function 'void setIO(std::string)':
election_campaign.cpp:35:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen((inoutname+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
election_campaign.cpp:36:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen((inoutname+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
94232 KB |
Output is correct |
2 |
Correct |
43 ms |
94176 KB |
Output is correct |
3 |
Correct |
50 ms |
94212 KB |
Output is correct |
4 |
Correct |
51 ms |
94464 KB |
Output is correct |
5 |
Correct |
203 ms |
124356 KB |
Output is correct |
6 |
Correct |
155 ms |
133312 KB |
Output is correct |
7 |
Correct |
175 ms |
130392 KB |
Output is correct |
8 |
Correct |
193 ms |
125012 KB |
Output is correct |
9 |
Correct |
180 ms |
128572 KB |
Output is correct |
10 |
Correct |
163 ms |
124868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
94200 KB |
Output is correct |
2 |
Correct |
47 ms |
94284 KB |
Output is correct |
3 |
Correct |
53 ms |
94640 KB |
Output is correct |
4 |
Correct |
234 ms |
137536 KB |
Output is correct |
5 |
Correct |
237 ms |
138480 KB |
Output is correct |
6 |
Correct |
238 ms |
138520 KB |
Output is correct |
7 |
Correct |
237 ms |
138428 KB |
Output is correct |
8 |
Correct |
234 ms |
138436 KB |
Output is correct |
9 |
Correct |
217 ms |
138564 KB |
Output is correct |
10 |
Correct |
248 ms |
138476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
94200 KB |
Output is correct |
2 |
Correct |
47 ms |
94284 KB |
Output is correct |
3 |
Correct |
53 ms |
94640 KB |
Output is correct |
4 |
Correct |
234 ms |
137536 KB |
Output is correct |
5 |
Correct |
237 ms |
138480 KB |
Output is correct |
6 |
Correct |
238 ms |
138520 KB |
Output is correct |
7 |
Correct |
237 ms |
138428 KB |
Output is correct |
8 |
Correct |
234 ms |
138436 KB |
Output is correct |
9 |
Correct |
217 ms |
138564 KB |
Output is correct |
10 |
Correct |
248 ms |
138476 KB |
Output is correct |
11 |
Correct |
67 ms |
95868 KB |
Output is correct |
12 |
Correct |
246 ms |
138408 KB |
Output is correct |
13 |
Correct |
275 ms |
138388 KB |
Output is correct |
14 |
Correct |
230 ms |
138540 KB |
Output is correct |
15 |
Correct |
234 ms |
138436 KB |
Output is correct |
16 |
Correct |
230 ms |
138648 KB |
Output is correct |
17 |
Correct |
240 ms |
138444 KB |
Output is correct |
18 |
Correct |
262 ms |
138456 KB |
Output is correct |
19 |
Correct |
222 ms |
138556 KB |
Output is correct |
20 |
Correct |
225 ms |
138472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
300 ms |
126964 KB |
Output is correct |
2 |
Correct |
225 ms |
138640 KB |
Output is correct |
3 |
Correct |
434 ms |
134980 KB |
Output is correct |
4 |
Correct |
261 ms |
128468 KB |
Output is correct |
5 |
Correct |
300 ms |
134596 KB |
Output is correct |
6 |
Correct |
259 ms |
128404 KB |
Output is correct |
7 |
Correct |
344 ms |
133848 KB |
Output is correct |
8 |
Correct |
317 ms |
129220 KB |
Output is correct |
9 |
Correct |
224 ms |
138588 KB |
Output is correct |
10 |
Correct |
319 ms |
132540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
94232 KB |
Output is correct |
2 |
Correct |
43 ms |
94176 KB |
Output is correct |
3 |
Correct |
50 ms |
94212 KB |
Output is correct |
4 |
Correct |
51 ms |
94464 KB |
Output is correct |
5 |
Correct |
203 ms |
124356 KB |
Output is correct |
6 |
Correct |
155 ms |
133312 KB |
Output is correct |
7 |
Correct |
175 ms |
130392 KB |
Output is correct |
8 |
Correct |
193 ms |
125012 KB |
Output is correct |
9 |
Correct |
180 ms |
128572 KB |
Output is correct |
10 |
Correct |
163 ms |
124868 KB |
Output is correct |
11 |
Correct |
45 ms |
94592 KB |
Output is correct |
12 |
Correct |
46 ms |
94668 KB |
Output is correct |
13 |
Correct |
46 ms |
94660 KB |
Output is correct |
14 |
Correct |
51 ms |
94724 KB |
Output is correct |
15 |
Correct |
52 ms |
94504 KB |
Output is correct |
16 |
Correct |
46 ms |
94612 KB |
Output is correct |
17 |
Correct |
46 ms |
94532 KB |
Output is correct |
18 |
Correct |
47 ms |
94628 KB |
Output is correct |
19 |
Correct |
52 ms |
94556 KB |
Output is correct |
20 |
Correct |
46 ms |
94636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
94232 KB |
Output is correct |
2 |
Correct |
43 ms |
94176 KB |
Output is correct |
3 |
Correct |
50 ms |
94212 KB |
Output is correct |
4 |
Correct |
51 ms |
94464 KB |
Output is correct |
5 |
Correct |
203 ms |
124356 KB |
Output is correct |
6 |
Correct |
155 ms |
133312 KB |
Output is correct |
7 |
Correct |
175 ms |
130392 KB |
Output is correct |
8 |
Correct |
193 ms |
125012 KB |
Output is correct |
9 |
Correct |
180 ms |
128572 KB |
Output is correct |
10 |
Correct |
163 ms |
124868 KB |
Output is correct |
11 |
Correct |
47 ms |
94200 KB |
Output is correct |
12 |
Correct |
47 ms |
94284 KB |
Output is correct |
13 |
Correct |
53 ms |
94640 KB |
Output is correct |
14 |
Correct |
234 ms |
137536 KB |
Output is correct |
15 |
Correct |
237 ms |
138480 KB |
Output is correct |
16 |
Correct |
238 ms |
138520 KB |
Output is correct |
17 |
Correct |
237 ms |
138428 KB |
Output is correct |
18 |
Correct |
234 ms |
138436 KB |
Output is correct |
19 |
Correct |
217 ms |
138564 KB |
Output is correct |
20 |
Correct |
248 ms |
138476 KB |
Output is correct |
21 |
Correct |
67 ms |
95868 KB |
Output is correct |
22 |
Correct |
246 ms |
138408 KB |
Output is correct |
23 |
Correct |
275 ms |
138388 KB |
Output is correct |
24 |
Correct |
230 ms |
138540 KB |
Output is correct |
25 |
Correct |
234 ms |
138436 KB |
Output is correct |
26 |
Correct |
230 ms |
138648 KB |
Output is correct |
27 |
Correct |
240 ms |
138444 KB |
Output is correct |
28 |
Correct |
262 ms |
138456 KB |
Output is correct |
29 |
Correct |
222 ms |
138556 KB |
Output is correct |
30 |
Correct |
225 ms |
138472 KB |
Output is correct |
31 |
Correct |
300 ms |
126964 KB |
Output is correct |
32 |
Correct |
225 ms |
138640 KB |
Output is correct |
33 |
Correct |
434 ms |
134980 KB |
Output is correct |
34 |
Correct |
261 ms |
128468 KB |
Output is correct |
35 |
Correct |
300 ms |
134596 KB |
Output is correct |
36 |
Correct |
259 ms |
128404 KB |
Output is correct |
37 |
Correct |
344 ms |
133848 KB |
Output is correct |
38 |
Correct |
317 ms |
129220 KB |
Output is correct |
39 |
Correct |
224 ms |
138588 KB |
Output is correct |
40 |
Correct |
319 ms |
132540 KB |
Output is correct |
41 |
Correct |
45 ms |
94592 KB |
Output is correct |
42 |
Correct |
46 ms |
94668 KB |
Output is correct |
43 |
Correct |
46 ms |
94660 KB |
Output is correct |
44 |
Correct |
51 ms |
94724 KB |
Output is correct |
45 |
Correct |
52 ms |
94504 KB |
Output is correct |
46 |
Correct |
46 ms |
94612 KB |
Output is correct |
47 |
Correct |
46 ms |
94532 KB |
Output is correct |
48 |
Correct |
47 ms |
94628 KB |
Output is correct |
49 |
Correct |
52 ms |
94556 KB |
Output is correct |
50 |
Correct |
46 ms |
94636 KB |
Output is correct |
51 |
Correct |
340 ms |
129196 KB |
Output is correct |
52 |
Correct |
239 ms |
138436 KB |
Output is correct |
53 |
Correct |
399 ms |
132784 KB |
Output is correct |
54 |
Correct |
233 ms |
128196 KB |
Output is correct |
55 |
Correct |
308 ms |
127964 KB |
Output is correct |
56 |
Correct |
249 ms |
138392 KB |
Output is correct |
57 |
Correct |
334 ms |
134072 KB |
Output is correct |
58 |
Correct |
266 ms |
128100 KB |
Output is correct |
59 |
Correct |
300 ms |
129264 KB |
Output is correct |
60 |
Correct |
236 ms |
138436 KB |
Output is correct |
61 |
Correct |
363 ms |
134280 KB |
Output is correct |
62 |
Correct |
350 ms |
127980 KB |
Output is correct |
63 |
Correct |
278 ms |
127832 KB |
Output is correct |
64 |
Correct |
235 ms |
138556 KB |
Output is correct |
65 |
Correct |
318 ms |
133584 KB |
Output is correct |
66 |
Correct |
252 ms |
128236 KB |
Output is correct |
67 |
Correct |
300 ms |
128068 KB |
Output is correct |
68 |
Correct |
236 ms |
138456 KB |
Output is correct |
69 |
Correct |
364 ms |
132680 KB |
Output is correct |
70 |
Correct |
295 ms |
128156 KB |
Output is correct |