萌新刚学lct 1ms 求助大佬
查看原帖
萌新刚学lct 1ms 求助大佬
246800
wocaicai楼主2020/6/15 22:21

rt 本蒟蒻第一次写lct,学完之后基本是对着题解写的,唯有rotate这一步是自己写的(来自文艺平衡树的rotate)样例已经过了 , 但是交上去却是re 为什么呢?????

代码如下

#include<bits/stdc++.h>
using namespace std ;
#define maxn 10009
#define int long long
#define kkk signed main
int read(){
	int ans = 0 , f = 1 ; char ch = getchar() ;
	while ( ch < '0' || ch > '9') {	 if( ch == '-' )  f = -1 ; ch = getchar() ; }
	while ( ch >= '0' && ch <= '9' ) ans =  ( ans * 10 ) + ch - '0' , ch = getchar() ;
	return ans * f ;
}
inline void Swap(int &a , int &b){
	a^=b^=a^=b ;  
}
struct lct{
	int c[maxn][2] , fa[maxn] , top , q[maxn] , rev[maxn] ; 
	inline void pushdown(int x){
		if(rev[x]){
			int l = c[x][0] , r = c[x][1] ; 
			rev[l] ^= 1 , rev[r] ^= 1 , rev[x] ^= 1 ; 
			Swap(c[x][0] , c[x][1]) ; 
		}
	} 
	inline bool isroot(int x){
		return c[fa[x]][0] != x && c[fa[x]][1] != x ; 
	}
	//这是我的 
	inline void rotate(int x){
		int f = fa[x]  ; int  g = fa[f] ; 
		bool k = (x == c[f][1]) ;  
		fa[f] = x ;  fa[x] = g;
		fa[c[x][k ^ 1]] = f ; 
		if(!isroot(f)) c[g][(c[g][1] == f)] = x ;
		c[f][k] = c[x][k ^ 1] ; 
		c[x][k ^  1] = f ;  
	}
	//以下是题解的rotate 
	/*inline void rotate(register int x)
    {
        int y=fa[x],z=fa[y],l,r;
        l=c[y][0]==x?0:1;
        r=l^1;
        if(!isroot(y))
            c[z][c[z][0]==y?0:1]=x;
        fa[x]=z;
        fa[y]=x;
        fa[c[x][r]]=y;
        c[y][l]=c[x][r];
        c[x][r]=y;
    }*/
	inline void splay(int x){
		top = 1 ; 
		q[top] = x ; 
		for(int i = x ; !isroot(i) ; i = fa[i]){
			q[++top] = fa[i] ; 
		}
		for(int i = top ; i ; --i ){
			pushdown(q[i]) ; 
		}
		while(!isroot(x)){
			int y = fa[x] , z = fa[y] ; 
			if(!isroot(y)){
				rotate((c[y][0] == x) ^ (c[z][0] == y) ? (x) : (y)) ;
			}
			rotate(x) ; 
		}
	}
	inline void access(int x){
		for(int  t = 0 ; x ; t = x , x = fa[x]){
			splay(x) ; 
			c[x][1] = t ; 
		}
	}
	inline void makeroot(int x){
		access(x) ; splay(x); 
		rev[x] ^= 1 ; 
	} 
	inline int findroot(int x){
		access(x) ; splay(x) ; 
		while(c[x][0])
			x = c[x][0] ; 
		return  x ; 
	}
	inline void split(int x,  int y){
		makeroot(x) ; access(y) ; splay(y) ; 
	}
	inline void cut(int x, int y){
		split(x , y) ; 
		c[y][0] = 0 ; fa[x] = 0 ; 
	}
	inline void link(int x , int y){
		makeroot(x) ; 
		fa[x] = y ; 
	}
}T;
int n , m ; 
kkk(){
	n = read() , m = read() ;
	char ch[10] ; 
	while(m--){
		cin >> ch ; 
		if(ch[0]=='C'){
			int x = read() , y = read() ;
			T.link(x ,y) ;  
		}
		else if(ch[0] == 'D'){
			int x = read() , y = read() ; 
			T.cut(x , y) ;  
		}
		else {
			int x = read() , y = read() ; 
			puts(T.findroot(x) == T.findroot(y)?"Yes" : "No" ) ; 
		}
	}
	return 0 ;
}

2020/6/15 22:21
加载中...