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 ...
随机推荐
- 工控主板EM9161对ISO7816协议的支持
在当前的金融POS终端及相关领域,ISO7816通讯协议得到了广泛应用.英创的工控主板EM9161,可在其异步串口的基础上,通过简单的设置,就可把串口转为符合ISO7816协议的接口,实现与各种智能卡 ...
- MySQL数学函数
官方文档:Numeric Functions and Operators Name Description ABS() Return the absolute value ACOS() Return ...
- (转)PHP zval内存回收机制和refcount_gc和is_ref_gc
出处 : http://blog.sina.com.cn/s/blog_75a2f94f0101gygh.html 对于PHP这种需要同时处理多个请求的程序来说,申请和释放内存的时候应该慎之又慎,一不 ...
- BZOJ2754: [SCOI2012]喵星球上的点名
2754: [SCOI2012]喵星球上的点名 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 680 Solved: 314[Submit][Sta ...
- bzoj有趣的题目
你会发现bzoj上好多题AC率高的让人不敢想象 其实是因为数据没发,所以被n个人水过了-- 1142 1167 1351 1354 1359 1482 2812 3056 1469 我有特殊的减少代码 ...
- Linux磁盘及文件系统管理 2---- 使用fdisk进行磁盘管理
1 FDISK分区工具 1 fsidk是来自IBM的分区工具,支持绝大多数的操作系统,几乎所有的Linux都装有fdisk 2 fdisk是一个支持MBR的分区工具,如果要使用GPT的话我们无法使用f ...
- 解决selenium 启动ie浏览器报错:Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones
启动ie代码: System.setProperty("webdriver.ie.driver", "bin/IEDriverServer.exe"); Web ...
- BeyondCompare两个文件中同一行字符长度不一致的文件对比,比如pi文件对比(xjl456852原创)
假设有两个文件,里面存放的数字都只有一行,但长度不一样,对比时会有问题 示例文件: 对比示例如图: 左边的pi的字符串比较长,右边的比较短. 右边的pi的值不是从开始的第一个位置对比的,这样的情况是有 ...
- PyInstaller打包Python脚本为exe
1.PyInstaller-3.1.1 百度云链接 http://pan.baidu.com/s/1jHYWin8 密码 oapl 2.安装最新版本的 pywin32-217.win32-py2 ...
- Qt5官方demo解析集21——Extending QML - Adding Types Example
本系列全部文章能够在这里查看http://blog.csdn.net/cloud_castle/article/category/2123873 又是一个新的系列了,只是这个系列和我们之前的Chapt ...