# HDU 5029 Relief grain （树链剖分+权值线段树思想）

#include<bits/stdc++.h>
using namespace std;
#define debug cout<<"YES\n";
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define piii pair<pii,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
#define sc(x) scanf("%d",&x)
#define scc(x,y) scanf("%d%d",&x,&y)
#define sccc(x,y,z) scanf("%d%d%d",&x,&y,&z)
const int mod=201314;
const int maxn=1e5+5;
const int lz=0;
const ll INF=1e18;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){if(y==0) return x;return gcd(y,x%y);}

int n,m;
vector<int> g[maxn];
///树链剖分
int dep[maxn],fa[maxn],son[maxn],sz[maxn];
void dfs1(int u,int f){
sz[u]=1,dep[u]=dep[f]+1,son[u]=0,fa[u]=f;
for(auto v:g[u]) if(v!=f){
dfs1(v,u);
sz[u]+=sz[v];
if(sz[v]>sz[son[u]]) son[u]=v;
}
}
int dfn[maxn],tot=0,top[maxn],mp[maxn];
void dfs2(int u,int f){
top[u]=f,dfn[u]=++tot,mp[tot]=u;
if(son[u]) dfs2(son[u],f);
for(auto v:g[u]) if(v!=fa[u]&&v!=son[u])
dfs2(v,v);
}
///线段树
int maxc[maxn<<2],maxv[maxn<<2];///线段树
void build(lrt){
maxc[rt]=maxv[rt]=0;
if(l==r){
maxv[rt]=l;
return ;
}
int mid=l+r>>1;
build(lson),build(rson);
}
void update(lrt,int pos,int v){
if(l==r){
maxv[rt]=l;
maxc[rt]+=v;
return ;
}
int mid=l+r>>1;
if(pos<=mid) update(lson,pos,v);
else update(rson,pos,v);
if(maxc[rt<<1]>=maxc[rt<<1|1])
maxc[rt]=maxc[rt<<1],maxv[rt]=maxv[rt<<1];
else
maxc[rt]=maxc[rt<<1|1],maxv[rt]=maxv[rt<<1|1];
}
vector<pii> p[maxn];
void change(int x,int y,int z){
while(top[x]^top[y]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
p[dfn[top[x]]].push_back(mk(z,1));
p[dfn[x]+1].push_back(mk(z,-1));
x=fa[top[x]];
}
if(dep[x]<dep[y]) swap(x,y);
p[dfn[y]].push_back(mk(z,1));
p[dfn[x]+1].push_back(mk(z,-1));
}
int ans[maxn];
int main(){
ios::sync_with_stdio(false);/*
freopen("d://in.txt","r",stdin);
freopen("d://out.txt","w",stdout);*/
while(cin>>n>>m&&(n+m)){
tot=0;build(1,maxn,1);
rep(i,1,n+1) g[i].clear(),p[i].clear();
rep(i,1,n){
int x,y;
cin>>x>>y;
g[x].push_back(y);
g[y].push_back(x);
}
dfs1(1,0),dfs2(1,1);
rep(i,0,m){
int x,y,z;
cin>>x>>y>>z;
change(x,y,z);
}
rep(i,1,n+1){
for(auto v:p[i])
update(1,maxn,1,v.fi,v.se);
if(maxc[1]==0) ans[mp[i]]=0;
else ans[mp[i]]=maxv[1];
}
rep(i,1,n+1) cout<<ans[i]<<"\n";
}
return 0;
}

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客