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

Problem Description
We have met so many problems on the tree, so today we will have a query problem on a set of trees. 
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!
 
Input
There are multiple test cases in our dataset. 
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. 
 
Output
For each query you should output the correct answer of it. If you find this query is an illegal operation, you should output ‐1. 
You should output a blank line after each test case. 
 
Sample Input
5
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
 
Sample Output
3
-1
7
Hint

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.

 
Source
 
Recommend
lcy
 

题解:

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 ;
}
上一篇:每天一个linux命令(19):find 命令概览


下一篇:JSONP(转)