您的位置:首页 > 其它

LCT模板(link-cut-tree)

2017-08-02 13:47 429 查看
#include<stdio.h>
#include<algorithm>
#define N 200005
using namespace std;
//struct stp{int x,y,a,b;}a
;
//bool cmp(stp a,stp b){return a.a<b.a;}
int rev
,mx
,ch
[2],V
,pre
,fa
,st
,t,size
,n;
//splay中数组:rev反转标记 mx节点最小值 ch树的儿子节点
//V节点value pre节点的父节点 st存节点的栈
//其他数组:fa
int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}//并查集
bool is_root(int x){return ch[pre[x]][0]!=x&&ch[pre[x]][1]!=x;}
//判断x是否为其中一颗splay的根节点
void reverse(int x)
{
rev[x]^=1;swap(ch[x][0],ch[x][1]);
rev[ch[x][0]]^=1;rev[ch[x][1]]^=1;
}
void update(int x)
{
mx[x]=x;
if(V[mx[ch[x][0]]]>V[mx[x]])mx[x]=mx[ch[x][0]];
if(V[mx[ch[x][1]]]>V[mx[x]])mx[x]=mx[ch[x][1]];
size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
}
void rotate(int x)
{
int y=pre[x],z=pre[y];
bool f=ch[y][1]==x;
if(!is_root(y))ch[z][ch[z][1]==y]=x;
ch[y][f]=ch[x][!f];ch[x][!f]=y;
pre[x]=z;pre[y]=x;pre[ch[y][f]]=y;
update(y);update(x);
}
void splay(int x)
{
st[t=1]=x;
for(int y=x;!is_root(y);st[++t]=y=pre[y]);
for(;t;t--)if(rev[st[t]])reverse(st[t]);
for(;!is_root(x);rotate(x))if(!is_root(pre[x]))
ch[pre[x]][0]==x^ch[pre[pre[x]]][0]==pre[x]?rotate(x):rotate(pre[x]);
update(x);
}
void access(int x){for(int t=0;x;ch[x][1]=t,t=x,x=pre[x]){if(t)update(t);splay(x);}}
void make_root(int x){access(x);splay(x);rev[x]^=1;}
void link(int x,int y){make_root(x);pre[x]=y;}//将x的父亲设为y
void cut(int x,int y){make_root(x);access(y);splay(y);pre[x]=ch[y][0]=0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: