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;
}

[SPOJ375]Qtree的更多相关文章

  1. Cogs 1672. [SPOJ375 QTREE]难存的情缘 LCT,树链剖分,填坑计划

    题目:http://cojs.tk/cogs/problem/problem.php?pid=1672 1672. [SPOJ375 QTREE]难存的情缘 ★★★☆   输入文件:qtree.in  ...

  2. 树链剖分-SPOJ375(QTREE)

    QTREE - Query on a tree You are given a tree (an acyclic undirected connected graph) with N nodes, a ...

  3. SPOJ375.QTREE树链剖分

    题意:一个树,a b c 代表a--b边的权值为c.CHANGE x y  把输入的第x条边的权值改为y,QUERY x y 查询x--y路径上边的权值的最大值. 第一次写树链剖分,其实树链剖分只能说 ...

  4. [SPOJ375]QTREE - Query on a tree【树链剖分】

    题目描述 给你一棵树,两种操作. 修改边权,查找边权的最大值. 分析 我们都知道,树链剖分能够维护点权. 而且每一条边只有一个,且唯一对应一个儿子节点,那么就把信息放到这个儿子节点上. 注意,lca的 ...

  5. SPOJ375 QTREE - Query on a tree

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  6. Spoj375 Qtree--树链剖分

    Spoj375 Qtree给一棵共有 n(n · 10000) 个结点的树, 每条边都有一个权值, 要求维护一个数据结构, 支持如下操作: 1. 修改某条边的权值; 2. 询问某两个结点之间的唯一通路 ...

  7. HDU4010 Query on The Trees(LCT)

    人生的第一道动态树,为了弄懂它的大致原理,需要具备一些前置技能,如Splay树,树链剖分的一些概念.在这里写下一些看各种论文时候的心得,下面的代码是拷贝的CLJ的模板,别人写的模板比较可靠也方便自己学 ...

  8. 【COGS1672】【SPOJ375】QTREE

    这是我的第一个边权链剖 COGS上和SPOJ有点不一样就是没有多组数据了本质还是一样的 我写的是COGS那个事实上改一改就能够去SPOJ AC了= -= (但是我如今上不去SPOJ卧槽(╯‵□′)╯︵ ...

  9. QTREE系列题解

    打了快一星期的qtree终于打完了- - (其实还有两题改不出来弃疗了QAQ) orz神AK一星期前就虐完QTREE 避免忘记还是简单写下题解吧0 0 QTREE1 题意: 给出一颗带边权树 一个操作 ...

随机推荐

  1. 使用mysql-connector-java.jar连接MySql时出现:Error while retrieving metadata for procedure columns: java.sql.SQLException: Parameter/Column name pattern can not be NULL or empty.

    错误如下: 程序实现的功能是调用一个存储过程,但是不认这个存储过程的参数. 原因是版本太高了,由于使用的是6.0.6版本的,改成5.1.38即可. POM配置如下: <!-- mysql-con ...

  2. openstack DVR的AIO 问题

    问题描述 : 创建public 网络,创建路由器,并且把路由器的gateway 设置指向网络后有下面几种错误 路由器对应的linux network namespace 建立起来了,但是里面并没有对应 ...

  3. [转] SQL SERVER 2008 R2 安装中的账户设置问题

    故纸堆 原文:SQL SERVER 2008安装中设置账户的问题,2013-7 在安装SQL Server 2008数据库服务器的时候,服务器有可能处于以下几种环境中: ①工作组环境下的服务器 (Wo ...

  4. sql 语句哪里添加单引号问题

    1.sql 语句哪里添加单引号问题,哪些地方必须加双引号,否则sql语句会报错? :涉及varchar的值的时候,必须有单引号包括varchar值.int等其他字段类型,则不需要加单引号包括. 如: ...

  5. CEF3研究(四)之javascript集成

    一.介绍 谷歌浏览器和CEF使用V8JavaScript Engine作为内容的JavaScript实现.在浏览器中的每个窗口都有它自己在的JS上下文提供作用域和在窗口中安全的执行JS代码.CEF暴露 ...

  6. DNS的工作原理及解析

    DNS协议是互联网核心协议之一.不管是上网浏览,还是编程开发,都需要了解一点它的知识. 一.什么是DNS? DNS( Domain Name System)是“域名系统”的英文缩写,是一种组织成域层次 ...

  7. linux 下的特殊文件 /dev/null 和 /de/zero

    生成一个100Mb的文件 : time dd of=2Gb.file if=/dev/zero  bs=1024 count=100000 ubuntu 下测试磁盘的读写性能: 测试写: time d ...

  8. 使用WIN32汇编语言实现一个基本windows窗体的过程分析

    一个常规的windows窗体一般都是一些一样的构造.你假设想要更改一些个性化的设置,你能够在这个一般的模板伤添砖加瓦.构造自己比較喜欢的类型.下边就分析一下一般的windows窗体的一般模板. 一. ...

  9. Vs2013在Linux开发中的应用(19): 启动gdb

    快乐虾 http://blog.csdn.net/lights_joy/ 欢迎转载,但请保留作者信息 1.1    载入调试引擎 因为我们无法干预VC的调试引擎载入.但能够侦听VC的调试引擎载入事件, ...

  10. The data property "dialogVisble" is already declared as a prop. Use prop default value instead报错原因

    vue中使用props传递数据就不能在子组件的data中用同样的名字(比如dialogVisble)了,否则会报错.解决方法直接去掉data中的相同名字改为其他的.