码迷,mamicode.com
首页 > 其他好文 > 详细

Link Cut Tree 动态树 小结

时间:2018-09-25 01:06:19      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:roo   理解   tchar   turn   str   并查集   inline   names   namespace   

动态树有些类似 树链剖分+并查集 的思想,是用splay维护的

lct的根是动态的,"轻重链"也是动态的,所以并没有真正的轻重链

动态树的操作核心是把你要把 修改/询问/... 等等一系列的操作的树链放到一个splay里,然后用splay根据相对深度大小来维护这个树链

lct利用了splay的神奇性质,通过"认爹不认子"来达到记录多个子树的目的

lct的核心,access函数的意义是,在从这个点到它所在联通块中 相对深度最小的点 (可以理解为子树根) 的树链上,打通树链上每个点所在的splay树,并把需要的那部分树链放到一个splay树里,这样,这颗树链上的所有点就都在同一个splay里了,而且这颗splay里只存了这个树链而没有其他的点

由于根是动态的,所以有了makeroot函数,意义是把x点作为树根,先用access把需要的一部分树链取出,然后splay到根,发现x点原来是这条树链深度最大的点,现在深度变成了最小,用splay维护一下区间翻转即可

还有很多细节需要注意,洛谷上神犇的讲解很详细

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 #define root d[0].ch[1]
 5 #define il inline
 6 #define nu 7777
 7 #define inf 500000
 8 #define N 300100
 9 using namespace std;
10 
11 int n,m,tp;
12 int stk[N];
13 char str[10];
14 struct Link_Cut_Tree{
15     int fa[N],ch[N][2],sum[N],val[N],rev[N];
16     il int idf(int x){return ch[fa[x]][0]==x?0:1;}
17     il int isroot(int x){return (ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x)?1:0;}
18     il void con(int x,int ff,int p){fa[x]=ff;ch[ff][p]=x;}
19     il void pushup(int x){sum[x]=sum[ch[x][0]]^sum[ch[x][1]]^val[x];}
20     il void revers(int x){swap(ch[x][0],ch[x][1]),rev[x]^=1;}
21     il void pushdown(int x){
22         if(rev[x]){ 
23             if(ch[x][0]) revers(ch[x][0]);
24             if(ch[x][1]) revers(ch[x][1]);
25             rev[x]=0;}}
26     il void rot(int x){
27         int y=fa[x];int ff=fa[y];int px=idf(x);int py=idf(y);
28         if(!isroot(y)) ch[ff][py]=x;
29         fa[x]=ff,con(ch[x][px^1],y,px),con(y,x,px^1);
30         pushup(y),pushup(x);}
31     void splay(int x){
32         int y=x;stk[++tp]=x;
33         while(!isroot(y)){stk[++tp]=fa[y];y=fa[y];}
34         while(tp){pushdown(stk[tp--]);}
35         while(!isroot(x)){
36             y=fa[x];
37             if(isroot(y)) rot(x);
38             else if(idf(y)==idf(x)) rot(y),rot(x);
39             else rot(x),rot(x);
40         }}
41     il void access(int x){for(int y=0;x;y=x,x=fa[x])splay(x),ch[x][1]=y,pushup(x);}
42     il void mkroot(int x){access(x),splay(x),revers(x);}
43     il int findrt(int x){
44         access(x),splay(x);
45         while(ch[x][0])pushdown(x),x=ch[x][0];
46         return x;}
47     il void split(int x,int y){mkroot(x),access(y),splay(y);} 
48     il void link(int x,int y){mkroot(x);if(findrt(y)!=x)fa[x]=y;}
49     il void cut(int x,int y){
50         mkroot(x);
51         if(findrt(y)==x&&fa[x]==y&&!ch[x][1])
52             fa[x]=ch[y][0]=0,pushup(y);}
53 }lct;
54 
55 int gc()
56 {
57     int rett=0,fh=1;char c=getchar();
58     while(c<0||c>9){if(c==-)fh=-1;c=getchar();}
59     while(c>=0&&c<=9){rett=(rett<<3)+(rett<<1)+c-0;c=getchar();}
60     return rett*fh;
61 }
62 
63 int main()
64 {
65     n=gc(),m=gc();
66     for(int i=1;i<=n;i++) lct.val[i]=gc();
67     int fl,x,y;
68     for(int i=1;i<=m;i++)
69     {
70         fl=gc(),x=gc(),y=gc();
71         if(fl==0) lct.split(x,y),printf("%d\n",lct.sum[y]);
72         if(fl==1) lct.link(x,y);
73         if(fl==2) lct.cut(x,y);
74         if(fl==3) lct.splay(x),lct.val[x]=y,lct.pushup(x);
75     }
76     return 0;
77 }

 

Link Cut Tree 动态树 小结

标签:roo   理解   tchar   turn   str   并查集   inline   names   namespace   

原文地址:https://www.cnblogs.com/guapisolo/p/9697081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!