• 【学习心得】Link-cut Tree


    Link-cut Tree是一种支持改变树(森林)的形态(link和cut),同时维护树的路径上节点信息的数据结构。lct通过splay来维护每次的perferred path,说白了就是一个动态的树链剖分。splay的左右儿子分别代表preferred path上深度比它小和深度比它大的节点。

    Link-cut Tree需要支持以下的操作:

    1.access(x): 将节点x连接到perferred path上,返回值是一个节点,如果这是第一次access,则返回根节点,如果之前还access其他节点,则返回lca(last, x)。
    2.makeroot(x): 将x这个节点作为根,也就是换根操作。
    3.link(x,y): 连接x和y所在的子树。
    4.cut(x,y): 将x和y之间的边删除。

    我们先来看看access操作,借用Yang Zhe大神论文里的一张图来对access操作有一个形象的理解:
    Link-Cut Tree中的access操作
    这一次access过后,从N到根的路径都变成了perferred path。

    然后我们再来看看makeroot操作,我们单次makeroot操作就是把需要作为根的节点access,然后splay再到顶端,再通过给这个节点打上rev标记来将左右儿子交换。(不理解的可以画一张图理解一下。)
    这下有了makeroot也有了access,就很容易地处理出一条链上的信息了,link就只要makeroot(a),fa[a]=b就好了,cut只要makeroot(a),access(b),splay(a),ch[a][0]=fa[b]=0就能解决。

    参考模板:

    //
    //  Title : LCT(change root)
    //  Date : 03.05.2016
    //  Test : BZOJ-2049
    //  Complexity : O(mlogn)
    //  
    /*
        对于有link和cut操作维护树上的信息等问题——
        解决办法:link-cut tree
    */
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    
    #ifdef WIN32
        #define LL "%I64d"
    #else
        #define LL "%lld"
    #endif
    
    #ifdef CT
        #define debug(...) printf(__VA_ARGS__)
        #define setfile() 
    #else
        #define debug(...)
        #define filename ""
        #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
    #endif
    
    #define R register
    #define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
    #define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
    #define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
    #define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
    #define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
    char B[1 << 15], *S = B, *T = B;
    inline int FastIn()
    {
        R char ch; R int cnt = 0; R bool minus = 0;
        while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
        ch == '-' ? minus = 1 : cnt = ch - '0';
        while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
        return minus ? -cnt : cnt;
    }
    #define maxn 200010
    int n, m;
    struct Node *null;
    struct Node
    {
        bool rev;
        Node *ch[2], *fa;
        inline bool type()
        {
            return fa -> ch[1] == this;
        }
        inline bool check()
        {
            return this == fa -> ch[type()];
        }
        inline void set_rev()
        {
            std::swap(ch[0], ch[1]);
            rev ^= 1;
        }
        inline void pushdown()
        {
            if (rev)
            {
                ch[0] -> set_rev();
                ch[1] -> set_rev();
                rev = 0;
            }
        }
        void pushdownall()
        {
            if (check())
                fa -> pushdownall();
            pushdown();
        }
        inline void rotate()
        {
            R Node *f = fa;
            R bool d = type();
            (fa = f -> fa), f -> check() ? fa -> ch[f -> type()] = this : 0;
            (f -> ch[d] = ch[!d]) != null ? ch[!d] -> fa = f : 0;
            (ch[!d] = f) -> fa = this;
        }
        inline void splay(R bool need = 1)
        {
            if (need) pushdownall();
            for (; check(); rotate())
                if (fa -> check())
                    (type() != fa -> type() ? this : fa) -> rotate();
        }
        inline Node *access()
        {
            R Node *i = this, *j = null;
            for (; i != null; i = (j = i) -> fa)
            {
                i -> splay();
                i -> ch[1] = j;
            }
            return j;
        }
        inline void make_root()
        {
            access();
            splay(0);
            set_rev();
        }
        inline void link(R Node *that)
        {
            make_root();
            fa = that;
        }
        inline void cut(R Node *that)
        {
            make_root();
            that -> access();
            splay(0);
            that -> fa = ch[1] = null;
        }
        inline bool find(R Node *that)
        {
            access();
            splay();
            while (that -> fa != null)
                that = that -> fa;
            return that == this;
        }
    }mem[maxn];
    int main()
    {
    //  setfile();
        n = FastIn(), m = FastIn();
        null = mem;
        null -> fa = null -> ch[0] = null -> ch[1] = null;
        for (R int i = 1; i <= n; ++i) mem[i] = (Node) {0, {null, null}, null};
        for (R int i = 1; i <= m; ++i)
        {
            R char opt;
            while (opt = getc(), opt < 'A' || opt > 'Z');
            R int a = FastIn(), b = FastIn();
            if (opt == 'C')
            {
                (mem + a) -> link(mem + b);
            }
            else if (opt == 'D')
            {
                (mem + a) -> cut(mem + b);
            }
            else
            {
                puts((mem + a) -> find(mem + b) ? "Yes" : "No");
            }
        }
        return 0;
    }

    练习建议:
    BZOJ2002 [Hnoi2010]Bounce 弹飞绵羊
    BZOJ2049 [Sdoi2008]Cave 洞穴勘测
    BZOJ3282 Tree

  • 相关阅读:
    antd vue 刷新保留当前页面路由,保留选中菜单,保留menu选中
    你不知道的arguments
    js触发复制、粘贴,设置和读取剪切板的数据
    vue父组件data改变触发子组件prop值变化
    关于英语的unless
    PHP闭包调用外部参数使用范例
    全国省市区数据库数据,以及可视化echarts相关geoJson数据
    PHP开发环境搭建&mdash;phpstudy+eclipse+php development tool+xdebug+composer
    LBS——实现附近功能的几种方案浅谈
    好文共赏
  • 原文地址:https://www.cnblogs.com/cocottt/p/5550960.html
Copyright © 2020-2023  润新知