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 基本概念: 首先 ...
随机推荐
- Poj 2947 widget factory (高斯消元解同模方程)
题目连接: http://poj.org/problem?id=2947 题目大意: 有n种类型的零件,m个工人,每个零件的加工时间是[3,9],每个工人在一个特定的时间段内可以生产k个零件(可以相同 ...
- leaflet在地图上加载本地图片
<link href="~/Scripts/Leaflet/leaflet.css" rel="stylesheet" /><script s ...
- CentOS 6.5使用:[3]使用xftp传递文件
先检查CentOS系统是否安装了FTP服务 [root@centos ~]# rpm -qa | grep vsftpd 如果有内容输出,那么恭喜你,你的系统已经安装了ftp服务 如果没有那么按照 ...
- C++中的数学函数汇总
math.h 数学函数库,一些数学计算的公式的具体实现是放在math.h里,具体有: 1 三角函数 double sin (double); double cos (double); double t ...
- 前端组件化(二):优化 DOM 操作
看看上一节我们的代码,仔细留意一下 changeLikeText 函数,这个函数包含了 DOM 操作,现在看起来比较简单,那是因为现在只有 isLiked 一个状态.由于数据状态改变会导致需要我们去更 ...
- WPF学习09:数据绑定之 Binding to List Data
从WPF学习03:Element Binding我们可以实现控件属性与控件属性的绑定. 从WPF学习07:MVVM 预备知识之数据绑定 我们可以实现控件属性与自定义对象属性的绑定. 而以上两个功能在实 ...
- 微信官方UI库—WeUI
WeUI 为微信 Web 服务量身设计 概述 WeUI是一套同微信原生视觉体验一致的基础样式库,由微信官方设计团队为微信 Web 开发量身设计,可以令用户的使用感知更加统一.包含button.cell ...
- MYSQL 查询方法 统计查询 链接查询 子查询
mysql表格查询方法: 查询: 1.简单查询 select * from Info --查所有数据select Code,Name from Info --查指定列的数据select Code as ...
- Vue全局添加组件或者模块
import Api from './api.js' export default { install (Vue) { Vue.prototype.$Api = Api } } 这种格式就可以在mai ...
- CentOS7 Install Consul
Centos7 Install Consul 原文链接:http://www.cnblogs.com/caoguo/p/5959962.html 1) 环境 2) 安装 # yum install - ...