JustinJQian @ 2023-08-20 20:42:12
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <string>
#include <cstdlib>
using namespace std;
const int maxn = 1e5+1;
const int INF = 2e9;
inline int read(){
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
return x*f;
}
struct side{
int pos,v,wt;
};
int n;
vector <side> tree[maxn];
int tmax[maxn*4],fa[maxn][20],size[maxn],idx[maxn],pos[maxn],bl[maxn],dep[maxn],len1[maxn],wt[maxn];
int *root[maxn],*cnt = tmax;
void dfs(int u,int Fa){
fa[u][0] = Fa;
dep[u] = dep[Fa]+1;
size[u] = 1;
for(int i = 1;i < 15;i++){
fa[u][i] = fa[fa[u][i-1]][i-1];
}
for(int i = 0;i < tree[u].size();i++){
if(tree[u][i].v!=Fa){
int v = tree[u][i].v;
idx[tree[u][i].pos] = v;
wt[v] = tree[u][i].wt;
dfs(v,u);
size[u]+=size[v];
}
}
}
inline int *newTree(int len){
int *ret = cnt;
cnt+=4*len;
return ret;
}
void insert(int *tmax,int i,int l,int r,int pos,int wt){
if(l==r){
tmax[i] = wt;
return;
}
int mid = (l+r)>>1;
if(pos<=mid) insert(tmax,i<<1,l,mid,pos,wt);
else insert(tmax,(i<<1)+1,mid+1,r,pos,wt);
tmax[i] = max(tmax[i<<1],tmax[(i<<1)+1]);
}
void dfs2(int u,int len,int Fa){
int heavy = 0;
for(int i = 0;i < tree[u].size();i++){
int v = tree[u][i].v;
if(v!=Fa&&size[v]>size[heavy]){
heavy = v;
}
}
if(!heavy){
int tp = u;
for(int i = 1;i < len;i++){
tp = fa[tp][0];
}
root[tp] = newTree(len);
len1[tp] = len;
for(int i = len;i;i--){
bl[u] = tp;
pos[u] = i;
insert(root[tp],1,1,len,i,wt[u]);
u = fa[u][0];
}
return;
}
dfs2(heavy,len+1,u);
for(int i = 0;i < tree[u].size();i++){
int v = tree[u][i].v;
if(v!=Fa&&v!=heavy)dfs2(v,1,u);
}
}
void init(){
n=read();
for(int i = 1;i < n;i++){
int r1=read(),r2=read(),r3=read();
side s1;
s1.pos=i;s1.v=r2;s1.wt=r3;
tree[r1].push_back(s1);
s1.v=r1;
tree[r2].push_back(s1);
}
dfs(1,0);
dfs2(1,1,0);
}
int LCA(int a,int b){
if(dep[a]<dep[b])swap(a,b);
for(int i = 15;i >= 0;i--){
if(dep[a]-(1<<i)>=dep[b]){
a = fa[a][i];
}
}
if(a==b) return a;
for(int i = 15;i >= 0;i--){
if(fa[a][i]!=fa[b][i]){
a = fa[a][i],b = fa[b][i];
}
}
return fa[a][0];
}
int find(int *tmax,int i,int l,int r,int ll,int rr){
if(l >= ll&&r <= rr){
return tmax[i];
}
if(l > rr||r < ll){
return -INF;
}
int mid = (l+r)>>1;
return max(find(tmax,i<<1,l,mid,ll,rr),find(tmax,(i<<1)+1,mid+1,r,ll,rr));
}
int query(int lca,int a){
int ret = 0;
while(lca!=a){
if(bl[lca] == bl[a]){ret = max(find(root[bl[a]],1,1,len1[bl[a]],pos[lca]+1,pos[a]),ret);break;}
else ret = max(find(root[bl[a]],1,1,len1[bl[a]],1,pos[a]),ret),a = fa[bl[a]][0];
}
return ret;
}
void solve(){
char c[10];int a,b,num=0;
while(scanf("%s",c),c[0]!='D'){
a=read(),b = read();
if(c[0] =='Q'){
if(++num == 49){
int t = 0;
}
int lca = LCA(a,b);
printf("%d\n",max(query(lca,a),query(lca,b)));
}else{
wt[idx[a]] = b;
insert(root[bl[idx[a]]],1,1,len1[bl[idx[a]]],pos[idx[a]],b);
}
}
}
int main(){
// freopen("4114.in","r",stdin);
// freopen("4114.out","w",stdout);
init();
solve();
return 0;
}