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
题解
每个箱子向包含它的箱子连边,这样就构成了森林,用lct维护连通性即可
代码
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
int n,m,tot=0;
int fa[50005],c[50005][2],f[50005];
using namespace std;
int read()
{
int x=0;char ch=getchar();
while (ch<'0'||ch>'9'){ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x;
}
bool isroot(int x)
{
return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
}
void rotate(int x)
{
int l,r,y=fa[x],z=fa[y];
if (c[y][0]==x) l=0;else l=1;r=l^1;
if (!isroot(y))
{
if (c[z][0]==y) c[z][0]=x;else c[z][1]=x;
}
fa[x]=z;fa[y]=x;
fa[c[x][r]]=y;c[y][l]=c[x][r];
c[x][r]=y;
}
void splay(int x)
{
while (!isroot(x))
{
int y=fa[x],z=fa[y];
if (!isroot(y))
{
if (c[z][0]==y^c[y][0]==x) rotate(x);else rotate(y);
}
rotate(x);
}
}
void access(int x)
{
int t=0;
while (x)
{
splay(x);
c[x][1]=t;
t=x;
x=fa[x];
}
}
int root(int x)
{
access(x);
splay(x);
while (c[x][0]) x=c[x][0];
return x;
}
void cut(int x)
{
access(f[x]);
splay(x);
fa[x]=0;
}
void link(int x,int y)
{
cut(x);
if (!y)
{
f[x]=0;
return;
}
if (root(y)==x)
{
splay(x);
fa[x]=f[x];
return;
}
fa[x]=y;
f[x]=y;
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
for (int i=0;i<=n;i++)f[i]=fa[i]=c[i][0]=c[i][1]=0;
for (int i=1;i<=n;i++)
{
fa[i]=read();
f[i]=fa[i];
}
m=read();
char ch[10];
if(tot++)printf("\n");
while (m--)
{
scanf("%s",ch);
if (ch[0]=='M')
{
int x=read(),y=read();
if (x==y) continue;
link(x,y);
}
else
{
int x=read();
printf("%d\n",root(x));
}
}
}
return 0;
}
代码2
判定方式调了好久,发现致命错误
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
using namespace std;
int n,m,tot=0;
int fa[50005],c[50005][2],f[50005];
int read()
{
int x=0;char ch=getchar();
while (ch<'0'||ch>'9'){ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x;
}
bool isroot(int x)
{
return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
}
int find(int x)
{
while (fa[x]&&!isroot(x)) x=fa[x];//一定要判断x是否是辅助树的根,漏了这个调了半天,气死。
return x;
}
void rotate(int x)
{
int l,r,y=fa[x],z=fa[y];
if (c[y][0]==x) l=0;else l=1;r=l^1;
if (!isroot(y))
{
if (c[z][0]==y) c[z][0]=x;else c[z][1]=x;
}
fa[x]=z;fa[y]=x;
fa[c[x][r]]=y;c[y][l]=c[x][r];
c[x][r]=y;
}
void splay(int x)
{
while (!isroot(x))
{
int y=fa[x],z=fa[y];
if (!isroot(y))
{
if (c[z][0]==y^c[y][0]==x) rotate(x);else rotate(y);
}
rotate(x);
}
}
void access(int x)
{
int t=0;
while (x)
{
splay(x);
c[x][1]=t;
t=x;
x=fa[x];
}
}
int root(int x)
{
access(x);
splay(x);
while (c[x][0]) x=c[x][0];
return x;
}
void cut(int x)
{
access(f[x]);
splay(x);
fa[x]=0;
f[x]=0;
}
void link(int x,int y)
{
fa[x]=y;
f[x]=y;
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
for (int i=0;i<=n;i++)f[i]=fa[i]=c[i][0]=c[i][1]=0;
for (int i=1;i<=n;i++)f[i]=fa[i]=read();
m=read();
char ch[10];
if(tot++)printf("\n");
while (m--)
{
scanf("%s",ch);
if (ch[0]=='M')
{
int x=read(),y=read();
if (x==y) continue;
if (y==0)
{
cut(x);
continue;
}
access(y);
splay(y);
if (find(x)==y) continue;
cut(x);
fa[x]=y;
f[x]=y;
}
else
{
int x=read();
printf("%d\n",root(x));
}
}
}
return 0;
}