传送门

搞了这么长时间Splay终于可以搞LCT了,等等,什么是LCT?

$LCT$就是$Link-Cut-Tree$,是维护动态树的一个很高效的数据结构,每次修改和查询的均摊复杂度为$O(logN)$,因为那是用splay维护的,所以时间常数也会比较大..

但是相信有节操的出题人是不会恶意卡$LCT$的!

LCT的具体说明就就那篇喜闻乐见的Qtree研究,虽然我并没有怎么看懂,也许是我太弱了吧,总之和啃别人随手打的博客而言,那篇论文还是相对系统的了。

一句话概括$Link-Cut-Tree$就是Link-Cut-Tree=splay+树剖,所以下面的一些简要介绍沿用树剖和splay的用语。

具体说明几个操作;

0.$Splay$

和普通splay几乎一样,但是有一点需要注意,写在注释里了。

inline bool isroot(int node){
    if(t[node].fa==0)return 1;
    return t[t[node].fa].son[0]!=node&&t[t[node].fa].son[1]!=node;
}
inline int get(int node){return t[t[node].fa].son[1]==node;}
void rotate(int node){
    int old=t[node].fa,oldf=t[old].fa,which=get(node);
    if(!isroot(old))t[oldf].son[t[oldf].son[1]==old]=node;//先判断old是不是根再实现翻转,因为先翻转后无论如何old一定不是根
    t[old].son[which]=t[node].son[which^1];t[t[old].son[which]].fa=old;
    t[node].son[which^1]=old;t[old].fa=node;t[node].fa=oldf;
}
void splay(int x){
    int top=0;q[++top]=x;
    for(int i=x;!isroot(i);i=t[i].fa)q[++top]=t[i].fa;
    for(int i=top;i;i--)downit(q[i]);
    while(!isroot(x)){
         int old=t[x].fa,oldf=t[old].fa;
         if(!isroot(old))rotate(get(x)==get(old)?old:x);
         rotate(x);
    }
}

1.$Access$

这个操作就是把当前节点到当前树根变为重链。

具体实现的话和树剖其实有点像,跳到一条链的根,对链操作,跳掉下一条链的底。

void access(int node){
    int tmp=0;
    while(node){
        splay(node);t[node].son[1]=tmp;
        tmp=node;node=t[node].fa;
    }
}

2.$Reverse$

具体的作用就是把一个节点转到根,和splay实现区间翻转挺像的,代码实现不难理解。

void Reverse(int node){access(node);splay(node);t[node].tag^=1;}

3.$Link$

首先把一个节点转到树根,然后由这个节点向另一节点连条虚边,然后splay一下。

void Link(int noda,int nodb){
    Reverse(noda);
    t[noda].fa=nodb;
    splay(noda);
}

4.$Cut$

和上一个差不多。

void Cut(int noda,int nodb){Reverse(noda);access(nodb);splay(nodb);t[nodb].son[0]=t[noda].fa=0;}

5.$find$

int find(int node){
    access(node);splay(node);
    int tmp=node;
    while(t[tmp].son[0])tmp=t[tmp].son[0];
    return tmp;
}

6.$LCA$

void LCA(int noda,int nodb){//noda is LCA
    Reverse(nodb);access(noda);splay(noda);
}

这些操作拼接起来就是这道题的实现了。

//BZOJ 2049
//by Cydiater
//2016.9.12
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <algorithm>
using namespace std;
#define ll long long
#define up(i,j,n)        for(int i=j;i<=n;i++)
#define down(i,j,n)        for(int i=j;i>=n;i--)
#define FILE "sdoi2008_cave"
const int MAXN=1e6+5;
const int oo=0x3f3f3f3f;
inline int read(){
    char ch=getchar();int x=0,f=1;
    while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int q[MAXN],top=0,head,tail,N,M,noda,nodb;
char s[15];
struct Splay{
    int son[2],fa,tag,siz;
}t[MAXN];
namespace solution{
    inline bool isroot(int node){
        if(t[node].fa==0)return 1;
        return t[t[node].fa].son[0]!=node&&t[t[node].fa].son[1]!=node;
    }
    inline int get(int node){return t[t[node].fa].son[1]==node;}
    inline void downit(int node){
        if(t[node].tag){
            int leftson=t[node].son[0],rightson=t[node].son[1];
            t[leftson].tag^=1;t[rightson].tag^=1;
            swap(t[node].son[0],t[node].son[1]);
            t[node].tag=0;
        }
    }
    void rotate(int node){
        int old=t[node].fa,oldf=t[old].fa,which=get(node);
        if(!isroot(old))t[oldf].son[t[oldf].son[1]==old]=node;//先判断old是不是根再实现翻转,因为先翻转后无论如何old一定不是根
        t[old].son[which]=t[node].son[which^1];t[t[old].son[which]].fa=old;
        t[node].son[which^1]=old;t[old].fa=node;t[node].fa=oldf;
    }
    void splay(int x){
        int top=0;q[++top]=x;
        for(int i=x;!isroot(i);i=t[i].fa)q[++top]=t[i].fa;
        for(int i=top;i;i--)downit(q[i]);
        while(!isroot(x)){
            int old=t[x].fa,oldf=t[old].fa;
            if(!isroot(old))rotate(get(x)==get(old)?old:x);
            rotate(x);
        }
    }
    void access(int node){
        int tmp=0;
        while(node){
            splay(node);t[node].son[1]=tmp;
            tmp=node;node=t[node].fa;
        }
    }
    void Reverse(int node){access(node);splay(node);t[node].tag^=1;}
    void Link(int noda,int nodb){
        Reverse(noda);
        t[noda].fa=nodb;
        splay(noda);
    }
    void Cut(int noda,int nodb){Reverse(noda);access(nodb);splay(nodb);t[nodb].son[0]=t[noda].fa=0;}
    int find(int node){
        access(node);splay(node);
        int tmp=node;
        while(t[tmp].son[0])tmp=t[tmp].son[0];
        return tmp;
    }
}
int main(){
    //freopen(FILE".in","r",stdin);
    //freopen(FILE".out","w",stdout);
    freopen("input.in","r",stdin);
    using namespace solution;
    N=read();M=read();
    while(M--){
        scanf("%s",s);noda=read();nodb=read();
        if(s[0]=='C')Link(noda,nodb);
        if(s[0]=='D')Cut(noda,nodb);
        if(s[0]=='Q')puts(find(noda)==find(nodb)?"Yes":"No");
    }
    return 0;
}

BZOJ2049: [Sdoi2008]Cave 洞穴勘测 Link-Cut-Tree 模板题的更多相关文章

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

    link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...

  2. BZOJ2049 SDOI2008 Cave 洞穴勘测 【LCT】

    BZOJ2049 SDOI2008 Cave 洞穴勘测 Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分 ...

  3. 【LCT】BZOJ2049 [SDOI2008]Cave 洞穴勘测

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 10059  Solved: 4863[Submit ...

  4. [BZOJ2049][Sdoi2008]Cave 洞穴勘测 LCT模板

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 9705  Solved: 4674[Submit] ...

  5. [bzoj2049][Sdoi2008]Cave 洞穴勘测_LCT

    Cave 洞穴勘测 bzoj-2049 Sdoi-2008 题目大意:维护一个数据结构,支持森林中加边,删边,求两点连通性.n个点,m个操作. 注释:$1\le n\le 10^4$,$1\le m\ ...

  6. [BZOJ2049] [SDOI2008] Cave 洞穴勘测 (LCT)

    Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...

  7. BZOJ2049——[Sdoi2008]Cave 洞穴勘测

    1.题目大意:就是一个动态维护森林联通性的题 2.分析:lct模板题 #include <stack> #include <cstdio> #include <cstdl ...

  8. [bzoj2049][Sdoi2008]Cave 洞穴勘测——lct

    Brief Description 给定一个森林,您需要支持两种操作: 链接两个节点. 断开两个节点之间的链接. Algorithm Design 对于树上的操作,我们现在已经有了树链剖分可以处理这些 ...

  9. bzoj2049: [Sdoi2008]Cave 洞穴勘测 lct裸题

    题意:三种操作一种摧毁一条边,一种链接一条边,一种查询两个点是否联通 题解:lct的link和cut即可 /********************************************** ...

随机推荐

  1. jQuery 插件编程精讲与技巧

    适应的读者: 1.有一定的jquery编程基础但是想在技能上有所提升的人 2.前端开发的程序员 3.对编程感兴趣的学生 为什么要学习jquery插件的编写? 为什么要学习jquery插件的编写?相信这 ...

  2. mysql创建触发器

    触发器语句只有一句话 可以省略begin和end CREATE trigger `do_praise` after insert on praise for each row update post ...

  3. Windows10易升下载

    为了更好的帮助用户快速跨版本升级windows,退出Windows易升!在线下载,更新安装!网速快的话需要半个小时搞定! 升级完毕,如股票感觉OK.记得清理C盘Windows.old文件 01.磁盘- ...

  4. 【摘录】某表含有N个字段超精简模糊查询方法

    SELECT * FROM  table where CONCAT(a,b,c......) like '%key%' select name from syscolumns where id=obj ...

  5. 使用 screen 管理你的远程会话

    文章转载自:https://www.ibm.com/developerworks/cn/linux/l-cn-screen/ 在此只作为笔记使用,不做他用 你是不是经常需要 SSH 或者 telent ...

  6. javascript 数组实例

    在遍历数组时, 如果想要排除 null / undefined 和 不存在的元素时,代码如下: for ( var i = 0; i < a.length; i++ ){ //跳过null / ...

  7. 在Eclipse 中打开当前文件夹

    最近试过好多次,安装插件来 在Eclipse 中打开当前文件所在文件夹,结果总是不甚如意. 烦躁了,决定还是不要使用插件了!!! 1.打开Eclipse,点击菜单栏上的Run--External To ...

  8. bzoj 1001

    Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一 ...

  9. 用git管理源代码

    自从做iOS开发的一年多以来,之前一直都是用svn进行代码管理.因为工作需要,我也开始用git管理源代码.关于git的基本使用,在此做一个详细的介绍,希望能对初次接触git的人有所帮助! 本篇博客针对 ...

  10. 【LintCode】计算两个数的交集(二)

    问题分析: 用两个指针分别遍历即可. 问题求解: public class Solution { /** * @param nums1 an integer array * @param nums2 ...