#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i=a;i<=b;i++)
#define rep2(i,a,b,c) for (int i=a;i<=b;i+=c)
#define rev(i,a,b) for (int i=a;i>=b;i--)
#define rev2(i,a,b,c) for (int i=a;i>=b;i-=c)
#define bit(i,j) ((i>>j)&1)
#define pii pair<int,int>
#define ull unsigned long long
#define pb push_back
#define pf push_front
#define ll long long
#define Fi first
#define Se second
#define on(n) __builtin_popcountll(n)
#define ld long double
#define __log2(x) 31-__builtin_clz(x)
#define Mask(x) (1LL<<x)
#define ALL(v) v.begin(),v.end()
const int MAXN=1e5+5;
const int mod=998244353;
int n,m,a,b,c,u,v,dp[MAXN];
vector<int> g[MAXN];bool ok=0;
struct Path{
int x,y,w;
};
vector<Path> path[MAXN];
struct HLD{
struct IT{
int st[4*MAXN];
void update(int x,int l,int r,int u,int v){
if (u<l||r<u||l>r) return;
if (l==r){
st[x]+=v;
return;
}
int mid=(l+r)/2;
update(x*2,l,mid,u,v);
update(x*2+1,mid+1,r,u,v);
st[x]=st[x*2]+st[x*2+1];
}
int get(int x,int l,int r,int u,int v){
if (v<l||r<u||l>r) return 0;
if (u<=l&&r<=v) return st[x];
int mid=(l+r)/2;
return get(x*2,l,mid,u,v)+get(x*2+1,mid+1,r,u,v);
}
void Upd(int pos,int val) { update(1,1,n,pos,val); };
int Get(int l,int r) { return get(1,1,n,l,r); };
};
IT sumdp,sumchilddp;
int sz[MAXN],par[MAXN],depth[MAXN];
int id[MAXN],head[MAXN],heavy[MAXN],cnt=0;
void dfs(int u){
sz[u]=1;
for (int v:g[u]){
if (v==par[u]) continue;
par[v]=u;
depth[v]=depth[u]+1;
dfs(v);
sz[u]+=sz[v];
if (heavy[u]==-1||sz[v]>sz[heavy[u]]) heavy[u]=v;
}
}
void Decompose(int u,int t){
id[u]=++cnt;
head[u]=t;
if (heavy[u]!=-1) Decompose(heavy[u],t);
for (int v:g[u]){
if (v==par[u]||v==heavy[u]) continue;
Decompose(v,v);
}
}
int lca(int u,int v){
for (;head[u]!=head[v];v=par[head[v]])
if (depth[head[u]]>depth[head[v]]) swap(u,v);
if (depth[u]>depth[v]) swap(u,v);
return u;
}
void Updsumdp(int u,int val) {sumdp.Upd(id[u],val);};
void Updsumchilddp(int u,int val) {sumchilddp.Upd(id[u],val);};
int CalcPath(int u,int v){
int Total=0,Exclu=0;
for (;head[u]!=head[v];v=par[head[v]]){
if (depth[head[u]]>depth[head[v]]) swap(u,v);
Total+=sumchilddp.Get(id[head[v]],id[v]);
Exclu+=sumdp.Get(id[head[v]],id[v]);
}
if (depth[u]>depth[v]) swap(u,v);
Total+=sumchilddp.Get(id[u],id[v]);
Exclu+=sumdp.Get(id[u],id[v]);
return Total-Exclu;
}
void Init(){
memset(heavy,-1,sizeof(heavy));
dfs(1);
Decompose(1,1);
}
};
HLD tree;
void dfs(int u,int p){
for (int v:g[u]){
if (v==p) continue;
dfs(v,u);
}
int sum=0;
for (int v:g[u]) if (v!=p) sum+=dp[v];
dp[u]=sum;
tree.Updsumchilddp(u,sum);
if (!path[u].empty())
for (Path tmp:path[u]) dp[u]=max(dp[u],tree.CalcPath(tmp.x,tmp.y)+tmp.w);
tree.Updsumdp(u,dp[u]);
}
void solution(){
cin >> n;
rep(i,1,n-1){
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
tree.Init();
cin >> m;
rep(i,1,m){
cin >> a >> b >> c;
path[tree.lca(a,b)].pb({a,b,c});
}
dfs(1,-1);
cout << dp[1];
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int test=1;
while(test--)
solution();
}
# | 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... |