Time Limit: 10000/5000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
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.
The picture below shows the state after Jack performs “MOVE 4 1”:
Then he performs “MOVE 3 0”, the state becomes:
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.
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
2008 Asia Regional Chengdu
题外话:
在考试中遇到这一题的时候,一开始想到是并查集,但是并查集很显然不能保证几次合并后的祖先是最高级的,只能知道是同一个集合的,之后想到要快速查找祖先有想到了倍增,但事实上在合并更新的操作上会不行。
题解:
这题所需要的操作是合并两棵树,并且快速知道这个树的祖先。
在这里采用splay来维护合并和查找的操作,然后用dfs序来确定一棵树的整体,即一棵树的子节点为[a,a+n],这样才能保证是在以某个节点为祖先的树中操作。
借鉴了以下的题解(大佬的splay写的真好)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<algorithm>
#include<stack>
#define N 200000
struct aa{
int p,ne;
}da[N];
int tr[N][2],fa[N],tou[N],dd,q;
int n,next[N],tmp,size;
char s[20];
void add(int x,int y){da[++dd].p=y;da[dd].ne=tou[x];tou[x]=dd;}
void build(int x){
fa[x]=size;
tr[size][1]=x;
size=x;
for (int i=tou[x];i;i=da[i].ne)build(da[i].p);
fa[x+n]=size;
tr[size][1]=x+n;
size=x+n;
}
inline void rotate(int x,bool f){
int y=fa[x];
int z=fa[y];
tr[y][!f]=tr[x][f];
fa[tr[x][f]]=y;
fa[x]=z;
if (z)tr[z][tr[z][1]==y]=x;
tr[x][f]=y;
fa[y]=x;
}
void splay(int x,int k){
int y=fa[x];
while (y!=k){
int z=fa[y];
bool f=(tr[y][0]==x);
if (z!=k&& f==(tr[z][0]==y))rotate(y,f);
rotate(x,f);
y=fa[x];
}
}
void move(int x,int y){
if (x==y)return;
splay(x,0);splay(x+n,x);
for (int i=y;i;i=fa[i])if (tr[x+n][0]==i)return;
int a=tr[x][0];
int b=tr[x+n][1];
tr[x][0]=tr[x+n][1]=fa[a]=fa[b]=0;
if (a&&b){
while (tr[b][0])b=tr[b][0];
tr[b][0]=a;
fa[a]=b;
}
if (y==0)return;
splay(y,0);
int i;
for (i=tr[y][1];tr[i][0];i=tr[i][0]);
splay(i,y);
tr[i][0]=x;
fa[x]=i;
}
int query(int x){
splay(x,0);
for (;tr[x][0];x=tr[x][0]);
return x;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("hdu2475.in","r",stdin);
freopen("hdu2475.out","w",stdout);
#endif
bool xi=0;
while (scanf("%d",&n)!=EOF){
memset(tou,0,sizeof(tou));
memset(next,0,sizeof(next));
memset(da,0,sizeof(da));
tmp=0;dd=0;
if (!xi)xi=1;
else printf("\n");
for (int i=1;i<=n;i++){
int x;
scanf("%d",&x);
if (x)add(x,i);
else {
next[tmp]=i;
tmp=i;
}
}
for (int i=next[0];i;i=next[i])size=0,build(i);
scanf("%d",&q);
for (int i=1;i<=q;i++){
scanf("%s",s);
if (s[0]=='Q'){
int x;
scanf("%d",&x);
printf("%d\n",query(x));
}else{
int x,y;
scanf("%d%d",&x,&y);
move(x,y);
}
}
for (int i=1;i<=2*n;i++)tr[i][1]=tr[i][0]=fa[i]=0;
//printf("\n");
}
return 0;
}