Description
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
Solution
存一下模板QvQ
代码能力太渣。。
#include<iostream>
#include<cstring>
#include<cstdio>
#define Max(a,b) (a>b?a:b)
#define MAXN 10005
using namespace std;
int T,n;
int head[MAXN],cnt,e[MAXN][3],sz;
int top[MAXN],siz[MAXN],dep[MAXN],father[MAXN],ans,pos[MAXN];
char opt[15];
struct Node{
int next,to;
}Edges[MAXN*2];
void addedge(int u,int v)
{
Edges[++cnt].next=head[u];
head[u]=cnt;
Edges[cnt].to=v;
}
void dfs1(int u)
{
siz[u]=1;
for(int i=head[u];~i;i=Edges[i].next)
{
int v=Edges[i].to;
if(v!=father[u])
{
father[v]=u;
dep[v]=dep[u]+1;
dfs1(v);
siz[u]+=siz[v];
}
}
}
void dfs2(int u,int t)
{
int k,maxsiz=0;
sz++;pos[u]=sz;top[u]=t;
for(int i=head[u];~i;i=Edges[i].next)
{
int v=Edges[i].to;
if(v!=father[u]&&siz[v]>maxsiz)
k=v,maxsiz=siz[v];
}
if(!maxsiz)return;
dfs2(k,t);
for(int i=head[u];~i;i=Edges[i].next)
{
int v=Edges[i].to;
if(v!=k&&v!=father[u])
dfs2(v,v);
}
}
struct Segtree{
int l,r,max;
}t[MAXN*4];
void build(int idx,int l,int r)
{
t[idx].l=l,t[idx].r=r;
if(l==r)return;
int mid=(l+r)/2;
build(idx*2,l,mid);
build(idx*2+1,mid+1,r);
}
void change(int idx,int a,int b)
{
if(t[idx].l==t[idx].r)
{
t[idx].max=b;
return;
}
int mid=(t[idx].l+t[idx].r)/2;
if(a<=mid)change(idx*2,a,b);
else change(idx*2+1,a,b);
t[idx].max=Max(t[idx*2].max,t[idx*2+1].max);
}
void query(int idx,int a,int b)
{
if(t[idx].l>=a&&t[idx].r<=b)
{
ans=Max(ans,t[idx].max);
return;
}
int mid=(t[idx].l+t[idx].r)/2;
if(a>mid)query(idx*2+1,a,b);
else if(b<=mid)query(idx*2,a,b);
else
{
query(idx*2,a,b);
query(idx*2+1,a,b);
}
}
int main()
{
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
cnt=sz=0;
memset(head,-1,sizeof(head));
for(int i=1;i<n;i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
addedge(a,b);
addedge(b,a);
e[i][0]=a,e[i][1]=b,e[i][2]=c;
}
dfs1(1);dfs2(1,1);
build(1,1,sz);
for(int i=1;i<n;i++)
{
if(dep[e[i][0]]<dep[e[i][1]])swap(e[i][0],e[i][1]);
change(1,pos[e[i][0]],e[i][2]);
}
for(;;)
{
int a,b;
scanf("%s",opt);
if(opt[0]=='D')break;
if(opt[0]=='C')
{
scanf("%d%d",&a,&b);
if(dep[e[a][0]]<dep[e[a][1]])swap(e[a][0],e[a][1]);
change(1,pos[e[a][0]],b);
}
else if(opt[0]=='Q')
{
ans=0;
scanf("%d%d",&a,&b);
while(top[a]!=top[b])
{
if(dep[top[a]]<dep[top[b]])swap(a,b);
query(1,pos[top[a]],pos[a]);
a=father[top[a]];
}
if(dep[a]>dep[b])swap(a,b);
if(a!=b)query(1,pos[a]+1,pos[b]);
printf("%d\n",ans);
}
}
}
return 0;
}