// __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 100005
#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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9712 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
4 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9932 KB |
Output is correct |
5 |
Runtime error |
199 ms |
78700 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9740 KB |
Output is correct |
3 |
Correct |
6 ms |
10060 KB |
Output is correct |
4 |
Runtime error |
244 ms |
106000 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9740 KB |
Output is correct |
3 |
Correct |
6 ms |
10060 KB |
Output is correct |
4 |
Runtime error |
244 ms |
106000 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
231 ms |
83448 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9712 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
4 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9932 KB |
Output is correct |
5 |
Runtime error |
199 ms |
78700 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9712 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
4 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9932 KB |
Output is correct |
5 |
Runtime error |
199 ms |
78700 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |