记录编号 585462 评测结果 AAAAAAAAAAAAAAA
题目名称 [USACO Open18 Platinum]Disruption 最终得分 100
用户昵称 Gravatar┭┮﹏┭┮ 是否通过 通过
代码语言 C++ 运行时间 1.089 s
提交时间 2023-12-11 11:45:28 内存使用 14.89 MiB
显示代码纯文本
#include <bits/stdc++.h> 
using namespace std;
const int N = 1e5+10;
#define ll long long
int n,m;
struct made{
    int ver,nx;
}e[N<<1];
struct line{
    int x,y;
}li[N];
int hd[N],tot,cnt;
int fa[N],size[N],son[N],dep[N];
int top[N],dfn[N],rnk[N];
struct Tree{
    int l,r,mi,la;
    #define l(x) t[x].l
    #define la(x) t[x].la
    #define r(x) t[x].r
    #define mi(x) t[x].mi
}t[N<<2];
void build(int x,int l,int r){
    l(x) = l,r(x) = r,mi(x) = INT_MAX,la(x) = INT_MAX;
    if(l == r)return;
    int mid = l + r >> 1;
    build(x<<1,l,mid);
    build(x<<1|1,mid+1,r);
}
void pushup(int x){
    mi(x) = min(mi(x<<1),mi(x<<1|1));
}
void pushdown(int x){
    if(la(x) == INT_MAX)return;
    la(x<<1) = min(la(x<<1),la(x));
    la(x<<1|1) = min(la(x<<1|1),la(x));
    mi(x<<1) = min(mi(x<<1),la(x));
    mi(x<<1|1) = min(mi(x<<1|1),la(x));
    la(x) = INT_MAX;
}
void change(int x,int l,int r,int z){
    if(l <= l(x) && r(x) <= r){
        mi(x) = min(mi(x),z);
        la(x) = min(la(x),z);
        return;
    }
    pushdown(x);
    int mid = l(x) + r(x) >> 1;
    if(l <= mid)change(x<<1,l,r,z);
    if(r > mid)change(x<<1|1,l,r,z);
    pushup(x);
}
int ask(int x,int l){
    if(l(x) == r(x))return mi(x);
    pushdown(x);
    int mid = l(x) + r(x) >> 1;
    if(l <= mid)return ask(x<<1,l);
    else return ask(x<<1|1,l);
}
//-----------------
void add(int x,int y){
    tot++;
    e[tot].nx = hd[x],e[tot].ver = y,hd[x] = tot;
}
void dfs1(int x){
    size[x] = 1;
    for(int i = hd[x];i;i = e[i].nx){
        int y = e[i].ver;
        if(dep[y])continue;
        dep[y] = dep[x] + 1;
        fa[y] = x;
        dfs1(y);
        size[x] += size[y];
        if(!son[x] || size[y] > size[son[x]])son[x] = y;
    }
}
void dfs2(int x,int t){
    dfn[x] = ++cnt;
    rnk[cnt] = x;
    top[x] = t;
    if(!son[x])return;
    dfs2(son[x],t);
    for(int i = hd[x];i;i = e[i].nx){
        int y = e[i].ver;
        if(y != fa[x] && y != son[x])dfs2(y,y);
    }
}
void cal(int x,int y,int z){
    while(top[x] != top[y]){
        if(dep[top[x]] < dep[top[y]])swap(x,y);
        change(1,dfn[top[x]],dfn[x],z);
        x = fa[top[x]];
    }
    if(x == y)return;
    if(dep[x] > dep[y])swap(x,y);
    change(1,dfn[x]+1,dfn[y],z);
}
int main(){
    freopen("disrupt.in","r",stdin);
    freopen("disrupt.out","w",stdout);
    scanf("%d%d",&n,&m);
    for(int i = 1;i < n;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);add(y,x);
        li[i].x = x,li[i].y = y;
    }
    dep[1] = 1;
    dfs1(1);
    dfs2(1,1);
    build(1,1,n);
    for(int i = 1;i <= m;i++){
        int x,y,z;
        scanf("%d%d%d",&x,&y,&z);
        cal(x,y,z);
    }
    for(int i = 1;i < n;i++){
        int x = dep[li[i].x] < dep[li[i].y]?li[i].y:li[i].x;
        int ans = ask(1,dfn[x]);
        if(ans == INT_MAX)printf("-1\n");
        else printf("%d\n",ans);
    }
    
    return 0;
    
}