Link Cut Tree
zhoukangyang · · 个人记录
\tt Link \ \ Cut\ \ Tree 详解
1. 性质
该操作就是把节点
怎么做呢?
- 把当前节点转到
\tt Splay 的根 - 把左儿子更换为上一个
\tt Splay 的根 (把左子树变成用轻边连接,这时此节点为所在链的最底端,把上一个链变成用重边连接即可,就是把左子树换成上一个\tt Splay ) - 更新数据
- 跳到当前点的父亲
void access(int now) {
int y = 0;
while (now) {
Splay(now);
ch[now][1] = y;
upd(now);
y = now;
now = fa[now];
}
}
2.\tt makeroot(x)
该操作就是把节点
- 把这个节点到根变成重边(此时这个节点所在的
\tt Splay 只剩下了该节点到根的节点) - 把此节点转到当前
Splay 的根(方便打标记) - 在这个节点上打翻转标记(为了让该节点变成根节点,让链的深度翻转,就是让
Splay 的所有节点的左右子树翻转)
void filp(int now) { //翻转操作
if(now!=0) swap(ch[now][0],ch[now][1]),mer_[now]^=1;
}
void pushdown(int now) { //标记下传
if(!mer_[now]) return;
mer_[now]^=1,filp(ch[now][0]),filp(ch[now][1]);
}
void makeroot(int now) {
access(now),Splay(now),filp(now);
}
3.\tt findroot(x)
该操作就是找到当前树的根
找到树根就是找到深度最小的节点,只要从
int findroot(int now) {
access(now),Splay(now);
while(ch[now][0]) {
pushdown(now);
now = ch[now][0];
}
Splay(now);
return now;
}
4.\tt split(x,y)
就是把
只要把
void split(int x,int y) {
makeroot(x),access(y),Splay(y);
}
5.\tt link(x,y)
在
判断联通,然后把
void split(int x,int y) {
makeroot(x),access(y),Splay(y);
}
6.\tt cut(x,y)
在
判断联通,然后把
void cut(int x,int y){//断边
makeroot(x);
if(findroot(y)==x&&fa[y]==x&&!ch[y][0]){
fa[y]=ch[x][1]=0;
upd(x);
}
}
\tt AC 代码
#include<bits/stdc++.h>
#define N 410000
using namespace std;
int s[N],v[N],ch[N][2],fa[N],mer_[N];
bool get(int now) {
return ch[fa[now]][0]==now||ch[fa[now]][1]==now;
}
void filp(int now) {
if(now!=0) swap(ch[now][0],ch[now][1]),mer_[now]^=1;
}
void pushdown(int now) {
if(!mer_[now]) return;
mer_[now]^=1,filp(ch[now][0]),filp(ch[now][1]);
}
void upd(int now) {
s[now]=s[ch[now][0]]^s[ch[now][1]]^v[now];
}
void rotate(int now) {
int fath=fa[now],ffa=fa[fath],flag=(ch[fath][1]==now),ano=ch[now][1^flag];
if(get(fath)) ch[ffa][ch[ffa][1]==fath] = now;
if(ano) fa[ano]=fath;
ch[now][1^flag] = fath , ch [fath][flag] = ano , fa[fath] = now , fa[now] = ffa;
upd(fath),upd(now);
}
int tot,lnow,stac[N];
void Splay(int now) {
lnow = now, tot = 1, stac[1] = now;
while(get(lnow)) ++tot,stac[tot]=lnow=fa[lnow];
while(tot != 0) pushdown(stac[tot]),--tot;
while(get(now)) {
lnow=fa[now],tot=fa[lnow];
if(get(lnow)) rotate((ch[lnow][0]==now)^(ch[tot][0]==lnow)?now:lnow);
rotate(now);
}
upd(now);
}
void access(int now) {
int y = 0;
while (now) {
Splay(now);
ch[now][1] = y;
upd(now);
y = now;
now = fa[now];
}
}
void makeroot(int now) {
access(now),Splay(now),filp(now);
}
int findroot(int now) {
access(now),Splay(now);
while(ch[now][0]) {
pushdown(now);
now = ch[now][0];
}
Splay(now);
return now;
}
void split(int x,int y) {
makeroot(x),access(y),Splay(y);
}
void link(int x,int y) {
makeroot(x);
if(findroot(y) != x) fa[x]=y;
}
void cut(int x,int y){//断边
makeroot(x);
if(findroot(y)==x&&fa[y]==x&&!ch[y][0]){
fa[y]=ch[x][1]=0;
upd(x);
}
}
int n,m,xx,yy,opt;
int main() {
scanf("%d%d",&n,&m);
for(int i = 1; i <= n ; i++) scanf("%d",&v[i]);
while(m--) {
scanf("%d%d%d",&opt,&xx,&yy);
if(opt==0) split(xx,yy),printf("%d\n",s[yy]);
if(opt==1) link(xx,yy);
if(opt==2) cut(xx,yy);
if(opt==3) Splay(xx),v[xx]=yy,upd(xx);
}
return 0;
}