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.
树链剖分模板题。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int fir[10010],ne[20010],to[20010],len[20010],n,
pos[10010],top[10010],num[10010],
size[10010],son[20010],fa[20010],dep[10010],val[20010],
mx[100010],tot;
void add(int num,int u,int v,int l)
{
ne[num]=fir[u];
fir[u]=num;
to[num]=v;
len[num]=l;
}
void init()
{
int i,x,y,z;
memset(fir,0,sizeof(fir));
memset(ne,0,sizeof(ne));
scanf("%d",&n);
for (i=1;i<n;i++)
{
scanf("%d%d%d",&x,&y,&z);
add(i*2,x,y,z);
add(i*2+1,y,x,z);
}
}
void dfs1(int u,int f)
{
int i,v;
size[u]=1;
for (i=fir[u];i;i=ne[i])
if ((v=to[i])!=f)
{
dep[v]=dep[u]+1;
fa[v]=u;
dfs1(v,u);
size[u]+=size[v];
if (son[u]==0||size[son[u]]<size[v])
son[u]=v;
}
}
void dfs2(int u,int f)
{
int i;
if (son[u])
{
pos[son[u]]=++tot;
top[son[u]]=top[u];
for (i=fir[u];i;i=ne[i])
if (to[i]==son[u])
val[tot]=len[i],num[i/2]=tot;
dfs2(son[u],u);
}
for (i=fir[u];i;i=ne[i])
if (to[i]!=son[u]&&to[i]!=f)
{
pos[to[i]]=++tot;
val[tot]=len[i];
num[i/2]=tot;
top[to[i]]=to[i];
dfs2(to[i],u);
}
}
void build(int p,int L,int R)
{
if (L==R)
{
mx[p]=val[L];
return;
}
int mid=(L+R)/2;
build(p*2,L,mid);
build(p*2+1,mid+1,R);
mx[p]=max(mx[p*2],mx[p*2+1]);
}
void pre()
{
dep[1]=1;
memset(son,0,sizeof(son));
dfs1(1,-1);
tot=0;
top[1]=1;
dfs2(1,-1);
build(1,1,tot);
}
void modi(int p,int L,int R,int k,int x)
{
if (L==R)
{
mx[p]=x;
return;
}
int mid=(L+R)/2;
if (k<=mid) modi(p*2,L,mid,k,x);
else modi(p*2+1,mid+1,R,k,x);
mx[p]=max(mx[p*2],mx[p*2+1]);
}
int qry(int p,int L,int R,int l,int r)
{
if (l<=L&&r>=R) return mx[p];
int ret=0,mid=(L+R)/2;
if (l<=mid) ret=max(ret,qry(p*2,L,mid,l,r));
if (r>mid) ret=max(ret,qry(p*2+1,mid+1,R,l,r));
return ret;
}
int query(int u,int v)
{
int f1,f2,ret=0;
while ((f1=top[u])!=(f2=top[v]))
{
if (dep[f1]<dep[f2])
{
swap(u,v);
swap(f1,f2);
}
ret=max(ret,qry(1,1,tot,pos[f1],pos[u]));
u=fa[f1];
}
if (u==v) return ret;
if (dep[u]<dep[v])
swap(u,v);
ret=max(ret,qry(1,1,tot,pos[son[v]],pos[u]));
return ret;
}
int main()
{
int T,x,y;
char s[10];
scanf("%d",&T);
while (T--)
{
init();
pre();
while (scanf("%s",s)&&s[0]!='D')
{
scanf("%d%d",&x,&y);
if (s[0]=='C') modi(1,1,tot,num[x],y);
else printf("%d\n",query(x,y));
}
}
}