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) ...
随机推荐
- 返回标量CLR自定义函数
昨天有学习了返回表自定义函数<CLR Table-Valued函数>http://www.cnblogs.com/insus/p/4378354.html.今天学习另一个,实现返回标量(S ...
- mac/linux中vim永久显示行号、开启语法高亮
步骤1: cp /usr/share/vim/vimrc ~/.vimrc 先复制一份vim配置模板到个人目录下 注:redhat 改成 cp /etc/vimrc ~/.vimrc 步骤2: vi ...
- WebPack系列:Webpack编译的代码如何在tomcat中使用时静态资源路径不对的问题如何解决
问题: 使用webpack+vue做前端,使用tomcat提供api,然后npm run build之后需要将编译,生成如下文件: | index.html \---appserver ...
- 软件工程(DBSD2016) Git Review
说明:任何问题请在评论区说明,会集中更新回复. 连连看组 源码: git clone https://git.coding.net/jx8zjs/llk.git 提交日志 一共有20次commit日志 ...
- HDU2444-The Accomodation of Students-判断是否为二分图+ISAP
要先判断是不是二分图.用黑白染色法. 遇到已经染过的跟当前的颜色相同时就说明不是二分图,也即出现了奇环 /*---------------------------------------------- ...
- Matlab中函数定义方法
Matlab自定义函数的六种方法 n1.函数文件+调用函数(命令)文件:需单独定义一个自定义函数的M文件: n2.函数文件+子函数:定义一个具有多个自定义函数的M文件: n3.Inline:无需M文件 ...
- HTML问题集锦
[1]HTML5怎么设置滚动字幕 <marquee direction=up behavior=scroll loop=3 scrollamount=1 scrolldelay=10 align ...
- 曼慧尼特u检验(两个样本数据间有无差异)
曼-惠特尼U检验(Mann-Whitney检验) How the Mann-Whitney test works Mann-Whitney检验又叫做秩和检验,是比较没有配对的两个独立样本的非参数检验. ...
- 腾讯云CentOS 安装MediaWiki
参考 : https://www.digitalocean.com/community/tutorials/how-to-install-mediawiki-on-centos-7 //安装好很多次终 ...
- json写入new_hello文件
#写入new文件 import json dic = {'name':'alex'} i = 8 s = 'hello' l = [11,22] f = open("new_hello&qu ...