记录编号 |
586014 |
评测结果 |
AAAAAAAAAAAAAAAAAAAA |
题目名称 |
雨天的尾巴 |
最终得分 |
100 |
用户昵称 |
┭┮﹏┭┮ |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
2.426 s |
提交时间 |
2024-01-02 22:05:30 |
内存使用 |
66.55 MiB |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5+10;
int n,m;
struct made{
int ver,nx;
}e[N<<1];
int hd[N],tot,cnt,idx;
int size[N],son[N],dep[N],fa[N];
int dfn[N],top[N];
struct Tree{
int ls,rs;
int ma;
#define ls(x) t[x].ls
#define rs(x) t[x].rs
#define ma(x) t[x].ma
}t[N*100];
int root[N],ans[N];
void pushup(int x){
ma(x) = max(ma(ls(x)),ma(rs(x)));
}
void insert(int &x,int l,int r,int k,int z){
if(!x)x = ++idx;
if(l == r){
ma(x) += z;
return;
}
int mid = l + r >> 1;
if(k <= mid)insert(ls(x),l,mid,k,z);
else insert(rs(x),mid+1,r,k,z);
pushup(x);
}
int merge(int a,int b,int l,int r){
if(!a)return b;
if(!b)return a;
if(l == r){
ma(a) += ma(b);
return a;
}
int mid = l + r >> 1;
ls(a) = merge(ls(a),ls(b),l,mid);
rs(a) = merge(rs(a),rs(b),mid+1,r);
pushup(a);
return a;
}
int query(int x,int l,int r){
if(l == r)return l;
int mid = l + r >> 1;
if(ma(ls(x)) >= ma(rs(x)))return query(ls(x),l,mid);
else return query(rs(x),mid+1,r);
}
void dfs3(int x){
for(int i = hd[x];i;i = e[i].nx){
int y = e[i].ver;
if(y == fa[x])continue;
dfs3(y);
root[x] = merge(root[x],root[y],1,1e5);
}
if(ma(root[x]) == 0)ans[x] = 0;
else ans[x] = query(root[x],1,1e5);
}
//---------------
void add(int x,int y){
tot++;
e[tot].ver = y,e[tot].nx = hd[x],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;
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);
}
}
int LCA(int x,int y){
while(top[x] != top[y]){
if(dep[top[x]] < dep[top[y]])swap(x,y);
x = fa[top[x]];
}
if(dep[x] > dep[y])swap(x,y);
return x;
}
int main(){
freopen("Rainbows.in","r",stdin);
freopen("Rainbows.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);
}
dep[1] = 1;
dfs1(1),dfs2(1,1);
for(int i = 1;i <= m;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
int lca = LCA(x,y);
insert(root[x],1,1e5,z,1);
insert(root[y],1,1e5,z,1);
insert(root[lca],1,1e5,z,-1);
if(fa[lca])insert(root[fa[lca]],1,1e5,z,-1);
}
dfs3(1);
for(int i = 1;i <= n;i++)printf("%d\n",ans[i]);
return 0;
}