又WA又T,弱小可怜无助.jpg
上次比较急,注释没删就发了,我爪巴
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL N=100005;
#define DL pair<LL,LL>
struct node
{
node *ls,*rs;LL l,r,v;
inline node(LL l,LL r):l(l),r(r) {ls=rs=nullptr;}
inline void up() {v=0;if(ls) {v+=ls->v;}if(rs) {v+=rs->v;}}
};
//inline bool operator < (const DL& a,const DL& b) {return (a.first==b.first)?(a.second>b.second):(a.first<b.first);}
inline LL getv(node* rs) {return rs?rs->v:0;}
inline node* cha(node* &rs,LL l,LL r,LL q,LL v)
{
if(!rs) rs=new node(l,r);if(l==r) {rs->v+=v;return rs;}
LL mid=l+(r-l>>1);if(q<=mid) rs->ls=cha(rs->ls,l,mid,q,v);else rs->rs=cha(rs->rs,mid+1,r,q,v);rs->up();
return rs;
}
inline LL query(node* &rs,LL l,LL r)
{
if(!rs || rs->l>r || rs->r<l) return 0;
if(rs->l>=l && rs->r<=r) return getv(rs);LL mid=rs->l+(rs->r-rs->l>>1),ans=0;
if(l<=mid) ans+=query(rs->ls,l,r);if(mid+1<=r) ans+=query(rs->rs,l,r);return ans;
}
inline LL qkth(node* &rs,LL k)
{
if(!rs || k<=0) return -1;if(rs->l==rs->r) return rs->l;
if(k<=getv(rs->ls)) return qkth(rs->ls,k);else return qkth(rs->rs,k-getv(rs->ls));
}
inline DL query_max_node(node* &rs)
{
if(!rs) return DL(0,0);
if(rs->l==rs->r) return make_pair(getv(rs),rs->l);
DL lmax=(rs->ls)?query_max_node(rs->ls):DL(0,0),rmax=(rs->rs)?query_max_node(rs->rs):DL(0,0);
if(lmax.first>rmax.first || (lmax.first==rmax.first && lmax.second<rmax.second)) return lmax;else return rmax;
}
inline node* merge (node* &x,node* &y,LL l,LL r)
{
if (!x||!y) return x?x:y;
if(l==r) {x->l=x->r=l,x->v+=y->v;return x;}
LL mid=l+(r-l>>1);x->ls=merge(x->ls,y->ls,l,mid),x->rs=merge(x->rs,y->rs,mid+1,r);
x->up();return x;
}
node* nodeT[N];
struct edges {LL to,nxt,v;} G[N<<1];
LL head[N],cnt,ct,qhd[N],uds[N<<1],lcans[N<<1],fas[N];
inline void add_edge(LL u,LL v,LL val) {G[cnt].to=v,G[cnt].v=val,G[cnt].nxt=head[u];head[u]=cnt++;}
struct queries {LL to,nxt,id;} Q[N<<1];
inline void add_q(LL u,LL v,LL id) {Q[ct].to=v,Q[ct].id=id,Q[ct].nxt=qhd[u];qhd[u]=ct++;}
bool vis[N],vis2[N];
inline LL getf(LL x) {if(x==uds[x]) return x;return uds[x]=getf(uds[x]);}
inline void Tarjan(LL u)
{
vis[u]=1;
for(auto i=head[u];i!=-1;i=G[i].nxt) {auto v=G[i].to;if(!vis[v]){Tarjan(v);uds[v]=u;}}
for(auto i=qhd[u];i!=-1;i=Q[i].nxt) {auto v=Q[i].to;if(vis[v]) lcans[Q[i].id]=getf(v);}
}
inline void find_fa(LL u,LL fa)
{
vis2[u]=1,fas[u]=fa;for(LL i=head[u];i!=-1;i=G[i].nxt) if(!vis2[G[i].to]) find_fa(G[i].to,u);
}
struct operators {LL x,y,z;}opts[N];
LL n,m,Z,ans[N];
inline LL qread()
{
LL a=0,f=1;char ch=getchar();
while(ch<'0' || ch>'9') {if(ch=='-') f*=-1;ch=getchar();}
while(ch>='0' && ch<='9') {a=a*10+ch-'0';ch=getchar();}
return a*f;
}
inline LL qwrite(LL x)
{
LL rnt=x;
if(x==0) {putchar('0'),putchar('\n');return 0;}
char w[128];LL cnt=0;
if(x<0) putchar('-'),x*=-1;
while(x) {w[cnt++]=(x%10)+'0';x/=10;}
while(cnt--) putchar(w[cnt]);putchar('\n');return rnt;
}
inline void set_path(LL nw)
{
LL x=opts[nw].x,y=opts[nw].y,z=opts[nw].z,nlca=lcans[nw],nlf=fas[nlca];
cha(nodeT[x],1,Z,z,1);cha(nodeT[y],1,Z,z,1);cha(nodeT[nlca],1,Z,z,-1);
if(nlf!=-1) cha(nodeT[nlf],1,Z,z,-1);
}
inline void dfsb(LL u)
{
vis[u]=1;for(LL i=head[u];i!=-1;i=G[i].nxt)
if(!vis[G[i].to]) dfsb(G[i].to),nodeT[u]=merge(nodeT[u],nodeT[G[i].to],1,Z);
if(nodeT[u]) ans[u]=query_max_node(nodeT[u]).second;
}
int main()
{
memset(head,-1,sizeof(head));memset(qhd,-1,sizeof(qhd));
memset(vis,0,sizeof(vis));memset(vis2,0,sizeof(vis2));
memset(fas,-1,sizeof(fas));
n=qread(),m=qread();for(LL i=1;i<=n;i++) uds[i]=i;
for(LL i=1;i<=n-1;i++) {LL u=qread(),v=qread();add_edge(u,v,1),add_edge(v,u,1);}find_fa(1,-1);;
for(LL i=1;i<=m;i++) opts[i].x=qread(),opts[i].y=qread(),opts[i].z=qread(),Z=max(Z,opts[i].z);
for(LL i=1;i<=m;i++) add_q(opts[i].x,opts[i].y,i),add_q(opts[i].y,opts[i].x,i);Tarjan(1);
for(LL i=1;i<=m;i++) set_path(i);
memset(vis,0,sizeof(vis));dfsb(1);
for(LL i=1;i<=n;++i) qwrite(query_max_node(nodeT[i]).second);
return 0;
}