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 ab 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

Hint

Added by: Thanh-Vy Hua
Date: 2005-06-08
Time limit: 0.851s
Source limit: 15000B
Memory limit: 1536MB
Cluster: Cube (Intel G860)
Languages: ADA ASM BASH BF C C# C++ 5 CLPS LISP sbcl LISP clisp D FORT HASK ICON ICK JAVA LUA NEM NICE CAML PAS gpc PAS fpc PERL PHP PIKE PRLG PYTH 2.7 RUBY SCM qobi SCM guile ST TEXT WSPC

树链剖分模板题。

维护单边权修改,查询链上边权最大值。

树链剖分是以点为基本单位的,需要维护边时,可以将边映射到它深度较大的那个端点上。查询时,不能经过LCA结点(因为该点对应的边不在所求链上)。

 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#define lc rt<<1
#define rc rt<<1|1
using namespace std;
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int T;
int n,m;
int pe[mxn][];
struct edge{
int v,nxt,w;
}e[mxn<<];
int hd[mxn],mct=;
void add_edge(int u,int v,int d){
e[++mct].v=v;e[mct].nxt=hd[u];e[mct].w=d;hd[u]=mct;return;
}
struct node{
int f,son;
int top,size;
int w,e,dep;
}tr[mxn];
int sz=;
void DFS1(int u){
tr[u].size=;
tr[u].son=;
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v==tr[u].f)continue;
tr[v].dep=tr[u].dep+;
tr[v].f=u;
DFS1(v);
tr[u].size+=tr[v].size;
if(tr[v].size>tr[tr[u].son].size)tr[u].son=v;
}
return;
}
void DFS2(int u,int top){
tr[u].top=top;
tr[u].w=++sz;
if(tr[u].son){
DFS2(tr[u].son,top);
for(int i=hd[u];i;i=e[i].nxt){
int v=e[i].v;
if(v!=tr[u].f && v!=tr[u].son){
DFS2(v,v);
}
}
}
tr[u].e=sz;
}
//
struct segtree{
int mx;
}st[mxn<<];
void change(int p,int v,int l,int r,int rt){
if(l==r){
if(l==p)
st[rt].mx=v;
return;
}
int mid=(l+r)>>;
if(p<=mid)change(p,v,l,mid,lc);
else change(p,v,mid+,r,rc);
st[rt].mx=max(st[lc].mx,st[rc].mx);
return;
}
int qmx(int L,int R,int l,int r,int rt){
if(L<=l && r<=R)return st[rt].mx;
int mid=(l+r)>>;
int res=-1e9;
if(L<=mid)res=max(res,qmx(L,R,l,mid,lc));
if(R>mid)res=max(res,qmx(L,R,mid+,r,rc));
return res;
}
int query(int x,int y){
int res=-1e9;
while(tr[x].top!=tr[y].top){
if(tr[tr[x].top].dep<tr[tr[y].top].dep)swap(x,y);
res=max(res,qmx(tr[tr[x].top].w,tr[x].w,,n,));
x=tr[tr[x].top].f;
}
if(tr[x].dep>tr[y].dep)swap(x,y);
if(x!=y)res=max(res,qmx(tr[tr[x].son].w,tr[y].w,,n,));//不经过公共祖先
return res;
}
//
void init(){
memset(st,,sizeof st);
memset(hd,,sizeof hd);
mct=;sz=;
}
int main(){
T=read();
int i,j,x,y,z;
while(T--){
init();
n=read();
int rt=n/+;
for(i=;i<n;i++){
x=read();y=read();z=read();
add_edge(x,y,z);
add_edge(y,x,z);
pe[i][]=x;pe[i][]=y;pe[i][]=z;//记录边信息
}
tr[rt].f=tr[rt].son=tr[rt].dep=;
DFS1(rt);
DFS2(rt,rt);
for(i=;i<n;i++){
if(tr[pe[i][]].dep>tr[pe[i][]].dep)swap(pe[i][],pe[i][]);
change(tr[pe[i][]].w,pe[i][],,n,);
}
char op[];
while(scanf("%s",op) && op[]!='D'){
if(op[]=='Q'){
x=read();y=read();
printf("%d\n",query(x,y));
}
if(op[]=='C'){
x=read();y=read();
change(tr[pe[x][]].w,y,,n,);
}
}
}
return ;
}

SPOJ375 Query on a tree的更多相关文章

  1. SPOJ375 Query on a tree(树链剖分)

    传送门 题意 给出一棵树,每条边都有权值,有两种操作: 把第p条边的权值改为x 询问x,y路径上的权值最大的边 code #include<cstdio> #include<algo ...

  2. SPOJ375 Query on a tree(LCT边权)

    之前做了两道点权的LCT,这次做一下边权的LCT.上网找了一下资料,发现对于边权的LCT有这么两种处理方法,一种是每条边建一个点,于是边权就转成点权了.另外一种则是每个边权对应到点权上,也就是每个点对 ...

  3. SPOJ375 Query on a tree 【倍增,在线】

    题目链接[http://www.spoj.com/problems/QTREE/] 题意:给出一个包含N(N<=10000)节点的无根树,有多次询问,询问的方式有两种1.DIST  a b 求a ...

  4. Query on a tree——树链剖分整理

    树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...

  5. SPOJ 375. Query on a tree (树链剖分)

    Query on a tree Time Limit: 5000ms Memory Limit: 262144KB   This problem will be judged on SPOJ. Ori ...

  6. QTREE3 spoj 2798. Query on a tree again! 树链剖分+线段树

    Query on a tree again! 给出一棵树,树节点的颜色初始时为白色,有两种操作: 0.把节点x的颜色置反(黑变白,白变黑). 1.询问节点1到节点x的路径上第一个黑色节点的编号. 分析 ...

  7. spoj 375 Query on a tree(树链剖分,线段树)

      Query on a tree Time Limit: 851MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Sub ...

  8. bzoj 3637: Query on a tree VI 树链剖分 && AC600

    3637: Query on a tree VI Time Limit: 8 Sec  Memory Limit: 1024 MBSubmit: 206  Solved: 38[Submit][Sta ...

  9. 动态树(Link Cut Tree) :SPOJ 375 Query on a tree

    QTREE - Query on a tree #number-theory You are given a tree (an acyclic undirected connected graph) ...

随机推荐

  1. 路由系统的核心对象——Router

    路由系统的核心对象--Router ASP.NET Core应用中的路由机制实现在RouterMiddleware中间件中,它的目的在于通过路由解析为请求找到一个匹配的处理器,同时将请求携带的数据以路 ...

  2. linux不同角色server分区方案

    服务器角色 分区建议 优点    RAID方案 单机服务器 如8G内存,300G硬盘        /boot 100-200M swap 16G,内存大小8G*2 / 80G /var 20G(也可 ...

  3. IntelliJ IDEA 13试用手记(附详细截图)

    从去年开始转java以来,一直在寻找一款趁手的兵器,eclipse虽然是很多java程序员的首选,但是我发现一旦安装了一些插件,workspace中的项目达到数10个以后,经常崩溃,实在影响编程的心情 ...

  4. 802.11 对于multicast 和 broadcast的处理

    ethernet内部会有broadcast 和 multicast.这两种包都是一个STA向多个STA发包. 当没有wifi存在的时候,LAN口之间的broadcast 和 multicast是可靠转 ...

  5. opencv3-core之基本操作

    这一篇打算将core部分的例子说完,这都是基于<opencv2.4.9tutorial.pdf>中的core部分,其实这些例子后期都很稳定的,也就是说就算是2.3.1和2.4.10 ,这几 ...

  6. Android 开发1000问笔记

    11.android使用全局变量 定义Data类继承Application 在manifest.xml中声明 http://blog.csdn.net/feiyangxiaomi/article/de ...

  7. WebConfig 详解

    一.Web.Config继承特性 首先我们就来看看配置文件的继承层次.都知道在ASP.NET中有很多的配置文件,如machine.config,web.config,特别是web.config出现在很 ...

  8. jQuery学习笔记(三):选择器总结

    这一节详细的总结jQuery选择器. 一.基础选择器 $('#info'); // 选择id为info的元素,id为document中是唯一的,因此可以通过该选择器获取唯一的指定元素$('.infoC ...

  9. Scala入门之函数

    /** * 函数可以被简单的被认为是包裹了一条或者几条语句的代码体,该代码体接收若干参数,经过代码体处理后返回结果,形如数学中的f(x) = x + 1 * 在Scala中函数式一等公民,可以向变量一 ...

  10. 使用docker发布spring cloud应用

    本文涉及到的项目: cloud-simple-docker:一个简单的spring boot应用 Docker是一种虚拟机技术,准确的说是在linux虚拟机技术LXC基础上又封装了一层,可以看成是基于 ...