struct LCT {
typedef int arr[N];
arr fa,ch[2],rev;
#define lc ch[0][x]
#define rc ch[1][x]
void upd(int x) {...}
bool son(int x) {return ch[1][fa[x]] == x;}
bool nroot(int x) {return ch[son(x)][fa[x]] == x;}
void rotate(int x) {
int y=fa[x],z=fa[y],c=son(x);
ch[c][y]=ch[c^1][x]; if(ch[c][y]) fa[ch[c][y]]=y;
if(nroot(y)) ch[son(y)][z]=x; fa[x]=z;
ch[c^1][x]=y; fa[y]=x; upd(y); upd(x);
}
void reverse(int x) { if(x) swap(lc,rc),rev[x] ^= 1;}
void pushdown(int x) { if(rev[x]) reverse(lc),reverse(rc),rev[x]=0;}
void dfs(int x) {if(nroot(x)) dfs(fa[x]); pushdown(x);}
void splay(int x) {
dfs(x);
for(int y=fa[x]; nroot(x); rotate(x),y=fa[x])
if(nroot(y)) rotate(son(x)^son(y)?x:y);
}
void access(int x) {for(int y=0; x; x=fa[y=x]) splay(x), rc=y, upd(x);}
void makeroot(int x) {access(x); splay(x); reverse(x);}
int findroot(int x) { access(x); splay(x); while(lc) x=lc,pushdown(x); return splay(x),x;}
void link(int x,int y) {makeroot(x); if(findroot(y)^x) fa[x]=y; access(x);}
void cut(int x,int y) { makeroot(x); if(findroot(y) == x && fa[y] == x && !ch[0][y]) {rc=fa[y]=0; upd(x); } }
void split(int x,int y) {makeroot(y); access(x); splay(x);}
} s;
03-05
07-20
07-20
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交