萌新刚学OI!LCT求调!
查看原帖
萌新刚学OI!LCT求调!
682028
_awa_keyai楼主2023/7/13 22:04
#include<bits/stdc++.h>

using namespace std;

#define int long long

inline long long read() {
    long long ret=0,f=1;
    char c=getchar();
    for(; c<'0'||c>'9'; c=getchar()) if(c=='-') f=-f;
    for(; c>='0'&&c<='9'; c=getchar()) ret=ret*10+c-'0';
    return ret*f;
}

int n,t;
#define maxn 10010

struct _tree{
	int son[2];
	int f,fl;
}a[maxn];

inline int isroot(int rt){
	return a[a[rt].f].son[0]!=rt&&a[a[rt].f].son[1]!=rt;
}

inline void pushdown(int rt){
	if(!rt||!a[rt].fl) return;
	a[a[rt].son[0]].fl^=1;
	a[a[rt].son[1]].fl^=1;
	a[rt].fl^=1;
	swap(a[rt].son[0],a[rt].son[1]);
}

inline void turn(int rt){
	int x=a[rt].f,y=a[x].f,k=a[x].son[0]==rt?0:1;
//	swap(a[rt].son[0],a[rt].son[1]);
	if(!isroot(x)){
		if(a[y].son[0]==x){
			a[y].son[0]=rt;
		} else {
			a[y].son[1]=rt;
		} 
	}
	a[rt].f=y;a[x].f=rt;
	a[a[rt].son[!k]].f=x;
	a[x].son[k]=a[rt].son[!k];a[rt].son[!k]=x;
}

void splay(int rt){
	int top=0,stack[maxn];
	stack[++top]=rt;
	for(int i=rt;!isroot(i);i=a[i].f){
		stack[++top]=a[i].f;
	}
	
	while(top) pushdown(stack[top--]);
	
	while(!isroot(rt)){
		int x=a[rt].f,y=a[x].f;
		if(!isroot(x)){
			if((a[x].son[0]==rt)^(a[y].son[0]==x)) turn(rt);
			else turn(x);
		}
		turn(rt);
	}
}

void access(int rt){
	for(int i=0;rt;i=rt,rt=a[rt].f){
		splay(rt);
		a[rt].son[1]=i;
	}
}

inline void makeroot(int rt){
	access(rt);splay(rt);a[rt].fl^=1;
}

int _find(int rt){
	access(rt);splay(rt);
	while(a[rt].son[0]) rt=a[rt].son[0];
	return rt;
}

inline void split(int x,int y){
	makeroot(x);
	access(y);
	splay(y);
}

inline void cut(int x,int y){
	split(x,y);
	a[y].son[0]=a[x].f=0;
}

inline void link(int x,int y){
	makeroot(x);
	a[x].f=y;
}

signed main(void){
    
    char ch[224];
    int n,t,x,y;
//    cin>>n>>t;
	n=read();t=read();
    while(t--){
    	scanf("%s",ch);
    	if(ch[0]=='C') {
    		link(x,y);
		}
		
		if(ch[0]=='D'){
			cut(x,y);
		} 
		
		if(ch[0]=='Q'){
			if(_find(x)==_find(y)){
				printf("Yes\n");
			} else {
				printf("No\n");
			} 
		} 
	}
    
    return 0;
}
2023/7/13 22:04
加载中...