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.



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
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  2478 

pid=2480" target="_blank" style="color:rgb(26,92,200); text-decoration:none">2480 2481 2479 2476 

题目大意: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);
}
}
}
}

 

HDOJ 题目2475 Box(link cut tree去点找祖先)的更多相关文章

  1. 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)

    题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...

  2. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  3. Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  4. Link/cut Tree

    Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...

  5. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  6. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

  7. [CodeForces - 614A] A - Link/Cut Tree

    A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...

  8. Link Cut Tree 总结

    Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...

  9. 洛谷P3690 [模板] Link Cut Tree [LCT]

    题目传送门 Link Cut Tree 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代 ...

随机推荐

  1. HDU_1072_Nightmare

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目描述:矩阵表示迷宫,0表示墙,1表示路,2表示起点,3表示终点,4表示重置炸弹时间(6秒),你需 ...

  2. Explaining difference between automaticallyAdjustsScrollViewInsets, extendedLayoutIncludesOpaqueBars, edgesForExtendedLayout

    automaticallyAdjustsScrollViewInsets:在导航栏透明时用到 In your viewDidLoad method, add if([self respondsToSe ...

  3. CAD得到所有实体方法(网页版)

    主要用到函数说明: IMxDrawSelectionSet::AllSelect 得到当前空间的所有实体.详细说明如下: 参数 说明 [in,defaultvalue(NULL)] IMxDrawRe ...

  4. Linux kernel 内存 - 页表映射(SHIFT,SIZE,MASK)和转换(32位,64位)

    0. Intro 如下是在32位下的情况,32位下,只有三级页表:PGD,PMD,PTE 在64位情况下,会有四级页表:PGD,PUD,PMD,PTE 但是原理基本上是一样的,本文主要是想记录一下页表 ...

  5. 编译器:gcc, clang, llvm

    clang Clang是LLVM的前端,可以用来编译C,C++,ObjectiveC等语言.传统的编译器通常分为三个部分,前端(frontEnd),优化器(Optimizer)和后端(backEnd) ...

  6. 60s倒计时

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 洛谷——P2236 [HNOI2002]彩票

    P2236 [HNOI2002]彩票 给你$m$个数,从中挑$n$个数,使得这$n$个数的倒数之和恰好等于$\frac{x}{y}$ 常见的剪纸思路: 如果当前的倒数和加上最小可能的倒数和$>$ ...

  8. Bet(The 2016 ACM-ICPC Asia China-Final Contest 思路题)

    题目: The Codejamon game is on fire! Fans across the world are predicting and betting on which team wi ...

  9. C语言二叉树的创建、(先中后序)遍历以及存在的问题

    #include<stdlib.h> #include<stdio.h> #define True 1 #define False 0 typedef char TElemTy ...

  10. java诗词横版--转为竖版

    import java.util.Arrays; /* 诗句横版转成竖版输出 */public class PrintPoem { public static void main(String[] a ...