HDU 4010 Query on The Trees(动态树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4010
题意:一棵树,四种操作:
(1)若x和y不在一棵树上,将x和y连边;
(2)若x和y在一棵树上,将x变成树根,将y从x树上分离;
(3)若x和y在一棵树上,将x到y路径上的所有值增加det;
(4)若x和y在一棵树上,输出x到y路径上的最大值。
思路:1操作用link维护,2操作用cut,34操作先split(x,y),然后对y做tag,并且记录路径的max值。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
int tot,go[],next[],first[];
int ch[][],rev[],tag[],q[];
int st[],mx[],fa[],v[];
bool pd(int x){
return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;
}
void insert(int x,int y){
tot++;go[tot]=y;next[tot]=first[x];first[x]=tot;
}
void addedge(int x,int y){
insert(x,y);insert(y,x);
}
void pushdown(int x){
int l=ch[x][],r=ch[x][];
if (rev[x]){
rev[x]^=;rev[l]^=;rev[r]^=;
std::swap(ch[x][],ch[x][]);
}
if (tag[x]){
if (l) tag[l]+=tag[x],v[l]+=tag[x],mx[l]+=tag[x];
if (r) tag[r]+=tag[x],mx[r]+=tag[x],v[r]+=tag[x];
tag[x]=;
}
}
void updata(int x){
int l=ch[x][],r=ch[x][];
mx[x]=std::max(v[x],std::max(mx[l],mx[r]));
}
void rotate(int x){
int y=fa[x],z=fa[y],l,r;
if (ch[y][]==x) l=;else l=;r=l^;
if (!pd(y)){
if (ch[z][]==y) ch[z][]=x;else ch[z][]=x;
}
fa[x]=z;fa[y]=x;fa[ch[x][r]]=y;
ch[y][l]=ch[x][r];ch[x][r]=y;
updata(y);updata(x);
}
void splay(int x){
int top=;st[++top]=x;
for (int i=x;!pd(i);i=fa[i])
st[++top]=fa[i];
for (int i=top;i;i--)
pushdown(st[i]);
while (!pd(x)){
int y=fa[x],z=fa[y];
if (!pd(y)){
if (ch[y][]==x^ch[z][]==y) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x){
for (int t=;x;t=x,x=fa[x]){
splay(x);
ch[x][]=t;
updata(x);
}
}
void makeroot(int x){
access(x);splay(x);rev[x]^=;
}
void cut(int x,int y){
makeroot(x);access(y);splay(y);ch[y][]=fa[ch[y][]]=;updata(y);
}
void link(int x,int y){
makeroot(x);
fa[x]=y;
}
int find(int x){
access(x);splay(x);
while (ch[x][]) x=ch[x][];
return x;
}
void add(int x,int y,int val){
makeroot(x);access(y);splay(y);
mx[y]+=val;v[y]+=val;tag[y]+=val;
}
int main(){
int n,x,y,m,opt,w;
while (scanf("%d",&n)!=EOF){
mx[]=-;
for (int i=;i<=n;i++)
mx[i]=tag[i]=rev[i]=v[i]=ch[i][]=ch[i][]=fa[i]=;
memset(first,,sizeof first);tot=;
for (int i=;i<n;i++){
scanf("%d%d",&x,&y);
addedge(x,y);
}
for (int i=;i<=n;i++){
scanf("%d",&v[i]);
mx[i]=v[i];
}
int top=;
q[top]=;
for (int k=;k<=top;k++){
for (int i=first[q[k]];i;i=next[i]){
int pur=go[i];
if (pur==fa[q[k]]) continue;
fa[pur]=q[k];
q[++top]=pur;
}
}
top=;
scanf("%d",&m);
while (m--){
scanf("%d",&opt);
if (opt==){
scanf("%d%d",&x,&y);
if (find(x)==find(y)) {puts("-1");continue;}
link(x,y);
}
else
if (opt==){
scanf("%d%d",&x,&y);
if (find(x)!=find(y)||x==y) {puts("-1");continue;}
cut(x,y);
}
else
if (opt==){
scanf("%d%d%d",&w,&x,&y);
if (find(x)!=find(y)){puts("-1");continue;}
add(x,y,w);
}
else
if (opt==)
{
scanf("%d%d",&x,&y);
if (find(x)!=find(y)){
printf("-1\n");
continue;
}
makeroot(x);access(y);splay(y);
printf("%d\n",mx[y]);
}
}
printf("\n");
}
}
HDU 4010 Query on The Trees(动态树)的更多相关文章
- HDU 4010 Query on The Trees (动态树)(Link-Cut-Tree)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意; 先给你一棵树,有 \(4\) 种操作: 1.如果 \(x\) 和 \(y\) 不在同一 ...
- 动态树(LCT):HDU 4010 Query on The Trees
Query on The Trees Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Othe ...
- HDU 4010.Query on The Trees 解题报告
题意: 给出一颗树,有4种操作: 1.如果x和y不在同一棵树上则在xy连边 2.如果x和y在同一棵树上并且x!=y则把x换为树根并把y和y的父亲分离 3.如果x和y在同一棵树上则x到y的路径上所有的点 ...
- HDU 4010 Query on The Trees(动态树LCT)
Problem Description We have met so many problems on the tree, so today we will have a query problem ...
- HDU 4010 Query on The Trees(动态树)
题意 给定一棵 \(n\) 个节点的树,每个点有点权.完成 \(m\) 个操作,操作四两种,连接 \((x,y)\) :提 \(x\) 为根,并断 \(y\) 与它的父节点:增加路径 \((x,y)\ ...
- HDU 4010 Query on The Trees
Problem Description We have met so many problems on the tree, so today we will have a query problem ...
- hdu 4010 Query on The Trees LCT
支持:1.添加边 x,y2.删边 x,y3.对于路径x,y上的所有节点的值加上w4.询问路径x,y上的所有节点的最大权值 分析:裸的lct...rev忘了清零死循环了两小时... 1:就是link操作 ...
- HDOJ 4010 Query on The Trees LCT
LCT: 分割.合并子树,路径上全部点的点权添加一个值,查询路径上点权的最大值 Query on The Trees Time Limit: 10000/5000 MS (Java/Others) ...
- Hdu 4010-Query on The Trees LCT,动态树
Query on The Trees Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Othe ...
随机推荐
- Altium Designer规则
1.PCB规则 是PCB设计中至关重要的一个环节:保证PCB符合电气要求.机械加工(精度)要求:为自动布局.布线和部分手动布局.布线操作提供依据 为规则检查提供依据,PCB编辑期间,AD会实时地进行一 ...
- PowerShell文件系统(一)前言
PowerShell文件系统(一)前言 3 12 2月, 2014 在 Powershell tagged Powershell教程 / 别名 / 文件系统 by Mooser Lee PowerS ...
- Powershell访问数组
数组的元素可以使用索引寻址,第一个元素的索引为0,第i个元素的索引为i-1,最后一个元素的索引为Count-1,但是Powershell为了使用方便,直接可以将 -1 作为最后的一个元素的索引. PS ...
- BZOJ1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚
1672: [Usaco2005 Dec]Cleaning Shifts 清理牛棚 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 414 Solved: ...
- jquery 的ready() 与window.onload()的区别
做web开发时常用Jquery中$(document).ready()和JavaScript中的window.onload方法,两者都是要在页面加载完成以后加载的方法,但是这两者还是有很大区别的.最近 ...
- JVM基础和调优(二)
主要讲述java虚拟机的内存体系结构 了解了JVM 的一些基础之后,我们来看看java虚拟机内存的体系结构,这个是理解JVM垃圾收集算法的前提,理解了内存结构我们才能够针对不同的部分根据我们的程序进行 ...
- TortoiseGit 使用教程
原文地址:http://blog.csdn.net/ethan_xue/article/details/7749639 git的使用越来越广泛 使用命令比较麻烦,下面讲解一下tortoisegit的使 ...
- NUnit - 使用感受
Nunit使用 最近项目开始大量使用Nunit, 发现Nunit还是有很多好处的. 1. 测试驱动逻辑,这样可以尽最大可能减少“修改”引入的Bug. 如果你修改了一些东西,导致Case跑不过.请检查你 ...
- 【Eclipse DDMS】 Can't bind to local 8600 for debugger
问题原因: 电脑上同时安装了Eclipse 和Android Studio两个ide. 关键是使用eclipse adb连接真机时候,android studio也处于运行状态,后者默认也是要连接ad ...
- ajax(ajax开发)
AJAX即“Asynchronous Javascript And XML”(异步JavaScript和XML),是指一种创建交互式网页应用的网页开发技术. AJAX = 异步 JavaScript和 ...