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 ...
随机推荐
- Android开发手记(18) 数据存储三 SQLite存储数据
Android为数据存储提供了五种方式: 1.SharedPreferences 2.文件存储 3.SQLite数据库 4.ContentProvider 5.网络存储 SQLite 是以嵌入式为目的 ...
- font awesome icon
http://fontawesome.io/icons/ http://www.bootstrapicons.com/
- C#之垃圾回收
垃圾回收时现代语言的标志之一.垃圾回收解放了手工管理对象释放的工作,提高了程序的健壮性,但是副作用就是程序代码可以对于创建对象变得随意. 1.避免不必要的对象创建 由于垃圾回收的代价较高,所以C#程序 ...
- Python 手册——开胃菜
如果你写过大规模的Shell脚本,应该会有过这样的体会:你还非常想再加一些别的功能进去,但它已经太大. 太慢.太复杂了:或者这个功能需要调用一个系统函数,或者它只适合通过C来调用……通常这些问题还不足 ...
- gnuplot使用
直接用yum安装gnuplot即可,例如 sudo sh -c "yum install gnuplot.x86_64 " 安装以后就可以使用了 编写gnuplot脚本 # grp ...
- JDK源码阅读(五)java.io.Serializable接口
package java.io; public interface Serializable { } (1)实现Serializable接口的类,将会被提示提供一个 serialVersionUID ...
- C++中四种类型转换方式
类型转换有c风格的,当然还有c++风格的.c风格的转换的格式很简单(TYPE)EXPRESSION,但是c风格的类型转换有不少的缺点,有的时候用c风格的转换是不合适的,因为它可以在任意类型之间转换,比 ...
- 提高matlab运行速度和节省空间的心得
提高matlab运行速度和节省空间的心得 首先推荐使用matlab 2006a版本,该版本优点很多(不过有一个小bug,就是通过GUI自动生成的m文件居然一大堆warning,希望在已经发布了的200 ...
- 使用Unity游戏引擎在IOS模拟器中运行的方法
在Unity编译IOS程序时,在Unity导航栏菜单中选择Edit->ProjectSettings ->Player(菜单项)选择IOS平台在下方SDK Version处选择运行设备为I ...
- 升级 Java 编程规范的6个约定
作为 Java 开发人员,我们会遵循一系列的编码风格和开发习惯.习惯使然是一方面,另一方面,我们也从不停下脚步质疑这些习惯.一段时间以后,笔者养成了一些不同于常人的编码风格和开发习惯.当第一次了解到这 ...