Link-Cut Tree动态树模板
生活随笔
收集整理的這篇文章主要介紹了
Link-Cut Tree动态树模板
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
講解博客:https://www.cnblogs.com/zhoushuyu/p/8137553.html?
struct Link_Cut_Tree {int fa[N],ch[N][2],sum[N],rev[N],Stack[N],top;bool son(int x)//判斷點x是父節點的左兒子還是右兒子 {return x==ch[fa[x]][1];}bool isroot(int x)//判斷一個點是不是根節點(當前splay的根節點){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}void pushup(int x)//上傳(維護信息){sum[x]=sum[ch[x][0]]^sum[ch[x][1]]^val[x];}void reverse(int x)//交換左右子樹 {if(!x)return;swap(ch[x][0],ch[x][1]);rev[x]^=1;}void pushdown(int x)//下傳(更新反轉標記用){if(!rev[x])return;reverse(ch[x][0]);reverse(ch[x][1]);rev[x]=0;}void rotate(int x)//splay的旋轉 {int y=fa[x],z=fa[y],c=son(x);ch[y][c]=ch[x][c^1];if(ch[y][c]) fa[ch[y][c]]=y;fa[x]=z;if(!isroot(y))ch[z][son(y)]=x;ch[x][c^1]=y;fa[y]=x;pushup(y);}void splay(int x)//將x轉到根節點 {Stack[top=1]=x;for (int i=x;!isroot(i);i=fa[i])Stack[++top]=fa[i];while (top) pushdown(Stack[top--]);for (int y=fa[x];!isroot(x);rotate(x),y=fa[x])if (!isroot(y)) son(x)^son(y)?rotate(x):rotate(y);pushup(x);}void access(int x)//把x節點到x所在樹(連通塊)的根節點之間的路徑全部變成重路徑{for (int y=0;x;y=x,x=fa[x])splay(x),ch[x][1]=y,pushup(x);}void makeroot(int x)//把x節點設為x所在樹(連通塊)的根節點{access(x);splay(x);reverse(x);}int findroot(int x)//找到x節點所在樹(連通塊)的根節點{access(x);splay(x);while (ch[x][0]) x=ch[x][0];splay(x);return x;}void split(int x,int y)//摳出x到y的路徑,摳完以后y是splay的根{makeroot(x);access(y);splay(y);}void cut(int x,int y)//砍斷x到y的邊{makeroot(x);if(findroot(y)==x&&fa[y]==x&&!ch[y][0])fa[y]=ch[x][1]=0,pushup(x);}void link(int x,int y)//連接x到y的邊{makeroot(x);if(findroot(y)!=x)fa[x]=y;} }t;?
總結
以上是生活随笔為你收集整理的Link-Cut Tree动态树模板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 洛谷 - P3975 [TJOI2015
- 下一篇: 洛谷 - P3690 【模板】Link