HDU

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.

InputInput 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.
OutputFor 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

题意:给定一些盒子,以及盒子的嵌套关系,现在有一些操作,可以把盒子移到另外的盒子里。一些询问,问包含x盒子的最外面的盒子是哪个。

思路:题意转化一下,就是森林,Cut(x,y)操作是可以把x为根的子树砍下来,接在y节点下面。 询问Query(x)就是查询x的根。

(模型很裸,还是写一下装进模板里。

像这样维护森林,求根,并查集肯定不够。我们用括号序列来做。

  • 那么点x的子树就的区间就是[x,x+N];
  • 点y在x的子树里,当且仅当pos[x]<=pos[y]<=pos[y+N]<=pos[x+N];(pos是点在splay里面的排名。)
  • 把x加到y的子树里,即[y,y+N]里面加序列[x,x+N];可以把后者加到y的后面。

(LCT应该更贴合这个题

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=100010;
int ch[maxn][2],fa[maxn],Laxt[maxn],Next[maxn],To[maxn],cnt,dd,N;
int get(int x){ return ch[fa[x]][1]==x; }
void rotate(int x)
{
    int old=fa[x],fold=fa[old],opt=get(x);
    fa[x]=fold; fa[old]=x; fa[ch[x][opt^1]]=old;
    ch[old][opt]=ch[x][opt^1];ch[x][opt^1]=old;
    if(fold) ch[fold][ch[fold][1]==old]=x;
}
void splay(int x,int y)
{
    for(int f;(f=fa[x])!=y;rotate(x)){
        if(fa[f]!=y) rotate(get(x)==get(f)?f:x);
    }
}
void add(int u,int v)
{
    Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v;
}
void dfs(int u) //按照括号序建spaly,起初全是单链。
{
    fa[u]=dd; ch[dd][1]=u; dd=u;
    for(int i=Laxt[u];i;i=Next[i]) dfs(To[i]);
    fa[u+N]=dd; ch[dd][1]=u+N; dd=u+N;
}
void build()
{
    for(int i=Laxt[0];i;i=Next[i]){
        dd=0; dfs(To[i]);
    }
}
int query(int x)
{
    splay(x,0);
    int now=x;
    while(ch[now][0]) now=ch[now][0];
    return now;
}
void move(int a,int b)
{
    if(a==b) return ;  //不合法1
    splay(a,0);
    splay(a+N,a);
    for(int t=b;t;t=fa[t]) if(ch[a+N][0]==t) return ; //不合法2
    int x=ch[a][0],y=ch[a+N][1]; fa[x]=fa[y]=ch[a][0]=ch[a+N][1]=0;
    int t=y; while(ch[t][0]) t=ch[t][0];
    splay(t,0); fa[x]=t; ch[t][0]=x;//[a,a+N]区间分离出来,合并剩余部分
    //右边的最小值放根,再合并,保证平衡。
    if(b==0) return ;
    splay(b,0);
    t=ch[b][1]; while(ch[t][0]) t=ch[t][0];
    splay(t,b);// t此时没有左儿子,把[a,a+N]挤进去。
    fa[a]=t; ch[t][0]=a;
}
int main()
{
    int M,x,y,T=0; char opt[6];
    while(~scanf("%d",&N)){
        if(T) printf("
");
        else T++;
        rep(i,0,N) Laxt[i]=0; cnt=0;
        rep(i,0,N+N) ch[i][0]=ch[i][1]=0;
        rep(i,1,N){
            scanf("%d",&x);
            add(x,i);
        }
        build();
        scanf("%d",&M);
        rep(i,1,M){
            scanf("%s",opt);
            if(opt[0]=='Q'){
                scanf("%d",&x);
                printf("%d
",query(x));
            }
            else {
                scanf("%d%d",&x,&y);
                move(x,y);
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/hua-dong/p/10116963.html