1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| inline char gc(){ static char buf[SIZE],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,SIZE,stdin),p1==p2)?EOF:*p1++; } template <class T>inline void read(T &x){ x=0;int ne=0;char c; while((c=gc())>'9'||c<'0')ne=c=='-';x=c-48; while((c=gc())>='0'&&c<='9')x=(x<<3)+(x<<1)+c-48;x=ne?-x:x;return ; } const int maxn=200005; const int inf=0x7fffffff; int n,m; struct Edge{ int ne,to; ll dis; }edge[maxn<<1]; int h[maxn],num_edge=1; inline void add_edge(int f,int to,ll c){ edge[++num_edge].ne=h[f]; edge[num_edge].to=to; edge[num_edge].dis=c; h[f]=num_edge; } pair<int,int> qwq[maxn]; struct Niconiconi{ int x,y; ll dis; Niconiconi(){x=y=dis=0;} Niconiconi(int _x,int _y,ll _c){x=_x,y=_y,dis=_c;} bool operator <(const Niconiconi &rhs)const{ return dis<rhs.dis; } }con[maxn<<1]; int dep[maxn],fa[maxn],son[maxn],size[maxn],top[maxn],dfn[maxn],rnk[maxn],tot=0; void dfs1(int now){ int v; size[now]=1; for(ri i=h[now];i;i=edge[i].ne){ v=edge[i].to; if(v==fa[now])continue; dep[v]=dep[now]+1,fa[v]=now; dfs1(v); size[now]+=size[v]; if(!son[now]||size[son[now]]<size[v])son[now]=v; } return ; } void dfs2(int now,int t){ int v; top[now]=t,dfn[now]=++tot,rnk[tot]=now; if(!son[now])return ; dfs2(son[now],t); for(ri i=h[now];i;i=edge[i].ne){ v=edge[i].to; if(v==fa[now]||v==son[now])continue; dfs2(v,v); } return ; } int tag[maxn<<2],w[maxn]; int L,R,dta; inline void pushdown(int now){ if(!tag[now<<1])tag[now<<1]=tag[now]; if(!tag[now<<1|1])tag[now<<1|1]=tag[now]; } void update(int now,int l,int r){ if(tag[now])return ; if(L<=l&&r<=R){ tag[now]=dta; return ; } int mid=(l+r)>>1; pushdown(now); if(L<=mid&&!tag[now<<1])update(now<<1,l,mid); if(mid<R&&!tag[now<<1|1])update(now<<1|1,mid+1,r); return ; } void ahaha(int now,int l,int r){ if(l==r){ if(tag[now])w[rnk[l]]=tag[now]; else w[rnk[l]]=-1; return ; } int mid=(l+r)>>1; pushdown(now); ahaha(now<<1,l,mid); ahaha(now<<1|1,mid+1,r); return ; } inline void update_path(int x,int y){ while(top[x]!=top[y]){ if(dep[top[x]]<dep[top[y]])std::swap(x,y); L=dfn[top[x]],R=dfn[x]; update(1,1,n); x=fa[top[x]]; } if(dep[x]>dep[y])std::swap(x,y); L=dfn[x]+1,R=dfn[y]; if(L>R)return ; update(1,1,n); } int main(){ int x,y;ll z; FO(worry); read(n),read(m); for(ri i=1;i<n;i++){ read(x),read(y); add_edge(x,y,0); add_edge(y,x,0); qwq[i]=pair<int,int>(x,y); } for(ri i=1;i<=m;i++){ read(x),read(y),read(z); con[i]=Niconiconi(x,y,z); } std::sort(con+1,con+1+m); dep[1]=0,fa[1]=0; dfs1(1); dfs2(1,1); memset(tag,0,sizeof(tag)); for(ri i=1;i<=m;i++){ x=con[i].x,y=con[i].y,dta=con[i].dis; update_path(x,y); } ahaha(1,1,n); for(ri i=1;i<n;i++){ x=qwq[i].first,y=qwq[i].second; if(dep[x]<dep[y])std::swap(x,y); printf("%d\n",w[x]); } return 0; }
|