You were given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3 ... N-1.
We'll ask you to perfrom some instructions of the following form:
Change I ti:change the cost of the i-th edge to Ti
Or
QUERY a b:ask for the maximum edge cost in the path from Node A to Node B
Input
The first line of input contains an integer t, the number of test cases (T <= 20). T test cases follow.
For each test case:
The first line there are an integer n (n <= 10000),
In the next N-1 lines, the i-th line describes the i-th edge:a line with three integers a b c denotes an edge between a, B of cost C (C <= 1000000),
The next lines contain instructions "Change I ti" or "QUERY a B",
The end of each test case was signified by the string "done".
There is one blank line between successive tests.
Output
For each "QUERY" operation, the write one integer representing its result.
Example
Input:
1
3
1 2 1
2 3 2
QUERY 1 2
Change 1 3
QUERY 1 2
Done
Output:
1
3
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std;
#define Lson l,m,rt<<1 #define RSON m+1,r,rt<<1|1 const int maxn=10009;
Vector<int> MP[MAXN];
int SZ[MAXN],FA[MAXN],TP[MAXN],DEP[MAXN],SON[MAXN],ID[MAXN],IDX=0,VAL[MAXN];
int t,n;
Char op[20]; struct Edge {int u,v,c;}
E[MAXN];
void dfs1 (int u,int f,int d) {sz[u]=1;
Fa[u]=f;
Dep[u]=d;
son[u]=0;
for (int i=0;i<mp[u].size (); i++) {int v=mp[u][i];
if (v==f) continue;
DFS1 (v,u,d+1);
SZ[U]+=SZ[V]; if (son[u]==0| |
SZ[SON[U]]<SZ[V]) Son[u]=v;
}} void dfs2 (int u,int TPP) {TP[U]=TPP;
Id[u]=++idx;
if (Son[u]) DFS2 (SON[U],TPP);
for (int i=0;i<mp[u].size (); i++) {int v=mp[u][i]; if (v==fa[u]| |
V==son[u]) continue;
DFS2 (V,V);
}} int tv[maxn<<2]; void pushup (int rt) {Tv[rt]=max (tv[rt<<1],tv[rt<<1|1]);} VOID build (int l,int R,int RT) {if (l==r) {tv[rt]=val[l];
Return
} int m=l+r>>1;
Build (Lson);
Build (Rson);
Pushup (RT);
} void Update (int p,int v,int l,int r,int RT) {if (l==r) {tv[rt]=v;
Return
} int m=l+r>>1;
if (p<=m) update (P,v,lson);
else update (P,v,rson);
Pushup (RT);
} int query (int l,int r,int l,int r,int RT) {if (l<=l&&r<=r) return TV[RT];
int m=l+r>>1;
int ans=-0x3f3f3f3f;
if (l<=m) Ans=max (Ans,query (L,r,lson));
if (m<r) Ans=max (Ans,query (L,r,rson));
return ans;
} int qq (int u,int v) {int t1=tp[u],t2=tp[v];
int ans=-0x3f3f3f3f;
while (T1!=T2) {if (Dep[t1]<dep[t2]) {swap (U,V);
Swap (T1,T2);
} ans=max (Query (id[t1],id[u],1,idx,1), ans);
U=FA[T1];
T1=tp[u];
} if (u==v) return ans; if (Dep[u]>dep[v]) Swap (U,V);
Ans=max (Query (id[son[u]],id[v],1,idx,1), ans);
return ans;
} int main () {scanf ("%d", &t);
while (t--) {idx=0;
scanf ("%d", &n);
for (int i=1;i<=n;i++) mp[i].clear ();
for (int i=1;i<n;i++) {scanf ("%d%d%d", &e[i].u,&e[i].v,&e[i].c);
Mp[e[i].u].push_back (E[I].V);
Mp[e[i].v].push_back (E[I].U);
} DFS1 (1,0,1);
DFS2 (a);
for (int i=1;i<n;i++) {if (DEP[E[I].U]<DEP[E[I].V]) swap (E[I].U,E[I].V);
VAL[ID[E[I].U]]=E[I].C;
} build (1,idx,1);
while (1) {scanf ("%s", op);
if (op[0]== ' D ') break;
int A, B;
scanf ("%d%d", &a,&b);
if (op[0]== ' Q ') {printf ("%d\n", QQ (A, b));
} else {update (id[e[a].u],b,1,idx,1); }}} return 0;
}