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]];
}
LCT模板
最新推荐文章于 2022-11-02 11:23:29 发布