BoxTime
limit:10000/5000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2374 Accepted Submission (s): 718
Problem Descriptionthere is N boxes on the ground, which is labeled by numbers from 1 to N. The boxes is 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 a operation is illegal, which is, if Box y are contained (directly or indirectly) by box X, or if Y is possible 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 is on the ground.
The picture below shows, 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. 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 had 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 was on the ground, it's not contained by any box; Otherwise, Box I is directly inside box AI. It is guaranteed, the input state was always correct (No loop exists).
Next line have 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, the output of the result is on a single line. Use a blank line to separate each test case.
Sample Input
15QUERY 1QUERY 2MOVE 2 0MOVE 1 2QUERY 6 4 6 1 04MOVE 4 1QUERY 3MOVE 1 4QUERY 1
Sample Output
11211
Source2008 Asia Regional Chengdu
Recommendlcy | We have carefully selected several similar problems for you:2478 2480 2481 2479 2476 The main idea: N points, and then gives the n points respectively of the parent node, the bottom m operation, move A B, put a in B inside, B is 0, directly on the ground, query asked ancestors AC code
problem:2475 (Box) Judge status:acceptedrunid:14537757 language:c++ author:lwj1994code 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); } } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdoj topic 2475 Box (link cut tree go to point to find ancestors)