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. ELK+FileBeat+Log4Net

    ELK+FileBeat+Log4Net搭建日志系统 output { elasticsearch { hosts => ["localhost:9200"] } stdou ...

  2. Android 常用的adb命令

    1.安装APK(如果加 -r 参数,保留已设定数据,重新安装filename.apk) adb install xxx.apk adb install -r xxx.apk 2.卸载APK(如果加 - ...

  3. Linux Linux程序练习十三(信号阻塞,捕获)

    /* * 题目: * 请编写一个程序,设置SIGINT和SIGQUIT信号, * 并在该程序中实现从文件中读取信息的操作, * 并保证在读取文件且只有在读取文件的过程中不会被发送的SIGINT和SIG ...

  4. CoffeeScript的类继承的工具函数extends

    __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { // 派生类时,如果基类的类属性值是对象,那么子类的类属性只是 ...

  5. 打印机设置(PrintDialog)、页面设置(PageSetupDialog) 及 RDLC报表如何选择指定打印机

    如果一台电脑同时连接多个打印机,而且每个打印机使用的纸张大小各不相同(比如:票据打印钱用的小票专用张,办公打印机用的是A4标准纸),在处理打印类的需求时,如果不用代码干预,用户必须每次打印时,都必须在 ...

  6. mac搭建mamp环境

    1 先安装homebrew; 执行:cd /usr/local; 非root用户执行: ruby -e "$(curl -fsSL https://raw.githubusercontent ...

  7. Simple File System

    This is my operating system class design. scanner.h #include<string> using namespace std; #pra ...

  8. 【python】 [基础] 数据类型,字符串和编码

    python笔记,写在前面:python区分大小写1.科学计数法,把10用e代替,1.23x10·9就是 1.23e9                            或者 0.00012就是1 ...

  9. 10-xargs 简明笔记

    从标准你输入获取内容创建和执行命令 xargs     [options] 选项 -n                                               数字,分组 示例 x ...

  10. redis HA高可用方案Sentinel和shard

    1.搭建redis-master.redis-slave以及seninel哨兵监控 在最小配置:master.slave各一个节点的情况下,不管是master还是slave down掉一个,“完整的” ...