HDOJ 题目2475 Box(link cut tree去点找祖先)
2017-06-22 10:20 tlnshuju 阅读(178) 评论(0) 编辑 收藏 举报Box
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2374 Accepted Submission(s): 718
Problem Description
There are N boxes on the ground, which are labeled by numbers from 1 to N. The boxes are magical, the size of each one can be enlarged or reduced arbitrarily.
Jack can perform the “MOVE x y” operation to the boxes: take out box x; if y = 0, put it on the ground; Otherwise, put it inside box y. All the boxes inside box x remain the same. It is possible that an operation is illegal, that is, if box y is contained (directly or indirectly) by box x, or if y is equal to x.
In the following picture, box 2 and 4 are directly inside box 6, box 3 is directly inside box 4, box 5 is directly inside box 1, box 1 and 6 are on the ground.
![](http://acm.hdu.edu.cn/data/images/C165-1002-1.jpg)
The picture below shows the state after Jack performs “MOVE 4 1”:
![](http://acm.hdu.edu.cn/data/images/C165-1002-2.jpg)
Then he performs “MOVE 3 0”, the state becomes:
![](http://acm.hdu.edu.cn/data/images/C165-1002-3.jpg)
During a sequence of MOVE operations, Jack wants to know the root box of a specified box. The root box of box x is defined as the most outside box which contains box x. In the last picture, the root box of box 5 is box 1, and box 3’s root box is itself.
Jack can perform the “MOVE x y” operation to the boxes: take out box x; if y = 0, put it on the ground; Otherwise, put it inside box y. All the boxes inside box x remain the same. It is possible that an operation is illegal, that is, if box y is contained (directly or indirectly) by box x, or if y is equal to x.
In the following picture, box 2 and 4 are directly inside box 6, box 3 is directly inside box 4, box 5 is directly inside box 1, box 1 and 6 are on the ground.
![](http://acm.hdu.edu.cn/data/images/C165-1002-1.jpg)
The picture below shows the state after Jack performs “MOVE 4 1”:
![](http://acm.hdu.edu.cn/data/images/C165-1002-2.jpg)
Then he performs “MOVE 3 0”, the state becomes:
![](http://acm.hdu.edu.cn/data/images/C165-1002-3.jpg)
During a sequence of MOVE operations, Jack wants to know the root box of a specified box. The root box of box x is defined as the most outside box which contains box x. In the last picture, the root box of box 5 is box 1, and box 3’s root box is itself.
Input
Input contains several test cases.
For each test case, the first line has an integer N (1 <= N <= 50000), representing the number of boxes.
Next line has N integers: a1, a2, a3, ... , aN (0 <= ai <= N), describing the initial state of the boxes. If ai is 0, box i is on the ground, it is not contained by any box; Otherwise, box i is directly inside box ai. It is guaranteed that the input state is always correct (No loop exists).
Next line has an integer M (1 <= M <= 100000), representing the number of MOVE operations and queries.
On the next M lines, each line contains a MOVE operation or a query:
1. MOVE x y, 1 <= x <= N, 0 <= y <= N, which is described above. If an operation is illegal, just ignore it.
2. QUERY x, 1 <= x <= N, output the root box of box x.
For each test case, the first line has an integer N (1 <= N <= 50000), representing the number of boxes.
Next line has N integers: a1, a2, a3, ... , aN (0 <= ai <= N), describing the initial state of the boxes. If ai is 0, box i is on the ground, it is not contained by any box; Otherwise, box i is directly inside box ai. It is guaranteed that the input state is always correct (No loop exists).
Next line has an integer M (1 <= M <= 100000), representing the number of MOVE operations and queries.
On the next M lines, each line contains a MOVE operation or a query:
1. MOVE x y, 1 <= x <= N, 0 <= y <= N, which is described above. If an operation is illegal, just ignore it.
2. QUERY x, 1 <= x <= N, output the root box of box x.
Output
For each query, output the result on a single line. Use a blank line to separate each test case.
Sample Input
2 0 1 5 QUERY 1 QUERY 2 MOVE 2 0 MOVE 1 2 QUERY 1 6 0 6 4 6 1 0 4 MOVE 4 1 QUERY 3 MOVE 1 4 QUERY 1
Sample Output
1 1 2 1 1
Source
Recommend
题目大意:n个点,然后给出n个点分别的父节点,下边m次操作,move a b,把a放到b里边。b为0。直接放地面,query 问祖先
ac代码
Problem : 2475 ( Box ) Judge Status : Accepted RunId : 14537757 Language : C++ Author : lwj1994 Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta #include<stdio.h> #include<string.h> struct LCT { int bef[50050],pre[50050],next[50050][2]; void init() { memset(pre,0,sizeof(pre)); memset(next,0,sizeof(next)); } void rotate(int x,int kind) { int y,z; y=pre[x]; z=pre[y]; next[y][!kind]=next[x][kind]; pre[next[x][kind]]=y; next[z][next[z][1]==y]=x; pre[x]=z; next[x][kind]=y; pre[y]=x; } void splay(int x) { int rt; for(rt=x;pre[rt];rt=pre[rt]); if(x!=rt) { bef[x]=bef[rt]; bef[rt]=0; while(pre[x]) { if(next[pre[x]][0]==x) { rotate(x,1); } else rotate(x,0); } } } void access(int x) { int fa; for(fa=0;x;x=bef[x]) { splay(x); pre[next[x][1]]=0; bef[next[x][1]]=x; next[x][1]=fa; pre[fa]=x; bef[fa]=0; fa=x; } } int query(int x) { access(x); splay(x); while(next[x][0]) x=next[x][0]; return x; } void cut(int x) { access(x); splay(x); bef[next[x][0]]=bef[x]; bef[x]=0; pre[next[x][0]]=0; next[x][0]=0; } void join(int x,int y) { if(y==0) cut(x); else { int tmp; access(y); splay(y); for(tmp=x;pre[tmp];tmp=pre[tmp]); if(tmp!=y) { cut(x); bef[x]=y; } } } }lct; int main() { int n,flag=0; while(scanf("%d",&n)!=EOF) { int i; if(flag) printf("\n"); else flag=1; for(i=1;i<=n;i++) { int x; scanf("%d",&x); lct.bef[i]=x; } int q; lct.init(); scanf("%d",&q); while(q--) { char str[10]; scanf("%s",str); if(str[0]=='Q') { int x; scanf("%d",&x); printf("%d\n",lct.query(x)); } else { int x,y; scanf("%d%d",&x,&y); lct.join(x,y); } } } }