当前位置:网站首页>hdu2475 Box
hdu2475 Box
2022-08-08 15:15:00 【51CTO】
http://www.elijahqi.win/2018/03/04/hdu2475-box/
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
because a tree with roots 所以lctRoots cannot be replaced 那怎么办 设f[x]Represents the true root in the form of the tree Because there is an illegal situation So cut the subtree first and then look at itx是否在y的上面 如果在 Then take it back Note that when you take it back, you need to put it back splay到根 Otherwise the location will be wrong If the operation is legal then select willx接到y上去 Also pay attention to changing in the real treef数组
边栏推荐
猜你喜欢
随机推荐
Fast DDS 共享内存测试例修改栈空间大小的方式-如改为30M
JS-BOM-名字转换器-输入名字位置颠倒
PHP —— 用 ThinkPHP5.0 实现微信小程序登陆
1052. The Angry Bookstore Boss
Interview questions 17.05. Letters and numbers
web-sql注入
瑞吉外卖学习笔记2
返回分页查询分类并统计多对多关系表中各分类下的应用数量
【Unity入门计划】用双血条方法控制伤害区域减血速度
儿子满墙奖状却没考上重点高中,妈妈愤怒撕下痛哭:不读出去打工
761. 特殊的二进制序列 : 经典构造题
腾讯又一长达 8 年的服务下架。。。
文档管理系统:攻克这3个痛点,解决80%企业文档管理难题
See how three years of CRUD programmers solve database deadlocks
PHP —— CI 框架实现微信小程序支付
小程序轮播图实现由远及近动画
你真的会软件测试bug分析定位嘛
kali换源详细步骤
增效降本开源节流,2022年技术趋势前瞻(异步编程/容器技术)
消除游戏中宝石下落的原理和实现