Jerry_heng @ 2023-08-14 20:22:54
只A了1个点
#include<bits/stdc++.h>
using namespace std;
int t,n,cnt,s[100010],pre[100010],m,cnt1,dep[100010],f[100010],siz[100010];
int tree[400010],son[100010],top[100010],id[100010],head[100010],val[100010];
string st;
struct nodd{
int to,nxt,val;
}edge[200010];
struct node{
int u,v;
}a[200010];
void add(int u,int v,int w){
edge[++cnt].to=v;
edge[cnt].nxt=head[u];
edge[cnt].val=w;
head[u]=cnt;
}
void dfs1(int now,int fa){
dep[now]=dep[fa]+1;
siz[now]=1;
f[now]=fa;
for(int i=head[now];i;i=edge[i].nxt){
int v=edge[i].to;
if(v==fa)continue;
dfs1(v,now);
val[v]=edge[i].val;
siz[now]+=siz[v];
if(siz[v]>siz[son[now]])son[now]=v;
}
}
void dfs2(int now,int tp){
top[now]=tp;
id[now]=++cnt1;
pre[cnt1]=now;
if(son[now])dfs2(son[now],tp);
for(int i=head[now];i;i=edge[i].nxt){
int v=edge[i].to;
if(v==f[now]||v==son[now])continue;
dfs2(v,v);
}
}
void pushup(int o){
tree[o]=max(tree[o*2],tree[o*2+1]);
}
void build(int o,int l,int r){
if(l==r){
tree[o]=val[pre[l]];
return;
}
int mid=(l+r)>>1;
build(o*2,l,mid);
build(o*2+1,mid+1,r);
pushup(o);
}
void change(int o,int l,int r,int pos,int v){
if(l==r){
tree[o]=v;
return;
}
int mid=(l+r)>>1;
if(pos<=mid)change(o*2,l,mid,pos,v);
else change(o*2+1,mid+1,r,pos,v);
pushup(o);
}
int query1(int o,int l,int r,int L,int R){
if(L<=l&&r<=R)return tree[o];
int mid=(l+r)>>1;
int ans=0;
if(L<=mid)ans=max(ans,query1(o*2,l,mid,L,R));
if(R>mid)ans=max(ans,query1(o*2+1,mid+1,r,L,R));
return ans;
}
int query(int u,int v){
if(u==v)return 0;
int ans=0;
while(top[u]!=top[v]){
if(dep[u]<dep[v])swap(u,v);
ans=max(ans,query1(1,1,n,id[top[u]],id[u]));
u=f[top[u]];
}
if(dep[u]>dep[v])swap(u,v);
ans=max(ans,query1(1,1,n,id[u]+1,id[v]));
return ans;
}
signed main(){
scanf("%d",&n);
for(int i=1;i<n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
a[i].u=u,a[i].v=v;
}
dfs1(1,0);
dfs2(1,1);
build(1,1,n);
while(cin>>st&&st!="DONE"){
if(st=="CHANGE"){
int x,y;
scanf("%d%d",&x,&y);
int u=a[x].u,v=a[x].v;
if(v==f[u])swap(u,v);
change(1,1,n,id[v],y);
}
else{
int u,v;
scanf("%d%d",&u,&v);
printf("%d\n",query(u,v));
}
}
return 0;
}
by Jerry_heng @ 2023-08-14 20:28:15
qwq
by sane1981 @ 2023-08-20 09:44:25
@Jerry_heng
if(dep[u]<dep[v])swap(u,v);
改成
if(dep[top[u]<dep[top[v])swap(u,v);