#include <bits/stdc++.h> using namespace std; const int maxn = 1e4+10; struct Edge{ int to, next; }edge[maxn<<1]; int tot, head[maxn]; void add_edge(int u, int v){ edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++; } int pos; int top[maxn]; int fa[maxn]; int deep[maxn]; int num[maxn]; int p[maxn]; int rp[maxn]; int son[maxn]; void init(){ memset(head, -1, sizeof(head)); tot = 0; pos = 0; memset(son, -1, sizeof(son)); } void dfs1(int u, int pre, int d){ deep[u] = d; fa[u] = pre; num[u] = 1; for(int i=head[u]; ~i; i = edge[i].next){ int v = edge[i].to; if(v!=pre){ dfs1(v, u, d+1); num[u] += num[v]; if(son[u] == -1||num[v]>num[son[u]]){ son[u] = v; } } } } void getpos(int u, int sp){ top[u] = sp; p[u] = pos++; rp[p[u]] = u; if(son[u] == -1) return; getpos(son[u], sp); for(int i=head[u]; ~i; i = edge[i].next){ int v = edge[i].to; if(v!=son[u]&&v!=fa[u]){ getpos(v, v); } } } struct Node{ int l, r; int Max; }node[maxn<<2]; void build(int rt, int l, int r){ node[rt].l = l; node[rt].r = r; node[rt].Max = 0; if(l == r) return; int mid = (l+r)/2; build(rt<<1, l, mid); build((rt<<1)|1, mid+1, r); } void push_up(int rt){ node[rt].Max = max(node[rt<<1].Max, node[(rt<<1)|1].Max); } void update(int rt, int k, int val){ if(node[rt].l == k&&node[rt].r == k){ node[rt].Max = val; return; } int mid = (node[rt].l+node[rt].r)/2; if(k<=mid) update(rt<<1, k, val); else update((rt<<1)|1, k, val); push_up(rt); } int query(int rt, int l, int r){ if(node[rt].l == l&&node[rt].r == r){ return node[rt].Max; } int mid = (node[rt].l+node[rt].r)/2; if(r<=mid)return query(rt<<1, l, r); else if(l>mid) return query((rt<<1)|1, l, r); else return max(query(rt<<1, l, mid), query((rt<<1)|1, mid+1, r)); } int find(int u, int v){ int f1 = top[u], f2 = top[v]; int temp = 0; while(f1!=f2){ if(deep[f1]<deep[f2]){ swap(f1, f2); swap(u, v); } temp = max(temp, query(1, p[f1], p[u])); u = fa[f1]; f1 = top[u]; } if(u == v) return temp; if(deep[u]>deep[v]) swap(u, v); return max(temp, query(1, p[son[u]], p[v])); } int e[maxn][3]; int main() { ios::sync_with_stdio(false); int t, n; cin>>t; while(t--){ init(); cin>>n; for(int i=0; i<n-1; i++){ cin>>e[i][0]>>e[i][1]>>e[i][2]; add_edge(e[i][0], e[i][1]); add_edge(e[i][1], e[i][0]); } dfs1(1, 0, 0); getpos(1, 1); build(1, 0, pos-1); for(int i=0; i<n-1; i++){ if(deep[e[i][0]]>deep[e[i][1]]){ swap(e[i][0], e[i][1]); } update(1, p[e[i][1]], e[i][2]); } string op; int u, v; while(cin>>op){ if(op[0] == 'D') break; cin>>u>>v; if(op[0] == 'Q'){ printf("%d\n", find(u, v)); } else{ update(1, p[e[u-1][1]], v); } } } return 0; }
|