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. JDBC使用游标实现分页查询的方法

    本文实例讲述了JDBC使用游标实现分页查询的方法.分享给大家供大家参考,具体如下: /** * 一次只从数据库中查询最大maxCount条记录 * @param sql 传入的sql语句 * @par ...

  2. windows2008 rs+sql 2008 下配置站点权限

    几点注意 Windows 2008 iis7.5  1 建立应用程序池 2 文件目录的权限加上 IIS AppPool\应用程序池名称 (找不到直接录入) 3 文件目录要给 IUser权限,不然出错. ...

  3. SQL SERVER 2008 在某表中新增一列时失败

    背景:新增列语句如:“alter table 表名 add 列名 float default 0 with values”(用VS2010做网站,这句话是在C#代码里执行的) 报错提示: 警告: 已经 ...

  4. 使用cloudcompare进行对比轨迹及评价

    0.预备知识: 我的系统是Ubuntu 16.04. 在其他发行版中,可能需要先安装snap(如有必要,请参阅相应的文档).快照发布在3个频道:“稳定”,“测试版”和“边缘”.“稳定版”和“测试版”频 ...

  5. datagrid总条数

    1.getData var data=$("#dg").datagrid("getData");alert('总数据量:' + data.total)//注意你 ...

  6. for循环,isinstance() 函数

    #isinstance()的运用 #练习: 求值总和以及平均值. str_list = [1,2,3,4,5,6,'a',7,8,9,'b',10,'c'] my_tal = 0 my_var = 0 ...

  7. enote笔记法的思考

    章节:enote笔记法的思考   why enote笔记法: key1)大脑喜欢颜色. 我们的大脑天生就喜欢颜色.对颜色很敏感,这是由我们人类过去的演化历程决定的. 你可以理解为,文字有了颜色,让这个 ...

  8. 本地搭建easy-mock

    easy-mock要用nodejs启动,需要先安装nodejs ubuntu系统: apt install node centos系统: curl --silent --location https: ...

  9. Linux之awk用法

    简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再 ...

  10. CentOS 6磁盘管理

    1.添加4块8G硬盘, 注:要先添加SCSI控制器,再添加SCSI硬盘 2.查看添加的硬盘 3.fdisk分区交互式命令 d delete a partition——————//删除一个分区 n ad ...