题意:
Problem code: QTREE |
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
解法:传说之中仰望依旧的树链剖分,根据节点的规模将一棵树进行剖分,然后分为若干条链。这些链可以看做是一条一条的线段,对应到一条长线段之中。然后用线段树维护,看起来很复杂其实并不难,尤其是这一道题的只用到最简单单点更新的线段树,难度不大。详情见代码//
// main.cpp
// query on a tree
//
// Created by 蘇與軒 on 15/2/13.
// Copyright (c) 2015年 蘇與軒. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <functional>
#define rep(i,a,b) for (int i=a;i<((b)+1);i++)
#define Rep(i,a,b) for (int i=a;i>=b;i--)
#define foreach(e,x) for (__typeof(x.begin()) e=x.begin();e!=x.end();e++)
#define mid ((l+r)>>1)
#define lson (k<<1)
#define rson (k<<1|1)
#define MEM(a,x) memset(a,x,sizeof a)
using namespace std;
const int N=20050;
typedef pair<int, int> pii;
typedef long long ll;
int pnt[N],head[N],nxt[N],cnt,T,n,re[N][3],root;
int tree[N],son[N],top[N],size[N],fa[N],dep[N],w[N],z;
void addedge(int u,int v,int c) {
pnt[cnt]=v;nxt[cnt]=head[u];head[u]=cnt++;
}
void dfs(int u) {
size[u]=1;son[u]=0;
for (int i=head[u];i!=-1;i=nxt[i]) {
int v=pnt[i];
if (v!=fa[u]){
fa[v]=u;
dep[v]=dep[u]+1;
dfs(v);
if (size[v]>size[son[u]]) son[u]=v;
size[u]+=size[v];
}
}
}
void build_tree(int u,int tp) {
top[u]=tp;w[u]=++z;
if (son[u]!=0) build_tree(son[u], tp);
for (int i=head[u];i!=-1;i=nxt[i]) {
int v=pnt[i];
if (v!=son[u]&&v!=fa[u]) build_tree(v, v);
}
}
void update(int k,int l,int r,int pos,int x) {
if (pos>r||pos<l) return ;
if (l==r) {
tree[k]=x;
return ;
}
update(lson,l,mid,pos,x);
update(rson, mid+1, r, pos, x);
tree[k]=max(tree[lson],tree[rson]);
}
int query(int k,int l,int r,int ll,int rr) {
if (ll>r||rr<l) return 0;
if (ll<=l&&rr>=r) return tree[k];
return max(query(lson,l,mid,ll,rr),query(rson, mid+1, r, ll, rr));
}
int find(int x,int y) {
int f1=top[x],f2=top[y],res=0;
while (f1!=f2) {
if (dep[f1]<dep[f2]){
swap(f1,f2);
swap(x,y);
}
res=max(res,query(1,1,z,w[f1],w[x]));
x=fa[f1];f1=top[x];
}
if (x==y) return res;
if (dep[x]>dep[y]) swap(x, y);
return max(res,query(1, 1, z, w[son[x]], w[y]));
}
int main(int argc, const char * argv[]) {
scanf("%d",&T);
while (T--) {
scanf("%d",&n);
MEM(head,-1);MEM(re,0);MEM(nxt,-1);MEM(size,0);MEM(tree,0);
root=(n+1)/2;
fa[root]=cnt=z=dep[root]=0;
rep(i,1,n-1) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
re[i][0]=a;re[i][1]=b;re[i][2]=c;
}
dfs(root);
build_tree(root,root);
rep(i,1,n-1) {
if (dep[re[i][0]]>dep[re[i][1]]) swap(re[i][0],re[i][1]);
update(1,1,z,w[re[i][1]],re[i][2]);
}
char s[20];
while (~scanf("%s",s)) {
if (s[0]=='D') break;
int a,b;
scanf("%d%d",&a,&b);
if (s[0]=='Q') printf("%d\n",find(a,b));
else update(1, 1, z, w[re[a][1]], b);
}
}
return 0;
}