Hdu 4010-Query on The Trees LCT,动态树
Query on The Trees
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 4091 Accepted Submission(s): 1774
There are N nodes, each node will have a unique weight Wi. We will have four kinds of operations on it and you should solve them efficiently. Wish you have fun!
For each case, the first line contains only one integer N.(1 ≤ N ≤ 300000) The next N‐1 lines each contains two integers x, y which means there is an edge between them. It also means we will give you one tree initially.
The next line will contains N integers which means the weight Wi of each node. (0 ≤ Wi ≤ 3000)
The next line will contains an integer Q. (1 ≤ Q ≤ 300000) The next Q lines will start with an integer 1, 2, 3 or 4 means the kind of this operation.
1. Given two integer x, y, you should make a new edge between these two node x and y. So after this operation, two trees will be connected to a new one.
2. Given two integer x, y, you should find the tree in the tree set who contain node x, and you should make the node x be the root of this tree, and then you should cut the edge between node y and its parent. So after this operation, a tree will be separate into two parts.
3. Given three integer w, x, y, for the x, y and all nodes between the path from x to y, you should increase their weight by w.
4. Given two integer x, y, you should check the node weights on the path between x and y, and you should output the maximum weight on it.
You should output a blank line after each test case.
1 2
2 4
2 5
1 3
1 2 3 4 5
6
4 2 3
2 1 2
4 2 3
1 3 5
3 2 1 4
4 1 4
-1
7
We define the illegal situation of different operations:
In first operation: if node x and y belong to a same tree, we think it's illegal.
In second operation: if x = y or x and y not belong to a same tree, we think it's illegal.
In third operation: if x and y not belong to a same tree, we think it's illegal.
In fourth operation: if x and y not belong to a same tree, we think it's illegal.
题解:
LCT的子树问题。
找到每个点所在的原始树(不是Splay树)的根。
又是子树判断最麻烦。。。
#include<bits/stdc++.h>
using namespace std;
#define MAXN 300010
#define INF 1e9
struct node
{
int left,right,val,mx;
}tree[MAXN];
struct NODE
{
int begin,end,next;
}edge[MAXN*];
int father[MAXN],rev[MAXN],tag[MAXN],U[MAXN],V[MAXN],Stack[MAXN],Head[MAXN],cnt;
void addedge(int bb,int ee)
{
edge[++cnt].begin=bb;edge[cnt].end=ee;edge[cnt].next=Head[bb];Head[bb]=cnt;
}
void addedge1(int bb,int ee)
{
addedge(bb,ee);addedge(ee,bb);
}
int read()
{
int s=,fh=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')fh=-;ch=getchar();}
while(ch>=''&&ch<=''){s=s*+(ch-'');ch=getchar();}
return s*fh;
}
int isroot(int x)
{
return tree[father[x]].left!=x&&tree[father[x]].right!=x;
}
void pushdown(int x)
{
int l=tree[x].left,r=tree[x].right;
if(rev[x]!=)
{
rev[x]^=;rev[l]^=;rev[r]^=;
swap(tree[x].left,tree[x].right);
}
if(tag[x]!=)
{
/*tag[l]+=tag[x];tag[r]+=tag[x];
tree[l].val+=tag[x];tree[r].val+=tag[x];
tree[l].mx+=tag[x];tree[r].mx+=tag[x];*/
if(l!=){tag[l]+=tag[x];tree[l].val+=tag[x];tree[l].mx+=tag[x];}
if(r!=){tag[r]+=tag[x];tree[r].val+=tag[x];tree[r].mx+=tag[x];}
tag[x]=;
}
}
void Pushup(int x)
{
int l=tree[x].left,r=tree[x].right;
tree[x].mx=max(max(tree[l].mx,tree[r].mx),tree[x].val);
}
void rotate(int x)
{
int y=father[x],z=father[y];
if(!isroot(y))
{
if(tree[z].left==y)tree[z].left=x;
else tree[z].right=x;
}
if(tree[y].left==x)
{
father[x]=z;father[y]=x;tree[y].left=tree[x].right;tree[x].right=y;father[tree[y].left]=y;
}
else
{
father[x]=z;father[y]=x;tree[y].right=tree[x].left;tree[x].left=y;father[tree[y].right]=y;
}
Pushup(y);Pushup(x);
}
void splay(int x)
{
int top=,i,y,z;Stack[++top]=x;
for(i=x;!isroot(i);i=father[i])Stack[++top]=father[i];
for(i=top;i>=;i--)pushdown(Stack[i]);
while(!isroot(x))
{
y=father[x];z=father[y];
if(!isroot(y))
{
if((tree[y].left==x)^(tree[z].left==y))rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x)
{
int last=;
while(x!=)
{
splay(x);
tree[x].right=last;Pushup(x);
last=x;x=father[x];
}
}
void makeroot(int x)
{
access(x);splay(x);rev[x]^=;
}
void link(int u,int v)
{
/*access(u);*/makeroot(u);father[u]=v;//splay(u);
}
void cut(int u,int v)
{
/*access(u);*/makeroot(u);access(v);splay(v);/*father[u]=tree[v].left=0;*/father[tree[v].left]=;tree[v].left=;Pushup(v);
}
int findroot(int x)
{
access(x);splay(x);
while(tree[x].left!=)x=tree[x].left;
return x;
}
int main()
{
int n,i,w,x,y,fh,Q,top=,u,j,v;
while(scanf("%d",&n)!=EOF)
{
top=;
for(i=;i<=n;i++)tree[i].val=tree[i].mx=tree[i].left=tree[i].right=rev[i]=tag[i]=father[i]=;
tree[].mx=-INF;
memset(Head,-,sizeof(Head));cnt=;
for(i=;i<n;i++)
{
U[i]=read();V[i]=read();
addedge1(U[i],V[i]);
}
Stack[++top]=;
for(i=;i<=top;i++)
{
u=Stack[i];
for(j=Head[u];j!=-;j=edge[j].next)
{
v=edge[j].end;
if(v!=father[u])
{
father[v]=u;
Stack[++top]=v;
}
}
}
for(i=;i<=n;i++)tree[i].mx=tree[i].val=read();
//for(i=1;i<n;i++)link(U[i],V[i]);
Q=read();
for(i=;i<=Q;i++)
{
fh=read();
if(fh==)
{
x=read();y=read();
if(findroot(x)!=findroot(y))link(x,y);
else {printf("-1\n");continue;}
}
else if(fh==)
{
x=read();y=read();
if(findroot(x)==findroot(y)&&x!=y)
{
/*makeroot(x);*/cut(x,y);
//access(y);access(father[y]);splay(father[y]);father[y]=tree[father[y]].left=0;
}
else {printf("-1\n");continue;}
}
else if(fh==)
{
w=read();x=read();y=read();
if(findroot(x)==findroot(y))
{
makeroot(x);access(y);splay(y);
tag[y]+=w;tree[y].mx+=w;tree[y].val+=w;
}
else {printf("-1\n");continue;}
}
else
{
x=read();y=read();
makeroot(x);access(y);splay(y);
if(findroot(x)!=findroot(y)){printf("-1\n");continue;}
printf("%d\n",tree[y].mx);
}
}
printf("\n");
}
return ;
}
Hdu 4010-Query on The Trees LCT,动态树的更多相关文章
- 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 LCT
支持:1.添加边 x,y2.删边 x,y3.对于路径x,y上的所有节点的值加上w4.询问路径x,y上的所有节点的最大权值 分析:裸的lct...rev忘了清零死循环了两小时... 1:就是link操作 ...
- 动态树(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 (动态树)(Link-Cut-Tree)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意; 先给你一棵树,有 \(4\) 种操作: 1.如果 \(x\) 和 \(y\) 不在同一 ...
- HDU 4010.Query on The Trees 解题报告
题意: 给出一颗树,有4种操作: 1.如果x和y不在同一棵树上则在xy连边 2.如果x和y在同一棵树上并且x!=y则把x换为树根并把y和y的父亲分离 3.如果x和y在同一棵树上则x到y的路径上所有的点 ...
- 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(动态树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4010 题意:一棵树,四种操作: (1)若x和y不在一棵树上,将x和y连边: (2)若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 ...
随机推荐
- Http请求通信(工具类)
Http请求通信(工具类) 异步消息处理流程是: 首先需要在主线程当中创建一个Handle对象,并重写handlerMessage()方法. 然后当子线程中需要进行UI操作时,就创建一个Message ...
- 黑马程序员-IO(二)
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 装饰设计模式: 当想要对已有对象进行功能增强时.可以定义类,将已经有的类传入,基于已经有的功能, ...
- jQuery 遍历过滤
缩写搜索元素的范围 三个最基本的过滤方法是:first(), last() 和 eq(),它们允许您基于其在一组元素中的位置来选择一个特定的元素. 其他过滤方法,比如 filter() 和 not() ...
- 函数还能这样玩儿~实现类似add(1)(2)(3)的函数
人生的第一份前端工作找到了,感谢大神主子们给半路出家自学的我这么多的机会,很高兴正式踏上客观又乐趣满满的程序员之路,哇咔咔咔. 分享一个准备面试时遇到的一个有趣的问题: 要求实现类似add(1)( ...
- Jmeter软件测试3--发送二进制报文
一直用Jmeter测试post接口,但报文信息都是明文方式,今天测试兄弟求助二进制报文如何使用Jmeter测试,查看了项目源码,报文中不仅采用二进制,而且还用java.util.zip进行了压缩,从晚 ...
- mysql 刘道成视频教程1、2课----------大致结构
第1课 第2课 一.整体结构 1.mysql -h localhost -u root -p *** 如果-h不写,则默认连接localhost. 2.连接服务器成功,-------> 显示数据 ...
- How To Read a Paper.md
@ Titile How To Read a Paper.md @ author Keshav, 译 uuplusu # 1. Intro 1. 读论文重要 2. 没有人教 3. ...
- Linux权限体系总结
总结 1. LINUX9位权限及rwx字符的作用.对应的数字及对应的用户和用户组图解. 2. rwx对文件来说代表什么意思. 3. rwx对目录来说代表什么意思. 4. 企业生产环境目录设置权 ...
- Git (2)
要使用Git首先遇到的问题是怎么把文件加到库中. 很简单. 新建一个目录,然后git init. 完成上述工作之后的唯一改动是在当前目录下生成了一个.git的子目录.这个子目录是一个集中的数据库,包含 ...
- 上传图片预览 支持IE8+,FF,Chrome ,保留原图片比例
代码及效果:链接