#include<bits/stdc++.h>
using namespace std;
#define il inline void
#define ll long long
#define lc (tr[x].s[0])
#define rc (tr[x].s[1])
#define int unsigned int
const int N=200000;
struct ios {
inline char read(){
static const int IN_LEN=1<<18|1;
static char buf[IN_LEN],*s,*t;
return (s==t)&&(t=(s=buf)+fread(buf,1,IN_LEN,stdin)),s==t?-1:*s++;
}
template <typename _Tp> inline ios & operator >> (_Tp&x){
static char c11,boo;
for(c11=read(),boo=0;!isdigit(c11);c11=read()){
if(c11==-1)return *this;
boo|=c11=='-';
}
for(x=0;isdigit(c11);c11=read())x=x*10+(c11^'0');
boo&&(x=-x);
return *this;
}
}io;
template<class T>il _print(T x){
if(x/10) _print(x/10);
putchar(x%10+'0');
}
template<class T>il print(T x){
if(x<0) putchar('-'),x=-x;
_print(x);
}
struct Tr{
int s[2],p,rev;
}tr[N];
int n,q;
int stk[N];
inline void pushrev(int x)
{
swap(tr[x].s[0],tr[x].s[1]);
tr[x].rev^=1;
}
inline void pushdown(int x)
{
if(tr[x].rev)
{
pushrev(lc);
pushrev(rc);
tr[x].rev=0;
}
return;
}
inline bool isroot(int x)
{
return tr[tr[x].p].s[0]!=x&&tr[tr[x].p].s[1]!=x;
}
inline void rotato(int x)
{
int y=tr[x].p,z=tr[y].p;
int k=tr[y].s[1]==x;
if(!isroot(y))
tr[z].s[tr[z].s[1]==y]=x;
tr[x].p=z;
tr[y].s[k]=tr[x].s[k^1],tr[tr[x].s[k^1]].p=y;
tr[x].s[k^1]=y,tr[y].p=x;
return;
}
inline void splay(int x)
{
int top=0;
int r=x;
stk[++top]=r;
while(!isroot(r))
stk[++top]=r=tr[r].p;
while(top)
pushdown(stk[top--]);
while(!isroot(x))
{
int y=tr[x].p;
int z=tr[y].p;
if(!isroot(y)){
if((tr[y].s[1]==x)^(tr[z].s[1]==y))
rotato(x);
else rotato(y);
}
rotato(x);
}
}
inline void access(int x)
{
int z=x;
for(int y=0;x;y=x,x=tr[x].p)
{
splay(x);
tr[x].s[1]=y;
}
splay(z);
}
inline void makeroot(int x)
{
access(x);
pushrev(x);
}
inline int findroot(int x)
{
access(x);
while(tr[x].s[0])
pushdown(x),x=tr[x].s[0];
splay(x);
return x;
}
inline void split(int x,int y)
{
makeroot(x);
access(y);
}
inline void link(int x,int y)
{
makeroot(x);
if(findroot(y)!=x)
tr[x].p=y;
}
inline void cut(int x,int y)
{
makeroot(x);
if(findroot(y)==x&&tr[y].p==x&&!tr[y].s[0])
{
tr[x].s[1]=tr[y].p=0;
}
return;
}
signed main()
{
// freopen("P1501_2.in","r",stdin);
// freopen("rua.txt","w",stdout);
// double times=clock();
scanf("%d%d",&n,&q);
for(int i=1;i<=q;i++)
{
char str[7];
int a,b;
scanf("%s%d%d",str,&a,&b);
if(str[0]=='Q')
{
makeroot(a);
if(findroot(b)!=a)
puts("No");
else puts("Yes");
}
if(str[0]=='C')
{
link(a,b);
}
if(str[0]=='D')
{
cut(a,b);
}
}
// printf("%lf\n",(clock()-times)/1000);
return 0;
}
第10个点Wa 错误信息为
Wrong Answer. wrong answer Too long on line 37886.