模板—LCT
2024-09-06 14:52:47
#include<iostream>
#include<cstring>
#include<cstdio>
#define LL long long
using namespace std;
int f[],c[][],st[];
bool r[];
#define l(x) c[x][0]
#define r(x) c[x][1]
inline bool nroot(int x){return c[f[x]][]==x||c[f[x]][]==x;}
void pushup(int x){;}
void pushr(int x){int t=l(x);l(x)=r(x);r(x)=t;r[x]^=;}
void pushdown(int x)
{
if(!r[x])return;
if(l(x))pushr(l(x));
if(r(x))pushr(r(x));
r[x]=;
}
void rotate(int x)//
{
int y=f[x],z=f[y],k=r(y)==x,w=c[x][!k];
if(nroot(y))c[z][r(z)==y]=x;c[x][!k]=y;c[y][k]=w;
if(w)f[w]=y;f[y]=x;f[x]=z;
pushup(y);
}
void splay(int x)//
{
int y=x,z=;st[++z]=y;
while(nroot(y))st[++z]=y=f[y];
while(z)pushdown(st[z--]);
while(nroot(x))
{
y=f[x];z=f[y];
if(nroot(y))rotate((l(y)==x)^(l(z)==y)?x:y);
rotate(x);
}
pushup(x);
}
void access(int x){for(int y=;x;x=f[y=x])splay(x),r(x)=y,pushup(x);}//
void makeroot(int x){access(x);splay(x);pushr(x);}//
int findroot(int x){access(x);splay(x);while(l(x))pushdown(x),x=l(x);splay(x);return x;}//
void split(int x,int y){makeroot(x);access(y);splay(y);}//
void link(int x,int y){makeroot(x);if(findroot(y)!=x)f[x]=y;}//
void cut(int x,int y)
{
makeroot(x);
if(findroot(y)==x&&f[y]==x&&!l(y))
f[y]=r(x)=,pushup(x);
}
int n,m;
signed main()
{
cin>>n>>m;
char opt[];int u,v;
for(int i=;i<=m;i++)
{
cin>>opt;
if(opt[]=='C')cin>>u>>v,link(u,v);
if(opt[]=='D')cin>>u>>v,cut(u,v);
if(opt[]=='Q')
{
cin>>u>>v;
if(findroot(u)==findroot(v))puts("Yes");
else puts("No");
}
}
}
最新文章
- 1Z0-053 争议题目解析481
- String split
- php版本引起的const问题
- Redis 存储原理
- android Service Activity交互之传递复杂数据类型的远程服务
- NENU_CS_segment_tree
- java.util.TreeSet源码分析
- DevExpress的 ASPxGridview控件的自动配置效果
- C#实现MySQL数据库中的blob数据存储
- 字符编码终极笔记:ASCII、Unicode、UTF-8、UTF-16、UCS、BOM、Endian
- Hashtable 小记
- python+flask 分分钟完美解析阿里云日志
- vue父子组件生命周期执行顺序
- 2072. Kirill the Gardener 3
- [android] 表格布局和绝对布局
- TXB0108 TXS0108E 8-Bit Bidirectional Voltage-Level Translator for Open-Drain and Push-Pull Applications
- vsm安装
- 移动设备&#160;小米2S不显示CD驱动器(H),便携设备,MTP,驱动USB&#160;Driver,MI2感叹号的解决方法
- eclipse启动一闪而退
- jquery 判断checkbox状态