LCT模板(link-cut-tree)

#include<stdio.h>  
#include<algorithm>  
#define N 200005  
using namespace std;  
//struct stp{int x,y,a,b;}a[N];  
//bool cmp(stp a,stp b){return a.a<b.a;}  
int rev[N],mx[N],ch[N][2],V[N],pre[N],fa[N],st[N],t,size[N],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);make_root(y);pre[x]=y;}//将x的父亲设为y   
void cut(int x,int y){make_root(x);access(y);splay(y);pre[x]=ch[y][0]=0;}  
  
  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值