#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 2e4+20; const int inf = 2e9; int n,m,top; int fa[maxn],c[maxn][2]; int val[maxn], mx[maxn], u[maxn], v[maxn]; int q[maxn]; bool rev[maxn]; int lz[maxn]; int edge[maxn]; int id; int tot, head[maxn]; void init(){ tot = 0; memset(head, -1, sizeof(head)); memset(lz, 0, sizeof(lz)); memset(mx, 0, sizeof(mx)); memset(rev, 0, sizeof(rev)); memset(c, 0, sizeof(c)); memset(fa, 0, sizeof(fa)); } bool isroot(int x) { return c[fa[x]][0]!=x && c[fa[x]][1]!=x; } void update(int x) { int l=c[x][0], r=c[x][1]; mx[x]=max(mx[l], mx[r]); if(x>n) mx[x]=max(mx[x],val[x]); } void pushdown(int x) { int l=c[x][0],r=c[x][1]; if(rev[x]) { rev[x]^=1;rev[l]^=1;rev[r]^=1; swap(c[x][0],c[x][1]); } } void rotate(int x) { int y=fa[x],z=fa[y],l,r; l=(c[y][1]==x);r=l^1; if(!isroot(y))c[z][c[z][1]==y]=x; fa[c[x][r]]=y;fa[y]=x;fa[x]=z; c[y][l]=c[x][r];c[x][r]=y; update(y);update(x); } void splay(int x) { top = 0; q[++top]=x; for(int i=x;!isroot(i);i=fa[i]) q[++top]=fa[i]; while(top)pushdown(q[top--]); while(!isroot(x)) { int y=fa[x],z=fa[y]; if(!isroot(y)) { if(c[y][0]==x^c[z][0]==y)rotate(x); else rotate(y); } rotate(x); } } void access(int x) { for(int t=0;x;t=x,x=fa[x]) splay(x),c[x][1]=t,update(x); } void makeroot(int x) { access(x);splay(x);rev[x]^=1; } void link(int x,int y) { makeroot(x);fa[x]=y; } void split(int x,int y) { makeroot(x);access(y);splay(y); } void cut(int x,int y) { split(x, y); c[y][0]=fa[c[y][0]]=0; } int find(int x){ access(x);splay(x); while(c[x][0])x=c[x][0]; return x; } int main() { int _; scanf("%d", &_); while(_--){ init(); scanf("%d", &n); id = n+1; int w; for(int i=1; i<n; i++){ scanf("%d%d%d", &u[i], &v[i], &w); edge[i] = id++; mx[id-1] = val[id-1] = w; link(u[i], id-1), link(v[i], id-1); } char op[10]; int a, b; while(scanf("%s", op)){ if(op[0] == 'D'){ break; } else if(op[0] == 'Q'){ scanf("%d%d", &a, &b); split(a, b); printf("%d\n", mx[b]); } else{ scanf("%d%d", &a, &b); splay(edge[a]); val[edge[a]] = b; update(edge[a]); } } } return 0; }
|