BZOJ-2049 [SDOI2008]洞穴勘测

LCT模版题。。。。

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cctype>
#include <cmath>
#define rep(i, l, r) for(int i=l; i<=r; i++)
#define clr(x, c) memset(x, c, sizeof(x))
#define travel(x) for(edge *p=fir[x]; p; p=p->n)
#define l(x) c[x][0]
#define r(x) c[x][1]
#define f(x) Father[x]
#define h(x) Head[x]
#define maxn 10009
#define inf 0x7fffffff
using namespace std;
inline int read()
{
	int x=0, f=1; char ch=getchar();
	while (!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
	while (isdigit(ch)) x=x*10+ch-'0', ch=getchar();
	return x*f;
}
int n, c[maxn][2], Head[maxn], Father[maxn];
bool rev[maxn];

inline void pushdown(int x)
{
	rev[x]^=1, rev[l(x)]^=1, rev[r(x)]^=1;
	swap(l(l(x)), r(l(x))), swap(l(r(x)), r(r(x)));
}
inline void rotate(int x)
{
	int y=f(x), z=f(y), l=(c[y][1]==x), r=l^1;
	if (z) c[z][c[z][1]==y]=x;
	f(c[x][r])=y, f(y)=x, f(x)=z;
	c[y][l]=c[x][r]; c[x][r]=y;
}
inline void Splay(int x)
{
	if (!x) return; if (rev[x]) pushdown(x); int y;
	while (f(x)) 
	{
		if (rev[y=f(x)]) pushdown(y), pushdown(x);
		if (!f(y)) h(x)=h(y), h(y)=0;
		rotate(x);
	}
}
inline void Acc(int x)
{
	int cmp=x, y; Splay(x); 
	f(r(x))=0, h(r(x))=x, r(x)=0;
	while (h(x)) 
		Splay(y=h(x)), f(r(y))=0, h(r(y))=y, r(y)=x, f(x)=y, h(x)=0, x=y;
	Splay(cmp);
}
inline void Eve(int x){Acc(x); rev[x]^=1; swap(l(x), r(x));}
inline int Top(int x){Acc(x); int now=x; while (l(now)) now=l(now); return now;}
inline void Build(){rep(i, 1, n) l(i)=r(i)=h(i)=f(i)=0;}
inline void Connect(int x, int y){Eve(x); Eve(y); Splay(y); h(x)=y;}
inline void Destroy(int x, int y){Eve(x); Acc(y); f(x)=l(y)=0;}
inline void Query(int x, int y){if (Top(x)==Top(y)) puts("Yes"); else puts("No");}
int main()
{
	n=read(); int m=read(); Build();
	rep(i, 1, m)
	{
		char ch[5]; scanf("%s", ch); int x=read(), y=read();
		if (ch[0]=='C') Connect(x, y); else if (ch[0]=='D') Destroy(x, y); else Query(x, y);
	}
	return 0;
}
posted @ 2015-05-05 17:27  NanoApe  阅读(158)  评论(0编辑  收藏  举报
AmazingCounters.com