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 基本概念: 首先 ...
随机推荐
- Xcode配置SVN详细步骤
转载:http://blog.csdn.net/weiqubo/article/details/8288635 Xcode 默认自带Git 与 SVN,我们本篇介绍SVN的详细配置步骤如下: 1. ...
- 451 Sort Characters By Frequency 根据字符出现频率排序
给定一个字符串,请将字符串里的字符按照出现的频率降序排列.示例 1:输入:"tree"输出:"eert"解释:'e'出现两次,'r'和't'都只出现一次.因此' ...
- 转】MongoDB主从复制实验 master/slave
原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/4/ 感谢! Posted: May 31, 2013 Ta ...
- [译]curl_multi_add_handle
NAMEcurl_multi_add_handle - add an easy handle to a multi session添加easy handle到multi session中 SYNOPS ...
- AJPFX总结mysql复制表结构,表数据
1.复制表结构及数据到新表CREATE TABLE 新表 SELECT * FROM 旧表 这种方法会将oldtable中所有的内容都拷贝过来,当然我们可以用delete from newtable; ...
- 2.3点击菜单显示div再点击就隐藏
事件:onclick 属性:display 利用if语句实现 <!DOCTYPE html><html><head><meta charset="u ...
- Mac下部署与启动STF
一.stf在Mac下的部署1.安装Java及jdk可自己谷歌(如果不能自建云梯)2.安装nodejs包(我是直接在官网下载的LTS版本) • Node.js v8.12.0 to /usr/local ...
- AIX RAC 安装失败完全卸载
1,删除软件安装目录 rm -rf /u01/app 2,删除以下目录内容 rm -rf/tmp/.oracle rm -rf/tmp/* rm -rf/tmp/ora* rm -rf/var/tmp ...
- OpenFlow_tutorial_2_Install_Required_Software
一.Required Software 我操作系统用的 ubuntu 18.04.vm image的OS是ubuntu14.04,这两个系统的GUI应该已经不兼容了,如果使用ubuntu18.04的主 ...
- CREATE CONVERSION - 定义一个用户定义的码制转换
SYNOPSIS CREATE [DEFAULT] CONVERSION name FOR source_encoding TO dest_encoding FROM funcname DESCRIP ...