动态树(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卡过的). ...
随机推荐
- codevs 1170 双栈排序
/* 好题啊 好题啊 而然还是看了一眼题解啊 有那么一点思路 但是离写出代码还很远 考虑必须分开放倒两个栈里的情况 即存在i<j<k 有 a[k]<a[i]<a[j] 这里RM ...
- 基于slf4j的log4j实战
参考文档如下: http://blog.csdn.net/anialy/article/details/8529188 slf4j是接口,基于门面模式,可以实现log4j和logback 参考文档如下 ...
- JAva Collections类方法详解
http://blog.csdn.net/lskyne/article/details/8961014 Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素 ...
- Creating a web application.
About creating web GIS applications As you learn and use ArcGIS for Server, you'll probably reach th ...
- PHP上传原理及应用
概要 1.FORM表现enctype属性 2.$_FILES系统函数 3.move_uploaded_file函数 4.is_uploaded_file函数 1.FORM标签的enctype属性 只有 ...
- oracle 数据库关闭的的几种方式总结
shutdown的几种方式,shutdown abort的一些弊端有哪些 1.shutdown normal 正常方式关闭数据库. 2.shutdown immediate ...
- Xcode添加静态库以及编译选项配置常见问题
一,Xcode编译出现Link错误,出现"duplicate symbols for architecture i386 clang"提示.问题:链接时,项目有重名文件.解决:根据 ...
- SGU 135.Drawing Lines
水题,不说了. #include <iostream> using namespace std; int f[70000]={1}; int n; int main(){ cin>& ...
- mongo db安装和php,python插件安装
安装mongodb 1.下载,解压mongodb(下载解压目录为/opt) 在/opt目录下执行命令 wget fastdl.mongodb.org/linux/mongodb-linux-x86_6 ...
- Entity Framework 级联删除
为一对主从表增加级联删除功能 protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.E ...