SPOJ375 Query on a tree
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 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
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的更多相关文章
- SPOJ375 Query on a tree(树链剖分)
传送门 题意 给出一棵树,每条边都有权值,有两种操作: 把第p条边的权值改为x 询问x,y路径上的权值最大的边 code #include<cstdio> #include<algo ...
- SPOJ375 Query on a tree(LCT边权)
之前做了两道点权的LCT,这次做一下边权的LCT.上网找了一下资料,发现对于边权的LCT有这么两种处理方法,一种是每条边建一个点,于是边权就转成点权了.另外一种则是每个边权对应到点权上,也就是每个点对 ...
- SPOJ375 Query on a tree 【倍增,在线】
题目链接[http://www.spoj.com/problems/QTREE/] 题意:给出一个包含N(N<=10000)节点的无根树,有多次询问,询问的方式有两种1.DIST a b 求a ...
- Query on a tree——树链剖分整理
树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...
- SPOJ 375. Query on a tree (树链剖分)
Query on a tree Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Ori ...
- QTREE3 spoj 2798. Query on a tree again! 树链剖分+线段树
Query on a tree again! 给出一棵树,树节点的颜色初始时为白色,有两种操作: 0.把节点x的颜色置反(黑变白,白变黑). 1.询问节点1到节点x的路径上第一个黑色节点的编号. 分析 ...
- spoj 375 Query on a tree(树链剖分,线段树)
Query on a tree Time Limit: 851MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Sub ...
- 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 ...
- 动态树(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) ...
随机推荐
- IE下默认TD colspan rowspan值为1
IE下默认TD colspan rowspan值为1,即使这个TD没有合并没有rowspan,colspan属性,其值都为1,chrome下正常. 判断是否rowspan colspan为TD.get ...
- Alpha版本项目展示要求
Alpha版本展示的时间暂定为11月17日课上.如有变动,另行通知. 在Alpha阶段项目评审会上, 每个团队有12分钟展示时间,10分钟问答和机动时间,我们的展示也不需要PPT,大家把要展现的东西写 ...
- DB2和Oracle区别
转 http://blog.chinaunix.net/uid-7374279-id-2057574.html 写在前面:今天客户来访(日本人),问我DB2和Oracle区别.因为不是DBA(勉强的理 ...
- centos安装php扩展
我自己的方法 先卸载php 重新安装 再安装扩展 1 rpm -qa|grep php 查看php相关 2 rpm-e 名称 一个个删除,个别需要先删除其他组件才能删除, 3 再用 rpm -qa|g ...
- java中的枚举类型
枚举类型是那些字段由一组固定常量组成的类型.常见的例子有:东南西北四个方向,星期几等. 所有枚举类型都隐式继承java.lang.Enum类型,因为java不支持多重继承,所以枚举不能继承其他任何类. ...
- 《Spring 3.0就这么简单》 读书笔记
第一章:快速入门 开发流程: 1.创建库表 依赖jar包配置 2.事务:事务是恢复和并发控制的基本单位. 原子性(Atomicity) 一致性(Consistency) 隔离性(Isolatio ...
- extJs学习基础4 Ext.each的用法
Ext.onReady(function(){ //案例一 /* var countries = ['Vietnam', 'Singapore', 'United States', 'Russia'] ...
- android 点亮屏幕与锁定屏幕
PowerManager pm=(PowerManager) getSystemService(Context.POWER_SERVICE); //获取电源管理器对象 PowerManager.Wak ...
- Ubuntu14.04下jdk的安装
1.下载JDK目前最新的JDK版本是:Java SE Development Kit 8u52.解压安装我们把JDK安装到这个路径:/usr/lib/jvm如果没有这个目录(第一次当然没有),我们就新 ...
- [转]session 持久化问题(重启服务器session 仍然存在)
转:http://xiaolongfeixiang.iteye.com/blog/560800 关于在线人数统计,大都使用SessionListener监听器实现. SessionListener 触 ...