【热烈庆祝ZOJ回归】

【首先声明:LCT≠动态树,前者是一种数据结构,而后者是一类问题,即:LCT—解决—>动态树】

Link-cut-tree(下文统称LCT)是一种强大的数据结构,不仅可以像树链剖分一样对树上的两点进行询问(权值和、权值的最值……),还可以维护森林的连通性。

学习LCT首推杨哲神犇的《QTREE解法的一些研究》,很详细地解释了LCT的概念及实现

本文则以ZOJ2114一题为例,分析LCT实现过程中的一些事项,并且力求读者对LCT有一个“不次于‘感性’的认识”

叙述过程中会直接引用论文中的术语

题目大意:

N个点(N<=2e4),M次操作(M<=4e4),维护N个点构成的森林

最初N个点两两不连通

U x y z:新建一条权值为z的边,把x所在树的根节点连到y所在的树上

Q x y:询问x到y的距离。如果x和y未连通,输出Not connected.

思路:

对于Q操作,如果x和y所属的根节点不同,那么x和y必然是不连通的

对于U操作,首先需要找到x和y的根节点(分别记作u和v),然后把u连到v上

查询根节点可以用并查集

我们需要对森林进行修改(合并树)及询问操作,树链剖分是无法胜任的(树剖只能用于静态树),所以我们考虑LCT

我们用Splay维护Auxiliary Tree(如果不知道这是什么,请看论文):

 ,Right= };

 struct SplayNode;
 SplayNode* vir;

 struct SplayNode
 {
     int dist;
     int totDist;
     bool isRoot;
     SplayNode* parent;
     SplayNode* child[];
 };

(为阅读方便,分割代码时作了一些修改)

vir的作用是代替空指针,从而免去很多不必要的判断

SplayNode类的成员变量如下:

dist:在森林(原图)中,该节点与父节点之间边的权值。

totDist:Auxiliary Tree中,以该节点为根的子树的dist之和

isRoot:该节点是否为所在Auxiliary Tree的根节点

child[]:该节点在Auxiliary Tree中的左右孩子。

parent:既可以是Auxiliary Tree内部的父节点,也可以是Auxiliary Tree整体的path-parent

(1)如果isRoot==false,那么parent表示该节点在Auxiliary Tree中的父节点

(2)如果isRoot==true,那么parent表示其所在Auxiliary Tree的path-parent

也就是说,对于某个节点u,可能有多个节点以u为parent,但其中只有至多两个与u构成Auxiliary Tree。在编程实现中,这是一个值得注意的问题

如图,ABCDE构成森林中的一个连通分量,实线表示Preferred Child

相应的Auxiliary Tree(Series)。实线表示Preferred Child关系,虚线表示Path Parent关系

在Auxiliary Tree中,我们“隐式”地把节点的深度作为BST的关键字。也就是说,中序遍历一棵Auxiliary Tree,得到的就是一条从上到下的Preferred Path链

(如果你不能理解从图1到图2的转换,复习一下LCT的相关定义)

之后是Splay的一些操作(成员函数):

     void init()
     {
         dist=totDist=;
         isRoot=true;
         parent=child[]=child[]=vir;
     }

     void update()
     {
         this->totDist =
                 child[Left]->totDist+child[Right]->totDist+this->dist;
     }

     void rotate(int dir)
     //If dir==Right then rotate clockwise, else rotate counter-clockwise
     {
         if(parent==parent->parent->child[Left])
             parent->parent->child[Left]=this;
         else if(parent==parent->parent->child[Right])
             parent->parent->child[Right]=this;

         parent->child[dir^]=this->child[dir];
         child[dir]->parent=this->parent;

         child[dir]=parent;
         parent=parent->parent;
         child[dir]->parent=this;
     }

     void zigzig(int dir)
     {
         parent->rotate(dir);
         this->rotate(dir);

         child[dir]->child[dir]->update();
         child[dir]->update();
         this->update();
     }

     void zigzag(int dir) //dir depends on first rotation
     {
         rotate(dir);
         rotate(dir^);

         child[Left]->update();
         child[Right]->update();
         this->update();
     }

     void zig(int dir)
     {
         rotate(dir);
         child[dir]->update();
         this->update();
     }

     void splay()
     {
         while(!isRoot)
         {
             ;
             ;
             ;

             if(!parent->isRoot)
             {
                 if(parent->parent->isRoot) this->isRoot=true;
                 ;
                 ;
             }
             else
                 this->isRoot=true;

             switch(status)
             {
             : zig(Right);
                     child[Right]->isRoot=false;
                     break;
             : zig(Left);
                     child[Left]->isRoot=false;
                     break;
             : zigzig(Right);
                     if(isRoot) child[Right]->child[Right]->isRoot=false;
                     break;
             : zigzag(Left);
                     if(isRoot) child[Right]->isRoot=false;
                     break;
             : zigzag(Right);
                     if(isRoot) child[Left]->isRoot=false;
                     break;
             :zigzig(Left);
                     if(isRoot) child[Left]->child[Left]->isRoot=false;
                     break;
             default:break;
             }
         }
     }

各函数的说明:

init:初始化。令该节点成为独立的一个连通分量。

update:(在旋转以后)更新节点的totDist值。

rotate:将节点上旋。如果dir==Right,那么沿顺时针方向上旋;如果dir==Left,那么沿逆时针方向上旋。

zig,zigzig,zigzag:Splay的单双旋

splay:提根,将当前节点提到所在Auxiliary Tree的根部

         if(parent==parent->parent->child[Left])
             parent->parent->child[Left]=this;
         else if(parent==parent->parent->child[Right])
             parent->parent->child[Right]=this;

这里的判断需要特别注意。必须写成else if的形式(还记得我们是怎样定义parent的吗?)

另外splay的过程中涉及到isRoot的变化

除此之外其他的部分和普通的SplayTree大同小异,在这里就不多说了,详见代码。

至此,LCT的基础——SplayNode就已经打好了,接下来就是LCT的精华:Access(也有的版本称为Expose)操作:

 int access(SplayNode* u)
 {
     SplayNode* v=vir;
     ;
     while(u!=vir)
     {
         u->splay();
         u->child[Right]->isRoot=true;
         res=u->child[Right]->totDist;
         u->child[Right]=v;
         v->isRoot=false;
         u->update();
         v=u; u=u->parent;
     }
     return res + v->child[Right]->totDist;
 }

 inline int query(SplayNode* u,SplayNode* v)
 {
     access(u);
     return access(v);
 }

比起SplayNode的实现是不是短了许多?(斜眼笑

然而,正所谓浓缩就是精华,Access的原理可以说是不容小视的(毕竟是LCT所有操作的基石)

接下来我们以此图(以下称为原图)为例,演示access(H)的全过程。图为access(H)之前

access(H)之后的效果图,可以看到途经的所有节点,其原来的Preferred Path都被切断了,取而代之的是“直达”H的Preferred Path

access(H)之前的Auxiliary Tree,也是我们操作的对象。

首先我们splay(H),把H提到所在Auxiliary Tree的根部

此时H的右子树对应原图中的Preferred Path

由于新的Preferred Path要“切断”沿途所有旧的Preferred Child,而H的右子树对应H原来的Preferred Child

所以H和I之间要断开,表示新的Preferred Path到H就中止了,以I为根的子树成为一棵新的Auxiliary Tree

之后别忘了update一下H的totDist

为了将A和H用Preferred Path相连,我们还要对H的Path Parent,也就是对C“开刀”

首先还是splay(C)。当然C已经是Auxiliary Tree的根节点了,所以对于本例,splay(C)什么都没做

然后依然将C与右子树D断开。

之后,我们要将C的Auxiliary Tree与H的Auxiliary Tree相连。由于H的Auxiliary Tree在原图中处于C的下方,所以H应该成为C的新的右孩子

如图所示,A和E已经用Preferred Path相连了。至此access(H)过程已经完成。不妨把此时的Auxiliary Tree和上面的效果图对比一下

当然在编程时我们需要一个判断条件:C的parent为vir,再往上走就没有任何意义了,所以access操作停止

……

那么,access的返回值是干嘛的?totDist到底有什么卵用?

举个栗子:询问D到H的距离。我们已经进行了access(H)

我们给每条边赋予一个权值(图中用黑色标注)

但在LCT的Auxiliary Tree中,这个权值实际上是存储到“下方”的节点中的(图中以红色标注)。括号中是totDist,括号外是dist

然后我们access(D)

这是access(D)之后的Auxiliary Tree

在原图中,D到F的距离可以分解成D到C的距离和C到H的距离。C是D和F的最近公共祖先(LCA)

而在Auxiliary Tree中,这恰好对应C的新、旧右孩子的totDist之和

H是C的”旧“右孩子,被D取代后,D就成了C的”新“右孩子

在编程过程中,我们需要设法保存”旧“右孩子的totDist值(要不然就失联了( ̄▽ ̄")),access函数中的res变量恰好胜任了这一要求

query函数中进行了两次access操作,第一次的返回值因为没有意义所以被丢弃了,而第二次的返回值就是询问的结果

问题已经解决了一半,接下来就是两个子树的合并(并不难):

 ;
 int center[maxN];

 int getCenter(int x)
 {
     return center[x]==x ? x :
            center[x]=getCenter(center[x]);
 }

 inline void link(int u,int v,int len)
 {
     access(node+u);
     node[u].splay();
     center[u]=v;
     node[u].parent=node+v;
     node[u].dist=len;
     node[u].update();
 }

center就是并查集,合并之前,首先要用getCenter函数找到两棵树的根节点u和v,然后把u连到v上

首先access(u),然后splay(u)

splay(u)使u成为所在Auxiliary Tree的根节点,这样在连接u和v时,改变parent指针不会切断u所在的Auxiliary Tree

于是这道题用LCT完美解决(而且效率很高),附上完整代码:

 #include <cstdio>
 #include <cstring>
 #include <algorithm>

 ,Right= };

 struct SplayNode;
 SplayNode* vir;

 struct SplayNode
 {
     int dist;
     int totDist;
     bool isRoot;
     SplayNode* parent;
     SplayNode* child[];

     void init()
     {
         dist=totDist=;
         isRoot=true;
         parent=child[]=child[]=vir;
     }

     void update()
     {
         this->totDist =
                 child[Left]->totDist+child[Right]->totDist+this->dist;
     }

     void rotate(int dir)
     //If dir==Right then rotate clockwise, else rotate counter-clockwise
     {
         if(parent==parent->parent->child[Left])
             parent->parent->child[Left]=this;
         else if(parent==parent->parent->child[Right])
             parent->parent->child[Right]=this;

         parent->child[dir^]=this->child[dir];
         child[dir]->parent=this->parent;

         child[dir]=parent;
         parent=parent->parent;
         child[dir]->parent=this;
     }

     void zigzig(int dir)
     {
         parent->rotate(dir);
         this->rotate(dir);

         child[dir]->child[dir]->update();
         child[dir]->update();
         this->update();
     }

     void zigzag(int dir) //dir depends on first rotation
     {
         rotate(dir);
         rotate(dir^);

         child[Left]->update();
         child[Right]->update();
         this->update();
     }

     void zig(int dir)
     {
         rotate(dir);
         child[dir]->update();
         this->update();
     }

     void splay()
     {
         while(!isRoot)
         {
             ;
             ;
             ;

             if(!parent->isRoot)
             {
                 if(parent->parent->isRoot) this->isRoot=true;
                 ;
                 ;
             }
             else
                 this->isRoot=true;

             switch(status)
             {
             : zig(Right);
                     child[Right]->isRoot=false;
                     break;
             : zig(Left);
                     child[Left]->isRoot=false;
                     break;
             : zigzig(Right);
                     if(isRoot) child[Right]->child[Right]->isRoot=false;
                     break;
             : zigzag(Left);
                     if(isRoot) child[Right]->isRoot=false;
                     break;
             : zigzag(Right);
                     if(isRoot) child[Left]->isRoot=false;
                     break;
             :zigzig(Left);
                     if(isRoot) child[Left]->child[Left]->isRoot=false;
                     break;
             default:break;
             }
         }
     }
 };

 int access(SplayNode* u)
 {
     SplayNode* v=vir;
     ;
     while(u!=vir)
     {
         u->splay();
         u->child[Right]->isRoot=true;
         res=u->child[Right]->totDist;
         u->child[Right]=v;
         v->isRoot=false;
         u->update();
         v=u; u=u->parent;
     }
     return res + v->child[Right]->totDist;
 }

 inline int query(SplayNode* u,SplayNode* v)
 {
     access(u);
     return access(v);
 }

 void initVir()
 {
     vir=new SplayNode;
     vir->init();
 }

 ;

 SplayNode node[maxN];
 int center[maxN];
 int N,Q;
 ;

 inline void link(int u,int v,int len)
 {
     access(node+u);
     node[u].splay();
     center[u]=v;
     node[u].parent=node+v;
     node[u].dist=len;
     node[u].update();
 }

 inline void initNode()
 {
     ;i<=N;i++) node[i].init();
     ;i<=N;i++) center[i]=i;
 }

 int getCenter(int x)
 {
     return center[x]==x ? x :
            center[x]=getCenter(center[x]);
 }

 #define DEBUG
 #undef DEBUG

 #ifdef DEBUG
 ;
 #endif

 void solve()
 {
     lastRes=;
     scanf("%d%d",&N,&Q);
     initNode();
     char cmd;
     int v1,v2,len;
     while(Q--)
     {
         do cmd=getchar(); while(cmd==' ' || cmd=='\n');
         if(cmd=='Q')
         {
             scanf("%d%d",&v1,&v2);
             if(getCenter(v1)==getCenter(v2))
                 printf("%d\n",lastRes=query(node+v1,node+v2));
             else printf("Not connected.\n");
 #ifdef DEBUG
             printf("#%d query successful.\n",++qc);
 #endif
         }
         else
         {
             scanf("%d%d%d",&v1,&v2,&len);
 #ifndef DEBUG
             v1=(v1-lastRes-)%N;
             ) v1+=N;
             v2=(v2-lastRes-)%N;
             ) v2+=N;
 #endif
             link(getCenter(v1),getCenter(v2),len);
         }
     }
 }

 int main()
 {
     initVir();
     int X; scanf("%d",&X);
     while(X--) solve();
     ;
 }

Problem:ZOJ P2114

从ZOJ2114(Transportation Network)到Link-cut-tree(LCT)的更多相关文章

  1. 洛谷P3690 [模板] Link Cut Tree [LCT]

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

  2. BZOJ 3282 Link Cut Tree (LCT)

    题目大意:维护一个森林,支持边的断,连,修改某个点的权值,求树链所有点点权的异或和 洛谷P3690传送门 搞了一个下午终于明白了LCT的原理 #include <cstdio> #incl ...

  3. Luogu 3690 Link Cut Tree

    Luogu 3690 Link Cut Tree \(LCT\) 模板题.可以参考讲解和这份码风(个人认为)良好的代码. 注意用 \(set\) 来维护实际图中两点是否有直接连边,否则无脑 \(Lin ...

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

    A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...

  5. [CodeForces - 614A] A - Link/Cut Tree

    A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...

  6. B - Link/Cut Tree

    Problem description Programmer Rostislav got seriously interested in the Link/Cut Tree data structur ...

  7. 614A - Link/Cut Tree 数乘

    A. Link/Cut Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  8. link cut tree 入门

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

  9. Link/cut Tree

    Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...

  10. 洛谷P3690 Link Cut Tree (模板)

    Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...

随机推荐

  1. 对使命召唤OL游戏中队友能相互救治的动作设定的感慨

    很偶然的在网吧看到有人在玩一个枪战游戏,场景特别真实特别吸引人,后来留意到是使命召唤OL.我使用QQ帐号(是腾讯代理)玩了一次,觉得游戏做的确实精致,子弹打击效果和人物被子弹击中的效果特别真实,大家可 ...

  2. OJ题目分类

    POJ题目分类 | POJ题目分类 | HDU题目分类 | ZOJ题目分类 | SOJ题目分类 | HOJ题目分类 | FOJ题目分类 | 模拟题: POJ1006 POJ1008 POJ1013 P ...

  3. 如何实例化i2c_client(四法)

    一.在板文件进行client的实例化 在内核的初始化中(例如在板文件中)定义设备的信息.这种操作的前提是内核编译的时候已经确定有哪些i2c设备和它们的地址,还要知道连接的总线的编号. 比如在板文件/a ...

  4. Caffe 编译

    Compilation Now that you have the prerequisites, edit your Makefile.config to change the paths for y ...

  5. linux下avr单片机开发:中断服务程序

    不管是什么单片机程序,中断总是非常重要的一部分 ,linux 下的avr开发,主要是依靠avr-gcc,以及avr-libc,它们对中断程序的格式要求,与window下的icc-avr以及win-av ...

  6. Servlet(2)

    一.伪代码演示Tomcat的内部代码运行 1).通过映射找到servlet-class的内容,字符串:com.gqx.servlet.FirstServlet 2).通过反射构造构造FirstServ ...

  7. 王灏:光音网络致力打造Wi-Fi大生态圈

    光音网络,做的是本地网络综合服务.在中国,想把互联网做到覆盖延伸范围之外的最后100米,光音网络是当中一家,也是最坚持的一家.为千万家本地生活商户提供帮助,为数亿本地用户提供最佳的本地网络体验,这是光 ...

  8. Codeforces 114A-Cifera(暴力)

    A. Cifera time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...

  9. [D3] 6. Color Scale

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  10. PCAP 文件内容解析命令

    针对网络接口.端口和协议的数据包截取.假定你要截取网络接口eth1,端口号6881的tcp数据包.数据文件保存为test.pcap. tcpdump -w test.pcap -i eth1 tcp ...