375. Query on a tree

Problem 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 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
 /* ***********************************************
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 (动态树)的更多相关文章

  1. SPOJ 375. Query on a tree (树链剖分)

    Query on a tree Time Limit: 5000ms Memory Limit: 262144KB   This problem will be judged on SPOJ. Ori ...

  2. spoj 375 Query on a tree(树链剖分,线段树)

      Query on a tree Time Limit: 851MS   Memory Limit: 1572864KB   64bit IO Format: %lld & %llu Sub ...

  3. 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, ...

  4. SPOJ 375 Query on a tree【树链剖分】

    题目大意:给你一棵树,有两个操作1.修改一条边的值,2.询问从x到y路径上边的最大值 思路:如果树退化成一条链的话线段树就很明显了,然后这题就是套了个树连剖分,调了很久终于调出来第一个模板了 #inc ...

  5. SPOJ 375 Query on a tree(树链剖分)

    https://vjudge.net/problem/SPOJ-QTREE 题意: 给出一棵树,树上的每一条边都有权值,现在有查询和更改操作,如果是查询,则要输出u和v之间的最大权值. 思路: 树链剖 ...

  6. 动态树(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) ...

  7. 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 ...

  8. QTREE3 spoj 2798. Query on a tree again! 树链剖分+线段树

    Query on a tree again! 给出一棵树,树节点的颜色初始时为白色,有两种操作: 0.把节点x的颜色置反(黑变白,白变黑). 1.询问节点1到节点x的路径上第一个黑色节点的编号. 分析 ...

  9. 【SPOJ】375. Query on a tree(树链剖分)

    http://www.spoj.com/problems/QTREE/ 这是按边分类的. 调试调到吐,对拍都查不出来,后来改了下造数据的,拍出来了.囧啊啊啊啊啊啊 时间都花在调试上了,打hld只用了半 ...

随机推荐

  1. sql newid()随机函数

    从A表随机取2条记录,用SELECT TOP 10 * FROM ywle order by newid()order by 一般是根据某一字段排序,newid()的返回值 是uniqueidenti ...

  2. OutLook中添加Exchange失败问题

    问题: 在邮件中添加账户后,打开outlook时报出错误:无法启动 Microsoft Outlook. 无法打开 Outlook 窗口. 无法打开此文件夹集合. 必须先使用当前的配置文件连接到 Mi ...

  3. java小爬虫

    爬取煎蛋网 1.找出页面网址的规律 2.设计页面图片网址的正则 代码: import java.io.BufferedInputStream; import java.io.BufferedOutpu ...

  4. Struts 2 Overview

    Struts2 is popular and mature web application framework based on the MVC design pattern. Struts2 is ...

  5. 易普优APS与国外知名高级计划排程系统对比

    众所周知软件执行效率受制于硬件性能,市面上的APS产品多为单机版本,企业要应用好APS,保证紧急插单.计划下发全程无忧,用户电脑硬件性能是不容忽视的一大瓶颈.APS的直接用户是车间管理人员.计划员,而 ...

  6. 003 JTA的使用与理解

    一:认识JTA 1.介绍 事物的ACID. 事务是计算机应用中不可或缺的组件模型,它保证了用户操作的原子性 ( Atomicity ).一致性 ( Consistency ).隔离性 ( Isolat ...

  7. Ubuntu16.04下HBase的安装与配置

    一.环境 os : Ubuntu 16.04 LTS 64bit jdk : 1.8.0_161 hadoop : mysql : hive : hbase: -hadoop2 安装HBase前,系统 ...

  8. (转)最短路算法--Dijkstra算法

    转自:http://blog.51cto.com/ahalei/1387799         上周我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最短 ...

  9. thinkphp3.2自动生成模块BIND_MODULE

    thinphp3.2中提供了自定义生成模块与控制器的常量,分别是BIND_MODULE,BUILD_CONTROLLER_LIST 在 index.php 文件中定义 BIND_MODULE,BUIL ...

  10. JSP九大内置对象简介

    1.out对象,是JspWriter类的实例,是向客户端输出内容常用的对象 方法: void println() 向客户端打印字符串 void clear() 清除缓冲区的内容,如果在flush之后调 ...