HDU 2475 Box
Box
This problem will be judged on HDU. Original ID: 2475
64-bit integer IO format: %I64d Java class name: Main
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
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
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
#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的更多相关文章
- HDU 2475 BOX 动态树 Link-Cut Tree
Box Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) [Problem De ...
- hdu 2475 BOX (splay)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 2475 Splay树是一种神奇的东西... 题意: 有一些箱子,要么放在地上,要么放在某个箱子里面 . 现在有两种操作: (1) MOV ...
- HDU 2475 Box 树型转线型 + 伸展树
树型转线型.第一次听说这个概念. . . , 可是曾经已经接触过了,如LCA的预处理部分和树链剖分等.可是没想到还能这么用,三者虽说有不同可是大体思想还是非常相近的,学习了. 推荐博客http://b ...
- HDOJ 题目2475 Box(link cut tree去点找祖先)
Box Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- 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 ...
- Box HDU - 2475 (Splay 维护森林)
Box \[ Time Limit: 5000 ms \quad Memory Limit: 32768 kB \] 题意 给出 \(n\) 个箱子的包含关系,每次两种操作. 操作 \(1\):把 \ ...
- HDU 2088 Box of Bricks
http://acm.hdu.edu.cn/showproblem.php?pid=2088 Problem Description Little Bob likes playing with his ...
- HDU 2088 Box of Bricks(脑洞)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2088 Box of Bricks Time Limit: 1000/1000 MS (Java/Oth ...
- 『嗨威说』算法设计与分析 - 贪心算法思想小结(HDU 2088 Box of Bricks)
本文索引目录: 一.贪心算法的基本思想以及个人理解 二.汽车加油问题的贪心选择性质 三.一道贪心算法题点拨升华贪心思想 四.结对编程情况 一.贪心算法的基本思想以及个人理解: 1.1 基本概念: 首先 ...
随机推荐
- 在spring boot 中使用itext和itextrender生成pdf文件
转载请注明出处 https://www.cnblogs.com/majianming/p/9539376.html 项目中需要对订单生成pdf文件,在第一版本其实已经有了比较满意的pdf文档,但是还是 ...
- php安装ionCube
- Hadoop YARN学习之重要术语总结(6)
Hadoop YARN学习之重要术语总结(6) - SLA服务等级 - RM(ResourceManager) - AM(ApplicationMaster) - NM(NodeMaster) - M ...
- 简说JAVA8引入函数式的问题
JAVA8中加入lambda演算是一个令人兴奋的新特性——虽然这个新特性来得太迟了,目前的主流开发语言中,JAVA似乎是最后一个支持函数式思维的语言. 虽然晚了点,但总比没有好——况且我认为它的实现还 ...
- JVM 优点与缺点的深入分析
Java 最初诞生的时候,它可以说是其他语言的进化版.不仅因为Java很简单,而且这一进化的语言还是一个可以运行第三方硬件字节码的虚拟机.它还是垃圾收集站,从而令存储管理和内核转储(core dump ...
- JavaScript——class与原型对象
原型对象的意义 通过new 一个构造函数,我们能够获得一个实例,在new 的过程中,程序会在内存中申请一块区域,同时我们可以加参数,所以每个对象都不一样. 原型对象则是同一个构造函数 new 出来的所 ...
- Fragment中获取Activity的Context (转)
Fragment中获取Activity的Context时只需要this.getActivity()即可. 而不是许多人说的this.getActivity().getApplicationCo ...
- Android原生方式获取经纬度
两种定位方式:GPS定位.WiFi定位优劣: 如果项目定位要求较高还是建议使用三方地图库 GPS定位相比Wifi定位更精准且可在无网络情况下使用,但在室内基本暴毙无法使用WiFi定位没有室内外限制也不 ...
- 【Linux】CentOS tar压缩与解压命令大全
tar命令详解 -c: 建立压缩档案 -x:解压 -t:查看内容 -r:向压缩归档文件末尾追加文件 -u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用 ...
- windows环境开启PHP fileinfo扩展
fileinfo作用:本模块中的函数通过在文件的给定位置查找特定的 魔术 字节序列 来猜测文件的内容类型以及编码(通俗来讲就是获取文件的MIME信息) 开启PHP fileinfo扩展的方法: 1.下 ...