动态树(Link Cut Tree) :SPOJ 375 Query on a tree
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的更多相关文章
- SPOJ 375. Query on a tree (动态树)
375. Query on a tree Problem code: QTREE You are given a tree (an acyclic undirected connected graph ...
- 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 (树链剖分)
Query on a tree You are given a tree (an acyclic undirected connected graph) with N nodes, and edges ...
- SPOJ 375 Query on a tree【树链剖分】
题目大意:给你一棵树,有两个操作1.修改一条边的值,2.询问从x到y路径上边的最大值 思路:如果树退化成一条链的话线段树就很明显了,然后这题就是套了个树连剖分,调了很久终于调出来第一个模板了 #inc ...
- SPOJ 375 Query on a tree 树链剖分模板
第一次写树剖~ #include<iostream> #include<cstring> #include<cstdio> #define L(u) u<&l ...
- 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(树链剖分)
https://vjudge.net/problem/SPOJ-QTREE 题意: 给出一棵树,树上的每一条边都有权值,现在有查询和更改操作,如果是查询,则要输出u和v之间的最大权值. 思路: 树链剖 ...
- spoj 375 query on a tree LCT
这道题是树链剖分的裸题,正在学LCT,用LCT写了,发现LCT代码比树链剖分还短点(但我的LCT跑极限数据用的时间大概是kuangbin大神的树链剖分的1.6倍,所以在spoj上是850ms卡过的). ...
随机推荐
- 关于在MDK4.5以上版本不能使用JLINK V8的解决办法
如果安装MDK4.50版本以上不能使用jlink8的话,请安装jlink 4.36k版本(或以下)驱动,安装完成后,把\SEGGER\JLinkARM_V436k目录下的JLinkARM.dll拷贝到 ...
- PL/SQL 记录集合IS TABLE OF的使用
在PL/SQL代码块中使用select into 赋值的话,有可能返回的是一个结果集.此时,如果使用基本类型或自定义的记录类型,将会报错. 因此,需要定义一个变量,是某种类型的集合.下面以一个基于表的 ...
- ecshop了解01
ecshop 是一个基于b2c的开源商城系统,从现在起来我也来学习一下,它是基于面向对象的,当然里面也有类. ecshop 的目录介绍 上面简单介绍一个ecshop的几个主要的文件,上面已经截图给大家 ...
- C语言中,如何通过socket得到对端IP地址
struct sockaddr_in clientaddr1; memset(&clientaddr1, 0x00, sizeof(clientaddr1)); socklen_t nl=si ...
- DATEDIFF interval=ms的用法
datediff(ms,@CurrDateTime,@Date)>0 当上面的日期超过24天,用上面的sql会有问题 要修改成如下: (CONVERT(VARCHAR,@CurrDateTime ...
- socket.io 实例
//引用 var io = require('socket.io')(server); //server io.on('connection', function(socket) { // ...
- 内容替换Filter
有时候需要对网站进行控制,防止输出非法内容或者敏感信息.这时我们可以使用filter来进行内容替换,其工作原理为,在Servlet将内容输出到response时,response将内容缓存起来,在Fi ...
- LA 6476 Outpost Navigation (DFS+剪枝)
题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...
- ECSHOP错误Redefining already defined constructor for class如何解决
本地PHP环境PHP5.4,安装ecshop2.7.3后,很多地方会报如下的错 Redefining already defined constructor for class XXX 使用和类名相同 ...
- 1s延时程序
#include <reg52.h>sbit P1_0 = P1^0;void Delay(); // 下面引用时一定要和这里的大小写一致否则会有警告或错误 void Main(){whi ...