LCT模板

39 篇文章 6 订阅
7 篇文章 0 订阅

讲解点击这里

int t[N][2],fa[N],rev[N],p[N],c1[N],d[N];
void down(int x)
{
    if(rev[x]==0) return;
    swap(t[x][0],t[x][1]);
    rev[t[x][0]]^=1;rev[t[x][1]]^=1;
    rev[x]=0;
}
bool lr(int x){return x==t[fa[x]][1];}
void rotate(int x)
{
    int y=fa[x],k=lr(x);
    t[y][k]=t[x][1-k];
    if(t[x][1-k]) fa[t[x][1-k]]=y;
    fa[x]=fa[y];
    if(fa[y]) t[fa[y]][lr(y)]=x;
    else p[x]=p[y],p[y]=0;
    t[x][1-k]=y;fa[y]=x;
    updata(y);updata(x);
}
void xc(int x,int y)
{
    do
    {
        d[++d[0]]=x;
        x=fa[x];
    } while(x!=y);
    for(;d[0];d[0]--) down(d[d[0]]);
}
void splay(int x,int y)
{
    xc(x,y);
    while(fa[x]!=y)
    {
        if(fa[fa[x]]!=y)
            if(lr(x)==lr(fa[x])) rotate(fa[x]);
            else rotate(x);
        rotate(x);
    }
}
void access(int x)
{
    int y=0;
    while(x>0)
    {
        splay(x,0);
        fa[t[x][1]]=0,p[t[x][1]]=x;
        t[x][1]=y,fa[y]=x;
        p[y]=0;
        updata(x);
        y=x,x=p[x];
    }
}
void makeroot(int x)
{
    access(x);splay(x,0);rev[x]^=1;
}
void link(int x,int y)
{
    makeroot(x);p[x]=y;
}
void cut(int x,int y)
{
    makeroot(x);access(y);
    splay(y,0);
    t[y][0]=0;fa[x]=p[x]=0;
    updata(y);
}
int find(int x,int y)
{
    makeroot(x);access(y);
    splay(x,0);splay(y,x);
    return c1[t[y][0]];
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值