You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3…N-1.
We will 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 on 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:
In the first line there is 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 is signified by the string “DONE”.
There is one blank line between successive tests.
Output
For each “QUERY” operation, 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(1,1);
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;
}