This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#pragma GCC optimize("-Ofast")
//#pragma GCC optimize("trapv")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")
#define I inline void
#define S struct
#define vi vector<int>
#define vii vector<pair<int,int>>
#define pii pair<int,int>
#define pll pair<ll,ll>
using namespace std ;
using ll = long long ;
using ld = long double ;
const int N = 2e5 + 7 , mod = 1e9 + 7 ;
const ll inf = 2e18 ;
// How interesting!
int n, m, t ;
vector<int> adj[N] ;
int up[N][20] ;
int st[N] , en[N] ;
vector< pair<pii,int> > vec[N] ;
ll dp[N], kdp[N] ;
void dfs(int x, int p){
st[x] = ++ t ;
up[x][0] = p ;
for(int i = 1; i < 20 ; ++ i)
up[x][i] = up[up[x][i-1]][i-1] ;
for(auto u : adj[x]){
if(u == p)continue ;
dfs(u , x) ;
}
en[x] = ++ t;
}
inline bool upper(int u, int v){return st[u] <= st[v] && en[v] <= en[u] ; }
int lca(int u, int v){
if(upper(u , v))return u ;
if(upper(v , u))return v ;
for(int i = 19 ; ~i; -- i)
if(!upper(up[u][i] , v))
u = up[u][i] ;
return up[u][0] ;
}
ll bit[N] ;
inline void add(int pos , ll v){for(;pos<N;pos+=pos&-pos)bit[pos]+=v ;} ;
ll upto(int pos){
ll ret = 0 ;
for(;pos;pos-=pos&-pos)
ret+=bit[pos] ;
return ret;
}
inline ll get(int l, int r){return upto(r) - upto(l - 1) ; }
inline ll walk(int u , int v){return get(st[v], st[u]); }
ll solve(int x, int p){
ll ret = 0 ;
for(auto u : adj[x]){
if(u == p)continue ;
kdp[x] += solve(u , x) ;
}
for(auto u : vec[x]){
int a = u.first.first ;
int b = u.first.second ;
ret = max(ret , walk(a , x) + walk(b , x) + u.second + kdp[x]) ;
}
dp[x] = max(ret , kdp[x]) ;
add(st[x] , kdp[x] - dp[x]) ;
add(en[x] , dp[x] - kdp[x]) ;
return dp[x];
}
int main(){
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
//freopen("in.in" , "r" , stdin) ;
cin>> n ;
for(int i = 0 ;i < n - 1; ++ i){
int u , v;
cin >> u >> v ;
adj[u].push_back(v) ;
adj[v].push_back(u) ;
}
dfs(1 , 1) ;
cin>> m ;
for(int i = 0 ; i < m; ++ i){
int u , v , w ;
cin >> u >> v >> w ;
int lc = lca(u , v) ;
vec[lc].push_back({{u , v}, w}) ;
}
solve(1 , 1) ;
cout<< dp[1] ;
return 0 ;
}
/*
- bounds sir (segtree = 4N, eulerTour = 2N, ...)
- a variable defined twice?
- will overflow?
- is it a good complexity?
- don't mess up indices (0-indexed vs 1-indexed)
- reset everything between testcases.
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |