动态树(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卡过的). ...
随机推荐
- html获取gps坐标
<script> function getLocation(){ var options={ enableHighAccuracy:true, maximumAge:1000 } if(n ...
- (tomcat访问不了的两种解决方法)Bad Request(Invalid Hostname)
显示这个页面的时候一般有几中解决方法: 第一种就是如下图所示的方法: 具体步骤是: 1.也就是左下角win的“运行”中输入cmd进入doc窗口中 2.输入代码:netstat -ano 3.找到占用8 ...
- 前端过滤XSS攻击
日常开发过程中,对于存在用户交互的一些门户网站等,过滤xss攻击是必不可少的. 此处主要记录下我在工作过程中的简单处理方法. 前端过滤XSS攻击, 我这里用的是开源工程 js-xss,官网地址:htt ...
- AndroidManifest.xml--android系统权限定义
1. 系统编译结束自动生成的java类,描述系统所有定义的权限 out/target/common/R/android/Manifest.java 2. 权限检查方法 frameworks/base/ ...
- Use StringBuilder instead of String as possible as you can.
If you are care a littile about the time your algorithm cost,you should notice that,you my use Strin ...
- SQL SERVER 中PatIndex的用法个人理解
一般用法:PatIndex('%AAA%',‘BBBBBBBB’) 上句的意思是查找AAA在BBBBBBBB中的位置,从1开始计算,如果没有的话则返回0 其中%AAA%的用法和 SQL语句中like的 ...
- oracle中所有关于时间日期的问题总结
select current_date as 当前会话时间,sysdate as 系统时间, systimestamp as 系统详细时间 from dual;
- c读写文件相关
1.打开文件: 函数原型: FILE * fopen(const char * path,const char * mode); 返回值: 文件顺利打开后,指向该流的文件指针就会被返回.如果文件打开失 ...
- linux防火墙解封某端口
首先,使用netstat –tunlp查看是否23端口被防火墙封掉了: 再使用iptables修改设置, # iptables -I INPUT -p tcp --dport 23 –jACCEPT ...
- null和undefined的区别
不同之处: null是js语言的关键字,它表示一个特殊值,常用来描述“空值”.对null执行typeof运算,结果返回字符串“object”,也就是说,可以将null认为是一个特殊的对象值,含义是“非 ...