Box

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

【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】

QUERY
QUERY
MOVE
MOVE
QUERY MOVE
QUERY
MOVE
QUERY

【Sample Output】


【题意】

动态地维护一些盒子套盒子的操作,询问根。

【分析】

盒子与盒子的关系可以直观地用树的结构来表示,一个结点下的子结点可以表示大盒子里面直接套着的小盒子。

所以本题就是一个裸的Link-Cut Tree模型了。

关于LCT树,还是推荐Yang Zhe的QTREE论文吧。

动态树是用访问操作来划分树链,对于每一条树链,使用Splay来维护,用深度作为splay的左右关系。

看了很多代码,觉得还是写不好,总觉得别人的用起来不顺,最后是在自己原来Splay的基础上改的。

原本的整棵树是个splay,但是在LCT中,整棵树是由很多棵分散的Splay组合起来的,于是在其中的一些点上加上root标记,表示以这一点为根下面可以形成一棵splay树。多个这样的splay组合完成之后就是一棵LCT了。

后面的代码中加入了输入输出挂。。。。。。

 /* ***********************************************
MYID : Chen Fan
LANG : G++
PROG : HDU 2475
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; #define MAXN 50010 int sons[MAXN][];
int father[MAXN],pathfather[MAXN],data[MAXN];
bool root[MAXN];
int spttail=; void rotate(int x,int w) //rotate(node,0/1)
{
int y=father[x]; sons[y][!w]=sons[x][w];
if (sons[x][w]) father[sons[x][w]]=y;
father[x]=father[y];
if (father[y]&&(!root[y])) sons[father[y]][y==sons[father[y]][]]=x;
sons[x][w]=y;
father[y]=x; if (root[y])
{
root[x]=true;
root[y]=false;
}
} void splay(int x) //splay(node)
{
while(!root[x])
{
if (root[father[x]]) rotate(x,x==sons[father[x]][]);
else
{
int t=father[x];
int w=(sons[father[t]][]==t);
if (sons[t][w]==x)
{
rotate(x,!w);
rotate(x,w);
} else
{
rotate(t,w);
rotate(x,w);
}
}
}
} void access(int v)
{
int u=v;
v=;
while(u)
{
splay(u);
root[sons[u][]]=true;
sons[u][]=v;
root[v]=false;
v=u;
u=father[u];
}
} int findroot(int v)
{
access(v);
splay(v);
while (sons[v][]) v=sons[v][];
//splay(v,0);
return v;
} void cut(int v)
{
access(v);
splay(v);
father[sons[v][]]=;
root[sons[v][]]=true;
sons[v][]=;
} void join(int v,int w)
{
if (!w) cut(v);
else
{
access(w);
splay(w);
int temp=v;
while(!root[temp]) temp=father[temp];
if (temp!=w)
{
cut(v);
father[v]=w;
}
}
} int INT()
{
char ch;
int res;
while (ch=getchar(),!isdigit(ch));
for (res = ch - '';ch = getchar(),isdigit(ch);)
res = res * + ch - '';
return res;
} char CHAR()
{
char ch, res;
while (res = getchar(), !isalpha(res));
while (ch = getchar(), isalpha(ch));
return res;
} int main()
{
//freopen("2475.txt","r",stdin); int n;
double flag=false;
while(scanf("%d",&n)!=EOF)
{
if (flag) printf("\n");
flag=true; memset(father,,sizeof(father));
memset(sons,,sizeof(sons));
for (int i=;i<=n;i++)
{
//scanf("%d",&father[i]);
father[i]=INT();
root[i]=true;
} int m;
m=INT();
for (int i=;i<=m;i++)
{
char s=CHAR();
if (s=='M')
{
int x,y;
x=INT();
y=INT();
join(x,y);
} else
{
int q;
q=INT();
printf("%d\n",findroot(q));
}
}
} return ;
}

HDU 2475 BOX 动态树 Link-Cut Tree的更多相关文章

  1. 动态树(Link Cut Tree) :SPOJ 375 Query on a tree

    QTREE - Query on a tree #number-theory You are given a tree (an acyclic undirected connected graph) ...

  2. LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)

    为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...

  3. P3690 【模板】Link Cut Tree (动态树)

    P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...

  4. 【刷题】洛谷 P3690 【模板】Link Cut Tree (动态树)

    题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor ...

  5. LuoguP3690 【模板】Link Cut Tree (动态树) LCT模板

    P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...

  6. LG3690 【模板】Link Cut Tree (动态树)

    题意 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的 ...

  7. link cut tree 入门

    鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...

  8. Link Cut Tree学习笔记

    从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...

  9. Link Cut Tree 总结

    Link-Cut-Tree Tags:数据结构 ##更好阅读体验:https://www.zybuluo.com/xzyxzy/note/1027479 一.概述 \(LCT\),动态树的一种,又可以 ...

随机推荐

  1. 8.17HTML 标签

    1.HTML body属性: bgbgcolor      页面背景色 text              文字颜色 topmargin     上页面边距 leftmargin      左 rig ...

  2. 浅谈vertical-align

    vertical-align的有效值为:baseline:sub:super:top:text-top:middle:bottom:text-bottom:length或者百分比值: 对块级元素使用无 ...

  3. ios压缩图片

    /** *  压缩图片到指定文件大小 * *  @param image 目标图片 *  @param size  目标大小(最大值) * *  @return 返回的图片文件 */ - (NSDat ...

  4. SVG中image tag的高亮

    今天下午和晚上全部在实验室调试scs可视化,下午主要时间在调试字符云图的东西,不过感觉没有提升,还是不好看,囧~~ 把力道图中的线条给调整了,调细之后,好看了不少. 因为需要多个区域交互,也就是需要高 ...

  5. nmon的安装与使用

    nmon的安装与使用 1.下载 nmon:http://nmon.sourceforge.net/pmwiki.php?n=Site.Download nmonanalyser http://www. ...

  6. UIWebView是什么

    UIWebView类是用来显示网络内容.要使用它,可以简单的创造一个UIWebView对象,放置到窗口上,并且发送一个指向网络内容的请求.通过这个类,可以控制网页历史的前进後退,也可以通过程序去控制网 ...

  7. Oracle删除死锁进程的方法

    本文实例讲述了Oracle删除死锁进程的方法.分享给大家供大家参考.具体如下: 步骤1:用以下SQL查看进程列表,判断出被锁定的表 复制代码代码如下: SELECT dob.OBJECT_NAME T ...

  8. C# 系统应用之清除Cookies、IE临时文件、历史记录 转载

    http://blog.csdn.net/Eastmount/article/details/18821221 本文主要是项目"个人电脑使用记录清除软件"系类文章中关于清除浏览器C ...

  9. 使用rsync命令提高文件传输效率

    众多数据库服务器的管理过程中,在不同服务器间的文件传输是免不了的.您可以使用scp命令或FTP方法完成文件的发送和接收,这篇文章我将给大家介绍另外一种方法,这就是rsync命令.rsync是文件传输程 ...

  10. LNMP环境的安装配置

    0.安装必要的依赖软件 如果已经安装了可能会进行升级,版本完全一致则不会进行任何操作. yum -y install bzip2-devel curl-devel freetype-devel gcc ...