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 <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <queue>
#define mem(p,k) memset(p,k,sizeof(p));
#define rep(a,b,c) for(int a=b;a<c;a++)
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 0x6fffffff
#define ll long long
using namespace std;
const int N=50010;
struct node{
int to,next;
}e[N*2];
int eg[N][3];
int edge,root,id,n;
int first[N],siz[N],dep[N],fa[N],son[N],tp[N],d[N],w[N];
int tree[N<<2];
void add(int a,int b){
e[++edge].to=b;
e[edge].next=first[a];
first[a]=edge;
}
void dfs(int x){
siz[x]=1;son[x]=0;
for(int i=first[x]; ~i ;i = e[i].next){
if(e[i].to==fa[x])continue;
fa[e[i].to]=x;
dep[e[i].to]=dep[x]+1;
dfs(e[i].to);
if(siz[e[i].to]>siz[son[x]]){
son[x]=e[i].to;
}
siz[x]+=siz[e[i].to];
}
}
void build_tree(int x,int top){
w[x]=++id;
tp[x]=top;
if(son[x]){
build_tree(son[x],top);
}
for(int i=first[x]; ~i ;i = e[i].next){
if(e[i].to==fa[x] || e[i].to==son[x])continue;
build_tree(e[i].to,e[i].to);
}
}
void build(int l,int r,int rt){
if(l==r){
tree[rt]=d[l];return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}
void update(int k,int c,int l,int r,int rt){
if(k==l&& k==r){
tree[rt]=c;return;
}
int m=(l+r)>>1;
if(k<=m)update(k,c,lson);
else update(k,c,rson);
tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}
int query(int L,int R,int l,int r,int rt){
if(L<=l && R>=r){
return tree[rt];
}
int m=(l+r)>>1,maxx=0;
if(L<=m) maxx=max(maxx,query(L,R,lson));
if(R>m) maxx=max(maxx,query(L,R,rson));
return maxx;
}
void Query(int x,int y){
int fx=tp[x],fy=tp[y],maxx=0;
while(fx!=fy){
if(dep[fx]<dep[fy]){
swap(fx,fy);swap(x,y);
}
maxx=max(maxx,query(w[fx],w[x],1,id,1));
x=fa[fx];
fx=tp[x];
}
if(x!=y){
if(dep[x]<dep[y]){
swap(x,y);
}
maxx=max(maxx,query(w[son[y]],w[x],1,id,1));
}
printf("%d\n",maxx);
}
void init(){
int a,b,c;
scanf("%d",&n);
edge=0;
id=0;
root=n/2+1;
fa[root]=dep[root]=tp[root]=0;
mem(first,-1);
for(int i=1;i<n;i++){
scanf("%d%d%d",&a,&b,&c);
eg[i][0]=a;eg[i][1]=b;eg[i][2]=c;
add(a,b);
add(b,a);
}
dfs(root);
build_tree(root,root);
for(int i=1;i<n;i++){
if(dep[eg[i][0]]<dep[eg[i][1]])swap(eg[i][0],eg[i][1]);
d[w[eg[i][0]]]=eg[i][2];
}
d[w[root]]=0;
build(1,id,1);
//for(int i=1;i<=n;i++)cout<<i<<"=="<<w[i]<<endl;
}
void solve(){
char s[11];
int x,y;
while(scanf("%s",s) && s[0]!='D'){
scanf("%d%d",&x,&y);
if(s[0]=='C'){
update(w[eg[x][0]],y,1,id,1);
}
else{
Query(x,y);
}
}
}
int main(){
int t;
cin>>t;
while(t--){
init();
solve();
}
return 0;
}
/*
5
5
1 2 1
2 3 4
2 4 2
4 5 3
*/