SPOJ 375. Query on a tree (动态树)
375. Query on a treeProblem code: QTREE |
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
/* ***********************************************
Author :kuangbin
Created Time :2013-9-3 21:06:05
File Name :F:\2013ACM练习\专题学习\动态树-LCT\SPOJQTREE.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std; //对一颗树,进行两个操作:
//1.修改边权
//2.查询u->v路径上边权的最大值
const int MAXN = ;
int ch[MAXN][],pre[MAXN];
int Max[MAXN],key[MAXN];
bool rt[MAXN];
void push_down(int r)
{ }
void push_up(int r)
{
Max[r] = max(max(Max[ch[r][]],Max[ch[r][]]),key[r]);
}
void Rotate(int x)
{
int y = pre[x], kind = ch[y][]==x;
ch[y][kind] = ch[x][!kind];
pre[ch[y][kind]] = y;
pre[x] = pre[y];
pre[y] = x;
ch[x][!kind] = y;
if(rt[y])
rt[y] = false, rt[x] = true;
else
ch[pre[x]][ch[pre[x]][]==y] = x;
push_up(y);
}
void P(int r)
{
if(!rt[r])P(pre[r]);
push_down(r);
}
void Splay(int r)
{
//P(r);
while( !rt[r] )
{
int f = pre[r], ff = pre[f];
if(rt[f])
Rotate(r);
else if( (ch[ff][]==f)==(ch[f][]==r) )
Rotate(f), Rotate(r);
else
Rotate(r), Rotate(r);
}
push_up(r);
}
int Access(int x)
{
int y = ;
do
{
Splay(x);
rt[ch[x][]] = true, rt[ch[x][]=y] = false;
push_up(x);
x = pre[y=x];
}
while(x);
return y;
}
//调用后u是原来u和v的lca,v和ch[u][1]分别存着lca的2个儿子
//(原来u和v所在的2颗子树)
void lca(int &u,int &v)
{
Access(v), v = ;
while(u)
{
Splay(u);
if(!pre[u])return;
rt[ch[u][]] = true;
rt[ch[u][]=v] = false;
push_up(u);
u = pre[v = u];
}
} void change(int u,int k)
{
Access(u);
key[u] = k;
push_up(u);
}
void query(int u,int v)
{
lca(u,v);
printf("%d\n",max(Max[v],Max[ch[u][]]));
} struct Edge
{
int to,next;
int val;
int index;
}edge[MAXN*];
int head[MAXN],tot;
int id[MAXN]; void addedge(int u,int v,int val,int index)
{
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].val = val;
edge[tot].index = index;
head[u] = tot++;
}
void dfs(int u)
{
for(int i = head[u];i != -;i = edge[i].next)
{
int v = edge[i].to;
if(pre[v] != )continue;
pre[v] = u;
id[edge[i].index] = v;
key[v] = edge[i].val;
dfs(v);
}
}
void init()
{
tot = ;
memset(head,-,sizeof(head));
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int n;
int u,v,w;
char op[];
scanf("%d",&T);
while(T--)
{
init();
scanf("%d",&n);
for(int i = ;i <= n;i++)
{
pre[i] = ;
ch[i][] = ch[i][] = ;
rt[i] = true;
}
Max[] = -;
for(int i = ;i < n;i++)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w,i);
addedge(v,u,w,i);
}
pre[] = -;
dfs();
pre[] = ;
while(scanf("%s",&op) == )
{
if(op[] == 'D')break;
scanf("%d%d",&u,&v);
if(op[] == 'C')
change(id[u],v);
else query(u,v);
}
}
return ;
}
SPOJ 375. Query on a tree (动态树)的更多相关文章
- SPOJ 375. Query on a tree (树链剖分)
Query on a tree Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Ori ...
- spoj 375 Query on a tree(树链剖分,线段树)
Query on a tree Time Limit: 851MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Sub ...
- SPOJ 375 Query on a tree(树链剖分)(QTREE)
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
- SPOJ 375 Query on a tree【树链剖分】
题目大意:给你一棵树,有两个操作1.修改一条边的值,2.询问从x到y路径上边的最大值 思路:如果树退化成一条链的话线段树就很明显了,然后这题就是套了个树连剖分,调了很久终于调出来第一个模板了 #inc ...
- SPOJ 375 Query on a tree(树链剖分)
https://vjudge.net/problem/SPOJ-QTREE 题意: 给出一棵树,树上的每一条边都有权值,现在有查询和更改操作,如果是查询,则要输出u和v之间的最大权值. 思路: 树链剖 ...
- 动态树(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) ...
- spoj 375 Query on a tree (树链剖分)
Query on a tree You are given a tree (an acyclic undirected connected graph) with N nodes, and edges ...
- QTREE3 spoj 2798. Query on a tree again! 树链剖分+线段树
Query on a tree again! 给出一棵树,树节点的颜色初始时为白色,有两种操作: 0.把节点x的颜色置反(黑变白,白变黑). 1.询问节点1到节点x的路径上第一个黑色节点的编号. 分析 ...
- 【SPOJ】375. Query on a tree(树链剖分)
http://www.spoj.com/problems/QTREE/ 这是按边分类的. 调试调到吐,对拍都查不出来,后来改了下造数据的,拍出来了.囧啊啊啊啊啊啊 时间都花在调试上了,打hld只用了半 ...
随机推荐
- 利用vw+rem实现移动web适配布局
基本概念 1.单位 Px(CSS pixels) 像素 (px) 是一种绝对单位(absolute units), 因为无论其他相关的设置怎么变化,像素指定的值是不会变化的 其实是相对于某个设备而言的 ...
- 小程序开发总结一:mpvue框架及与小程序原生的混搭开发
mpvue-native:小程序原生和mpvue代码共存 问题描述 mpvue和wepy等框架是在小程序出来一段时间之后才开始有的,所以会出现的问题有:需要兼容已有的老项目,有些场景对小程序的兼容要求 ...
- java基础81 jsp的内置对象(网页知识)
1.什么是内置对象? 在jsp开发中,会频繁使用到一些对象,如:HttpSession,ServletContext,HttpServletRequest. 如果每次使用这些对象时,都要去创 ...
- Jackson jsonToBean 忽略 Bean中不存在属性的匹配。
1. jsonToBean时,json中有的字段,bean中没有 无法匹配时,忽略此字段,不抛出异常(默认是抛出异常的) 方案一(局部):在class上加 @JsonIgnoreProperties( ...
- Merkle Tree(默克尔树)算法解析
Merkle Tree概念 Merkle Tree,通常也被称作Hash Tree,顾名思义,就是存储hash值的一棵树.Merkle树的叶子是数据块(例如,文件或者文件的集合)的hash值.非叶节点 ...
- OBJECT_ID()的使用方法
数据库中每个对像都有一个唯一的ID值,用Object_name(id)可以根据ID值得到对像的名称,object_id(name)可以根据对像名称得到对象的ID object_id()只能返回用户创建 ...
- centos7 配置ftp访问
vsftp安装 创建用户 并限定用户在自己的目录 1.查看是否已经安装了vsftpd vsftpd -version 2.安装vsftpd(CentOS7) yum install -y vsftpd ...
- 【LOJ】 #2033. 「SDOI2016」生成魔咒
题解 就是字符集较大需要离散化和建边表的后缀自动机水题 每次会加入i个新的串,其中重复的就是i的父亲节点所在节点的长度,减掉即可 代码 #include <iostream> #inclu ...
- Tuning Optimization
2017-02-22 在经过Trace的分析并重新优化之后 CPU: Intel(R) Xeon(R) CPU E5-2630 v2 @ 2.6GHz 24core 如上图,CPU使用率平均低于60% ...
- 文件哈希审计工具md5deep/hashdeep
文件哈希审计工具md5deep/hashdeep 在数据取证中,通常需要验证文件的哈希值,以判断文件是否已知好文件,或者文件是否被修改过.Kali Linux提供专用工具hashdeep.该工具的 ...