QTREE - Query on a tree

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   这题可以用树链剖分做,我这里用LCT做的,代码量更少。
 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std;
const int maxn=;
int Max[maxn],fa[maxn],ch[maxn][],key[maxn];
bool rt[maxn]; void Push_up(int p)
{
Max[p]=max(key[p],max(Max[ch[p][]],Max[ch[p][]]));
} void Rotate(int x)
{
int y=fa[x],g=fa[y],c=ch[y][]==x;
ch[y][c]=ch[x][c^];ch[x][c^]=y;
fa[ch[y][c]]=y;fa[y]=x;fa[x]=g;
if(rt[y])
rt[y]=false,rt[x]=true;
else
ch[g][ch[g][]==y]=x;
Push_up(y);
} void Splay(int x)
{
for(int y=fa[x];!rt[x];Rotate(x),y=fa[x])
if(!rt[y])
Rotate((ch[fa[y]][]==y)==(ch[y][]==x)?y:x);
Push_up(x);
} void Access(int x)
{
int y=;
while(x){
Splay(x);
rt[ch[x][]]=true;
rt[ch[x][]=y]=false;
Push_up(x);
x=fa[y=x];
}
} void Query(int x,int y)
{
Access(y),y=;
while(true)
{
Splay(x);
if(!fa[x]){
printf("%d\n",max(Max[y],Max[ch[x][]]));
return;
}
rt[ch[x][]]=true;
rt[ch[x][]=y]=false;
Push_up(x);
x=fa[y=x];
}
} void Change(int x,int d)
{
Access(x);
Splay(x);
key[x]=d;
Push_up(x);
} int fir[maxn],nxt[maxn<<],to[maxn<<],cnt;
int e[maxn][]; void addedge(int a,int b)
{
nxt[++cnt]=fir[a];to[cnt]=b;fir[a]=cnt;
} void DFS(int node)
{
for(int i=fir[node];i;i=nxt[i])
{
if(fa[to[i]])continue;
fa[to[i]]=node;
DFS(to[i]);
}
} void Init()
{
cnt=;Max[]=-;
for(int i=;i<=;i++){
Max[i]=fir[i]=fa[i]=;
rt[i]=;
}
return;
}
int main()
{
int T,n,a,b;
scanf("%d",&T);
while(T--)
{
Init();
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d%d%d",&e[i][],&e[i][],&e[i][]);
for(int i=;i<n;i++)
addedge(e[i][],e[i][]),addedge(e[i][],e[i][]);
fa[]=-;
DFS();
fa[]=;
for(int i=;i<n;i++)
{
if(fa[e[i][]]==e[i][])
swap(e[i][],e[i][]);
Change(e[i][],e[i][]);
}
char op[];
while(true)
{
scanf("%s",op);
if(!strcmp(op,"DONE"))break;
else if(!strcmp(op,"QUERY")){
scanf("%d%d",&a,&b);
Query(a,b);
} else {
scanf("%d%d",&a,&b);
Change(e[a][],b);
}
}
} return ;
}

动态树(Link Cut Tree) :SPOJ 375 Query on a tree的更多相关文章

  1. SPOJ 375. Query on a tree (动态树)

    375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph ...

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

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

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

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

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

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

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

  6. SPOJ 375 Query on a tree 树链剖分模板

    第一次写树剖~ #include<iostream> #include<cstring> #include<cstdio> #define L(u) u<&l ...

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

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

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

  9. spoj 375 query on a tree LCT

    这道题是树链剖分的裸题,正在学LCT,用LCT写了,发现LCT代码比树链剖分还短点(但我的LCT跑极限数据用的时间大概是kuangbin大神的树链剖分的1.6倍,所以在spoj上是850ms卡过的). ...

随机推荐

  1. java开发规范总结_命名规范

    规范需要平时编码过程中注意,是一个慢慢养成的好习惯 1.文件 1.属性文件后缀为properties,并且符合java中i18n的规范:   2.对于各产品模块自己的配置文件必须放置在自己模块的con ...

  2. oracle 10g 恢复dmp文件。

    1. 在winxp下,安装10g,默认选择,一路ok.(安装前自检出现dhcp警告,可直接忽略) 2.命令行,在xp下,输入sqlplus,即可启动,登陆用 sqlplus / as sysdba 用 ...

  3. linux 脚本编写基础(一)

    1. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在 ...

  4. css布局小技巧 2016.03.06

    偶遇一个可爱的css布局学习网站,立刻学起来哟- max-width: 当页面左右宽度缩小时,为了避免出现左右滚动条的糟糕体验,就可以用到max-width啦!页面比宽度小时,会自动缩小哦- max- ...

  5. jQuery慢慢啃筛选(四)

    1.eq(index|-index) 获取第N个元素 其中负数:一个整数,指示元素的位置,从集合中的最后一个元素开始倒数.(1算起) $("p").eq(1)//获取匹配的第二个元 ...

  6. 用jq 做了一个排序

    <ul id="cont"> <li data="5">5</li> <li data="1"&g ...

  7. request.getSession()

    request.getSession(); 与request.getSession(false);区别 服务器把session信息发送给浏览器   浏览器会将session信息存入本地cookie中 ...

  8. shell之sort

    转http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html) sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分钟 ...

  9. CentOS 忘记root密码,解决方法

    1.开机后,在倒数5秒结束前,按下任意键 2.在显示centos...的那个界面下,按e键(edit) 3.会出现三行的界面,选择中间 kernel...那行,然后按e键 4.在接着出现的那个界面最后 ...

  10. js浏览器键盘事件控制(转自新浪微博)

    js键盘事件全面控制 主要分四个部分第一部分:浏览器的按键事件第二部分:兼容浏览器第三部分:代码实现和优化第四部分:总结 第一部分:浏览器的按键事件 用js实现键盘记录,要关注浏览器的三种按键事件类型 ...