Box

Time Limit: 5000ms
Memory Limit: 32768KB

This problem will be judged on HDU. Original ID: 2475
64-bit integer IO format: %I64d      Java class name: Main

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

 
解题:Link-Cut tree
 #include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct LCT{
int fa[maxn],ch[maxn][],parent[maxn];
void init(){
memset(fa,,sizeof fa);
memset(ch,,sizeof ch);
}
void rotate(int x,int kd){
int y = fa[x];
ch[y][kd^] = ch[x][kd];
fa[ch[x][kd]] = y;
fa[x] = fa[y];
ch[x][kd] = y;
fa[y] = x;
if(fa[x]) ch[fa[x]][y == ch[fa[x]][]] = x;
}
void splay(int x,int goal = ){
int y = x;
while(fa[y]) y = fa[y];
if(x != y){
parent[x] = parent[y];
parent[y] = ;
while(fa[x] != goal){
if(fa[fa[x]] == goal) rotate(x,x == ch[fa[x]][]);
else{
int y = fa[x],z = fa[y],s = (y == ch[z][]);
if(x == ch[y][s]){
rotate(x,s^);
rotate(x,s);
}else{
rotate(y,s);
rotate(x,s);
}
}
}
}
}
void access(int x){
for(int y = ; x; x = parent[x]){
splay(x);
fa[ch[x][]] = ;
parent[ch[x][]] = x;
ch[x][] = y;
fa[y] = x;
parent[y] = ;
y = x;
}
}
int GetRoot(int x){
access(x);
splay(x);
while(ch[x][]) x = ch[x][];
return x;
}
void cut(int x){
access(x);
splay(x);
parent[ch[x][]] = parent[x];
parent[x] = ;
fa[ch[x][]] = ;
ch[x][] = ;
}
void join(int x,int y){
if(!y) cut(x);
else{
access(y);
splay(y);
int z = x;
while(fa[z]) z = fa[z];
if(z != y){
cut(x);
parent[x] = y;
}
}
}
}lct;
int main(){
int n,m,u,v;
char op[];
bool flag = false;
while(~scanf("%d",&n)){
lct.init();
if(flag) putchar('\n');
for(int i = ; i <= n; ++i)
scanf("%d",&lct.parent[i]);
scanf("%d",&m);
while(m--){
scanf("%s%d",op,&u);
if(op[] == 'Q') printf("%d\n",lct.GetRoot(u));
else{
scanf("%d",&v);
lct.join(u,v);
}
}
flag = true;
}
return ;
}

HDU 2475 Box的更多相关文章

  1. HDU 2475 BOX 动态树 Link-Cut Tree

    Box Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Problem De ...

  2. hdu 2475 BOX (splay)

    版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 2475 Splay树是一种神奇的东西... 题意: 有一些箱子,要么放在地上,要么放在某个箱子里面 . 现在有两种操作: (1) MOV ...

  3. HDU 2475 Box 树型转线型 + 伸展树

    树型转线型.第一次听说这个概念. . . , 可是曾经已经接触过了,如LCA的预处理部分和树链剖分等.可是没想到还能这么用,三者虽说有不同可是大体思想还是非常相近的,学习了. 推荐博客http://b ...

  4. HDOJ 题目2475 Box(link cut tree去点找祖先)

    Box Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  5. HDU - 2475:Box(splay维护森林)

    There are N boxes on the ground, which are labeled by numbers from 1 to N. The boxes are magical, th ...

  6. Box HDU - 2475 (Splay 维护森林)

    Box \[ Time Limit: 5000 ms \quad Memory Limit: 32768 kB \] 题意 给出 \(n\) 个箱子的包含关系,每次两种操作. 操作 \(1\):把 \ ...

  7. HDU 2088 Box of Bricks

    http://acm.hdu.edu.cn/showproblem.php?pid=2088 Problem Description Little Bob likes playing with his ...

  8. HDU 2088 Box of Bricks(脑洞)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2088 Box of Bricks Time Limit: 1000/1000 MS (Java/Oth ...

  9. 『嗨威说』算法设计与分析 - 贪心算法思想小结(HDU 2088 Box of Bricks)

    本文索引目录: 一.贪心算法的基本思想以及个人理解 二.汽车加油问题的贪心选择性质 三.一道贪心算法题点拨升华贪心思想 四.结对编程情况 一.贪心算法的基本思想以及个人理解: 1.1 基本概念: 首先 ...

随机推荐

  1. _bzoj2038 [2009国家集训队]小Z的袜子(hose)【莫队】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 裸的莫队,注意要先移动右端点再移动左端点. #include <cstdio&g ...

  2. 在面试官问你BS和CS区别的时候如何回答??

    这是我下来整理好的,如果哪里不全,望大家多多指教 C/S是Client/Server的缩写.服务器通常采用高性能的PC.工作站或小型机,并采用大型数据库系统,如Oracle.Sybase.Inform ...

  3. shell 2 解析

    ---- shell 3 /home/oracle/utility/macro/call_autopurge_arch.sh Description: Call purge archive log f ...

  4. reveal.js让程序员做ppt也享受快乐

    前言 程序员除了会写的一手漂亮的代码,也要求做出风格优雅的PPT,诸如向领导汇报工作.向小组成员反馈项目进展自己的工作等等.就本人而言,做ppt还要去找模板,还需要设计风格,内心是焦灼的.于是乎,我搜 ...

  5. 掌握Spark机器学习库-09.6-LDA算法

    数据集 iris.data 数据集概览 代码 package org.apache.spark.examples.examplesforml import org.apache.spark.ml.cl ...

  6. 结对项目--黄金点游戏(邓乐&曾亮)

    #include<stdio.h> #include<stdlib.h> #include<Windows.h> int result[100][1000000]; ...

  7. 【转】nABC法-产品分析思路

    Needs:需求N1,用户最基本需求是什么?N2,市场有多大?N3,行业链如何构成?N4,行业发展趋势如何?N5,扩充的需求有哪些? Approach:解决方案A1,解决方案如何构成?A2,需求优先级 ...

  8. windows SDK创建一个窗体

    #include <windows.h> /* Declare Windows procedure */ LRESULT CALLBACK WindowProcedure (HWND, U ...

  9. RackTables在LNMP系统的安装及使用

    RackTables是一款优秀的机房管理系统,可以十分方便的登记机房设备和连接情况,非常适合小型机房的运维.RackTables是PHP开发的免费系统,最新版本为0.20.14,PHP版本要求不低于P ...

  10. 网络爬虫之框架(Scrapy)

    Scrapy爬虫框架 爬虫框架是实现爬虫功能的一个软件结构和功能组件集合. 爬虫框架是一个半成品,能够帮助用户实现专业网络爬虫. Scrapy爬虫框架结构: