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

Box

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

Total Submission(s): 2374    Accepted Submission(s): 718

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

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

2008 Asia Regional Chengdu

Recommend

lcy   |   We have carefully selected several similar problems for you:  2478 2480 2481 2479 2476

题目大意:n个点,然后给出n个点分别的父节点,下边m次操作,move a b,把a放到b里边,b为0,直接放地面,query 问祖先

ac代码

Problem : 2475 ( Box )     Judge Status : Accepted
RunId : 14537757    Language : C++    Author : lwj1994
Code Render Status : Rendered By HDOJ C++ Code Render Version 0.01 Beta
#include<stdio.h>
#include<string.h>
struct LCT
{
    int bef[50050],pre[50050],next[50050][2];
    void init()
    {
        memset(pre,0,sizeof(pre));
        memset(next,0,sizeof(next));
    }
    void rotate(int x,int kind)
    {
        int y,z;
        y=pre[x];
        z=pre[y];
        next[y][!kind]=next[x][kind];
        pre[next[x][kind]]=y;
        next[z][next[z][1]==y]=x;
        pre[x]=z;
        next[x][kind]=y;
        pre[y]=x;
    }
    void splay(int x)
    {
        int rt;
        for(rt=x;pre[rt];rt=pre[rt]);
        if(x!=rt)
        {
            bef[x]=bef[rt];
            bef[rt]=0;
            while(pre[x])
            {
                if(next[pre[x]][0]==x)
                {
                    rotate(x,1);
                }
                else
                    rotate(x,0);
            }
        }
    }
    void access(int x)
    {
        int fa;
        for(fa=0;x;x=bef[x])
        {
            splay(x);
            pre[next[x][1]]=0;
            bef[next[x][1]]=x;
            next[x][1]=fa;
            pre[fa]=x;
            bef[fa]=0;
            fa=x;
        }
    }
    int query(int x)
    {
        access(x);
        splay(x);
        while(next[x][0])
            x=next[x][0];
        return x;
    }
    void cut(int x)
    {
        access(x);
        splay(x);
        bef[next[x][0]]=bef[x];
        bef[x]=0;
        pre[next[x][0]]=0;
        next[x][0]=0;
    }
    void join(int x,int y)
    {
        if(y==0)
            cut(x);
        else
        {
            int tmp;
            access(y);
            splay(y);
            for(tmp=x;pre[tmp];tmp=pre[tmp]);
            if(tmp!=y)
            {
                cut(x);
                bef[x]=y;
            }
        }
    }
}lct;
int main()
{
    int n,flag=0;
    while(scanf("%d",&n)!=EOF)
    {
        int i;
        if(flag)
            printf("\n");
        else
            flag=1;
        for(i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            lct.bef[i]=x;
        }
        int q;
        lct.init();
        scanf("%d",&q);
        while(q--)
        {
            char str[10];
            scanf("%s",str);
            if(str[0]=='Q')
            {
                int x;
                scanf("%d",&x);
                printf("%d\n",lct.query(x));
            }
            else
            {
                int x,y;
                scanf("%d%d",&x,&y);
                lct.join(x,y);
            }
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-27 16:45:06

HDOJ 题目2475 Box(link cut tree去点找祖先)的相关文章

HDOJ 题目3966 Aragorn&#39;s Story(Link Cut Tree成段加减点权,查询点权)

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5505    Accepted Submission(s): 1441 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lor

脑洞大开加偏执人格——可持久化treap版的Link Cut Tree

一直没有点动态树这个科技树,因为听说只能用Splay,用Treap的话多一个log.有一天脑洞大开,想到也许Treap也能从底向上Split.仔细思考了一下,发现翻转标记不好写,再仔细思考了一下,发现还是可以写的,只需要实时交换答案二元组里的两棵树,最后在吧提出来的访问节点放回去就行了.本着只学一种平衡树的想法,脑洞大开加偏执人格的开始写可持久化Treap版的Link Cut Tree... 写了才发现,常数硕大啊!!!代码超长啊!!!因为merge是从上到下,split从下到上,pushdow

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

P3690 [模板]Link Cut Tree (动态树) 题目背景 动态树 题目描述 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两个整数(x,y),代表询问从x到y的路径上的点的权值的xor和.保证x到y是联通的. 1:后接两个整数(x,y),代表连接x到y,若x到y已经联通则无需连接. 2:后接两个整数(x,y),代表删除边(x,y),不保证边(x,y)存在. 3:后接两个整数(x,y),代表将点x上的权值变成y. 输入输出

Link Cut Tree学习笔记

从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子树信息 小结 动态树问题和Link Cut Tree 动态树问题是一类要求维护一个有根树森林,支持对树的分割, 合并等操作的问题. Link Cut Tree(林可砍树?简称LCT)是解决这一类问题的一种数据结构. 一些无聊的定义 Link Cut Tree维护的是动态森林中每棵树的任意链剖分. P

[BJOI2014] 大融合 - Link Cut Tree

经典也是最基础的Link Cut Tree维护子树问题.我们考虑维护子树有哪些不同,无非就是需要去维护虚儿子,在这里就是维护虚儿子的size,记为si pushup(x) 这时的更新来源除了左右儿子和自己以外还有虚儿子 access(x) 由于access操作是做虚实互换的过程,会对虚儿子数据产生影响 link(x,y) 授予了新的父子关系,但这种关系是通过虚儿子实现的 需要注意 cut(x,y) 本身虽然没有影响虚儿子但也要更新一下 #include <bits/stdc++.h> usin

Codeforces Round #339 (Div. 2) A. Link/Cut Tree

A. Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, which is based on Splay trees. Specifically, he is now studying the expose procedure. Unfortunately, Rostislav is unable to understand the definition

AC日记——【模板】Link Cut Tree 洛谷 P3690

[模板]Link Cut Tree 思路: LCT模板: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 300005 int n,m,val[maxn]; int top,ch[maxn][2],f[maxn],xr[maxn],q[maxn],rev[maxn]; inline void in(int &now) { int if_z=1;now=0; char Cget=getchar(); while

bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门

link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isroot(int x):判断x是否为所在重链(splay)的根 void down(int x):下放各种标记 void rotate(int x):在x所在重链(splay)中将x旋转到fa[x]的位置上 void splay(int x):在x坐在重链(splay)中将x旋转到根 void acce

link cut tree 入门

鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. #include<cstdio> #include<cstring> #include<cctype> #include<algorithm> #include<queue> using namespace std; #define rep(i,s,