【刷题】BZOJ 2049 [Sdoi2008]Cave 洞穴勘测

200 5
Query 123 127
Connect 123 127
Query 123 127
Destroy 127 123
Query 123 127

3 5
Connect 1 2
Connect 3 1
Query 2 3
Destroy 1 3
Query 2 3

No
Yes
No

Yes
No

Solution

#include<bits/stdc++.h>
#define ll long long
#define db double
#define ld long double
#define lc(x) ch[(x)][0]
#define rc(x) ch[(x)][1]
const int MAXN=10000+10;
int n,m;
struct LCT{
int ch[MAXN][2],fa[MAXN],rev[MAXN];
inline void init()
{
memset(ch,0,sizeof(ch));
memset(fa,0,sizeof(fa));
memset(rev,0,sizeof(rev));
}
inline bool nroot(int x)
{
return lc(fa[x])==x||rc(fa[x])==x;
}
inline void reverse(int x)
{
std::swap(lc(x),rc(x));
rev[x]^=1;
}
inline void pushdown(int x)
{
if(rev[x])
{
if(lc(x))reverse(lc(x));
if(rc(x))reverse(rc(x));
rev[x]=0;
}
}
inline void rotate(int x)
{
int f=fa[x],p=fa[f],c=(rc(f)==x);
if(nroot(f))ch[p][rc(p)==f]=x;
fa[ch[f][c]=ch[x][c^1]]=f;
fa[ch[x][c^1]=f]=x;
fa[x]=p;
}
inline void splay(int x)
{
std::stack<int> s;
s.push(x);
for(register int i=x;nroot(i);i=fa[i])s.push(fa[i]);
while(!s.empty())pushdown(s.top()),s.pop();
for(register int y=fa[x];nroot(x);rotate(x),y=fa[x])
if(nroot(y))rotate((lc(y)==x)==(lc(fa[y])==y)?y:x);
}
inline void access(int x)
{
for(register int y=0;x;x=fa[y=x])splay(x),rc(x)=y;
}
inline void makeroot(int x)
{
access(x);splay(x);reverse(x);
}
inline int findroot(int x)
{
access(x);splay(x);
while(lc(x))pushdown(x),x=lc(x);
splay(x);
return x;
}
inline void split(int x,int y)
{
makeroot(x);access(y);splay(y);
}
{
makeroot(x);
if(findroot(y)!=x)fa[x]=y;
}
inline void cut(int x,int y)
{
makeroot(x);
if(findroot(y)==x&&fa[y]==x&&!lc(y))fa[y]=rc(x)=0;
}
};
LCT T;
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char c='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(c!='\0')putchar(c);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
int main()
{
T.init();
while(m--)
{
char opt[20];
scanf("%s",opt);
if(!strcmp(opt,"Query"))
{
int x,y;
if(T.findroot(y)==T.findroot(x))puts("Yes");
else puts("No");
}
if(!strcmp(opt,"Connect"))
{
int x,y;
}