ztx
[POJ3237]树的维护
[国家集训队2012]tree(伍一鸣)

[SDOI2008]Cave 洞穴勘测

ztx posted @ 2015年1月17日 17:26 in 动态树 with tags LCT , 485 阅读
/****************************************\
* Author : ztx
* Title  : [SDOI2008]Cave 洞穴勘测
* ALG    : LCT
* CMT    : 判断连通性
* Time   :
\****************************************/
/****************************************\
* Author : ztx
* Title  : [SDOI2008]Cave 洞穴勘测
* ALG    : LCT
* CMT    : 判断连通性
* Time   :
\****************************************/

#include <cstdio>

int CH , NEG ;
inline int GETCHAR() {
	static const int LEN = 1<<15 ;
	static char BUF[LEN] , *S = BUF , *T = BUF ;
	if (S == T) {
		T = (S=BUF)+fread(BUF , 1 , LEN , stdin) ;
		if (S == T) return EOF ;
	}
	return *S ++ ;
}

inline void read(int& ret) {
    ret = NEG = 0 ; while (CH=GETCHAR() , CH<'!') ;
    if (CH == '-') NEG = true , CH = GETCHAR() ;
    while (ret = ret*10+CH-'0' , CH=GETCHAR() , CH>'!') ;
    if (NEG) ret = -ret ;
}

inline void reads(int& ret) {
	while (ret=GETCHAR() , ret<'!') ;
	while (CH=GETCHAR() , CH>'!') ;
}

#define  maxn  10010LL

int fa[maxn] = {0} , ch[maxn][2] = {0} ;
bool rev[maxn] = {0} ;

#define  null     0LL
#define  left(u)  ch[u][0]
#define  right(u) ch[u][1]

inline void Exchange(int& a , int& b) { int c = a ; a = b ; b = c ; }

inline bool Isrt(int u) { return (fa[u]==null)||(left(fa[u])!=u&&right(fa[u])!=u) ; }

inline void Clear(int u) {
	if (!u) return ;
	if (!Isrt(u)) Clear(fa[u]) ;
	if (rev[u]) {
		rev[u] = false ;
		rev[left(u)] ^= true ;
		rev[right(u)] ^= true ;
		Exchange(left(u) , right(u)) ;
	}
}

inline void zig(int x) {
    /* right rotate */
    int y = fa[x] , z = fa[y] ;
    if (left(z) == y) left(z) = x ; else if(right(z) == y) right(z) = x ; fa[x] = z ;
    fa[right(x)] = y ; left(y) = right(x) ; fa[y] = x ; right(x) = y ;
}

inline void zag(int x) {
    /* left rotate */
    int y = fa[x] , z = fa[y] ;
    if (left(z) == y) left(z) = x ; else if(right(z) == y) right(z) = x ; fa[x] = z ;
    fa[left(x)] = y ; right(y) = left(x) ; fa[y] = x ; left(x) = y ;
}

inline void Splay(int x) {
	Clear(x) ;
	int y , z ;
	while (!Isrt(x)) {
		y = fa[x] , z = fa[y] ;
		if (Isrt(y)) {
			if (left(y) == x) zig(x) ; else zag(x) ;
		} else {
			if (left(z) == y) {
				if (left(y) == x) zig(x) , zig(x) ;
				else zag(x) , zig(x) ;
			} else if (right(z) == y) {
				if (right(y) == x) zag(x) , zag(x) ;
				else zig(x) , zag(x) ;
			}
		}
	}
}

inline int Access(int u) {
	int v = null ; for ( ; u ; u = fa[u]) Splay(u) , right(u) = v , v = u ; return v ;
}

inline void Makeroot(int u) {
	Access(u) , Splay(u) , rev[u] ^= true ;
}

inline void Link(int u , int v) {
	Makeroot(u) , fa[u] = v ;
}

inline void Cut(int u , int v) {
	Makeroot(u) ; Access(v) , Splay(v) , fa[u] = left(v) = null ;
}

inline int Getroot(int u) {
	for (u = Access(u) ; Clear(u) , left(u) ; u = left(u)) ; return u ;
}

inline void Query(int u , int v) {
	u = Getroot(u) , v = Getroot(v) ;
	printf("%s\n", u==v ? "Yes" : "No") ;
}

int n , m , cmd , u , v ;

int main() {
	#define READ
	#ifdef  READ
		freopen("sdoi2008_cave.in" ,"r",stdin ) ;
		freopen("sdoi2008_cave.out","w",stdout) ;
	#endif
	read(n) , read(m) ;
	while ( m -- ) {
		reads(cmd) , read(u) , read(v) ;
		if (cmd == 'C') Link(u , v) ;
		if (cmd == 'D') Cut(u , v) ;
		if (cmd == 'Q') Query(u , v) ;
	}
	#ifdef  READ
		fclose(stdin) ; fclose(stdout) ;
	#else
		getchar() ; getchar() ;
	#endif
	return 0 ;
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter