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

给一棵共有 n(n · 10000) 个结点的树, 每条边都有一个权值, 要求维护一个数据结构, 支持如下操作:

  1. 修改某条边的权值;
  2. 询问某两个结点之间的唯一通路上的最大边权.

    其中操作的总次数为 q.

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.

Sample Input

1

3

1 2 1

2 3 2

QUERY 1 2

CHANGE 1 3

QUERY 1 2

DONE

Sample Output

1

3


一道树链剖分的板子题,如果不会的话可以看浅谈算法——树链剖分

稍微需要注意的就是要将边权全部都下放到点权上去,这样的话方便维护

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x>=10) print(x/10);
putchar(x%10+'0');
}
const int N=1e4;
int dfn[N+10],ID[N+10],V[N+10],line[N+10],n;
struct Segment{
#define ls (p<<1)
#define rs (p<<1|1)
int tree[(N<<2)+10];
void init(){memset(tree,128,sizeof(tree));}
void build(int p,int l,int r){
if (l==r){
tree[p]=V[dfn[l]];
return;
}
int mid=(l+r)>>1;
build(ls,l,mid),build(rs,mid+1,r);
tree[p]=max(tree[ls],tree[rs]);
}
void change(int p,int l,int r,int x,int v){
if (l==r){
tree[p]=v;
return;
}
int mid=(l+r)>>1;
if (x<=mid) change(ls,l,mid,x,v);
else change(rs,mid+1,r,x,v);
tree[p]=max(tree[ls],tree[rs]);
}
int Query(int p,int l,int r,int x,int y){
if (x<=l&&r<=y) return tree[p];
int mid=(l+r)>>1,res=0;
if (x<=mid) res=max(res,Query(ls,l,mid,x,y));
if (y>mid) res=max(res,Query(rs,mid+1,r,x,y));
return res;
}
}Tree;
struct S1{
int pre[(N<<1)+10],now[N+10],child[(N<<1)+10],val[(N<<1)+10],tot,cnt;
int fa[N+10],deep[N+10],top[N+10],size[N+10],Rem[N+10];
void join(int x,int y,int z){pre[++tot]=now[x],now[x]=tot,child[tot]=y,val[tot]=z;}
void insert(int x,int y,int z){join(x,y,z),join(y,x,z);}
void dfs(int x,int Deep){
int T=0; size[x]=1,deep[x]=Deep;
for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
if (son==fa[x]) continue;
fa[son]=x,V[son]=val[p],line[(p+1)>>1]=son;
dfs(son,Deep+1);
size[x]+=size[son];
if (T<size[son]) T=size[son],Rem[x]=son;
}
}
void build(int x){
if (!x) return;
top[x]=Rem[fa[x]]==x?top[fa[x]]:x;
dfn[ID[x]=++cnt]=x;
build(Rem[x]);
for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
if (son==fa[x]||son==Rem[x]) continue;
build(son);
}
}
int Query(int x,int y){
int res=-inf;
while (top[x]!=top[y]){
if (deep[top[x]]<deep[top[y]]) swap(x,y);
res=max(res,Tree.Query(1,1,n,ID[top[x]],ID[x]));
x=fa[top[x]];
}
if (x==y) return res;
if (deep[x]>deep[y]) swap(x,y);
res=max(res,Tree.Query(1,1,n,ID[Rem[x]],ID[y]));
return res;
}
void init(){
tot=cnt=0;
memset(fa,0,sizeof(fa));
memset(now,0,sizeof(now));
memset(Rem,0,sizeof(Rem));
}
}T;
void init(){
Tree.init();
T.init();
}
char s[10];
int main(){
for (int Data=read();Data;Data--){
init();
n=read();
for (int i=1;i<n;i++){
int x=read(),y=read(),z=read();
T.insert(x,y,z);
}
T.dfs(1,1),T.build(1),Tree.build(1,1,n);
while (true){
scanf("%s",s);
if (s[0]=='D') break;
int x=read(),y=read();
if (s[0]=='Q') printf("%d\n",T.Query(x,y));
if (s[0]=='C') Tree.change(1,1,n,ID[line[x]],y);
}
}
return 0;
}

最新文章

  1. October 20th Week 43rd Thursday, 2016
  2. .NET面试题解析(11)-SQL语言基础及数据库基本原理
  3. Auto Layout
  4. Java中必须了解的常用类
  5. MySQL Database on Azure - 利用PowerShell快速创建使用数据库服务
  6. php 文件缓存
  7. 从头开始编写一个Orchard网上商店模块(6) - 创建购物车服务和控制器
  8. NODE JS拼命吹,我就不喜欢. 别问为什么,直觉.
  9. this——笔记
  10. 一个很简单的jQuery插件实例教程(菜鸟级)
  11. SQL 如何表示引号
  12. BZOJ 3403: [Usaco2009 Open]Cow Line 直线上的牛( deque )
  13. 分块编码(Transfer-Encoding: chunked)
  14. java 局部变量几点笔记
  15. Linux 高速操作IOport
  16. 易错点---所有的字符都自带bool值
  17. Linux电源管理-Linux regulator framework概述
  18. 前端select动态加载
  19. Linux文件浏览命令
  20. java 线程Thread 技术--1.5Lock 与condition 演示生产者与消费模式

热门文章

  1. Java 等额本金等额本息工具类
  2. 【APUE】【转】守护进程编写
  3. Centos5.11 //IP/phpmyadmin 远程无法登入
  4. VC++中的int main(int argc, char argv[])是什么意思
  5. ldd
  6. 【Java 虚拟机探索之路系列】:JIT编译器
  7. &amp;lt;Android&amp;gt;greenrobot-EventBus,guava-Event Bus的异步实现
  8. fedora下安装xdot和objgraph
  9. LoaderManager使用具体解释(四)---实例:AppListLoader
  10. udhcp源码详解(三)上 之配置信息的读取