BZOJ 2049: [Sdoi2008]Cave 洞穴勘测

Description

辉辉热衷于洞穴勘测。某天,他按照地图来到了一片被标记为JSZX的洞穴群地区。经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴。假如两个洞穴可以通过一条或者多条通道按一定顺序连接起来,那么这两个洞穴就是连通的,按顺序连接在一起的这些通道则被称之为这两个洞穴之间的一条路径。洞穴都十分坚固无法破坏,然而通道不太稳定,时常因为外界影响而发生改变,比如,根据有关仪器的监测结果,123号洞穴和127号洞穴之间有时会出现一条通道,有时这条通道又会因为某种稀奇古怪的原因被毁。辉辉有一台监测仪器可以实时将通道的每一次改变状况在辉辉手边的终端机上显示:如果监测到洞穴u和洞穴v之间出现了一条通道,终端机上会显示一条指令 Connect u v 如果监测到洞穴u和洞穴v之间的通道被毁,终端机上会显示一条指令 Destroy u v 经过长期的艰苦卓绝的手工推算,辉辉发现一个奇怪的现象:无论通道怎么改变,任意时刻任意两个洞穴之间至多只有一条路径。因而,辉辉坚信这是由于某种本质规律的支配导致的。因而,辉辉更加夜以继日地坚守在终端机之前,试图通过通道的改变情况来研究这条本质规律。然而,终于有一天,辉辉在堆积成山的演算纸中崩溃了……他把终端机往地面一砸(终端机也足够坚固无法破坏),转而求助于你,说道:“你老兄把这程序写写吧”。辉辉希望能随时通过终端机发出指令 Query u v,向监测仪询问此时洞穴u和洞穴v是否连通。现在你要为他编写程序回答每一次询问。已知在第一条指令显示之前,JSZX洞穴群中没有任何通道存在。

Input

第一行为两个正整数n和m,分别表示洞穴的个数和终端机上出现过的指令的个数。以下m行,依次表示终端机上出现的各条指令。每行开头是一个表示指令种类的字符串s(”Connect”、”Destroy”或者”Query”,区分大小写),之后有两个整数u和v (1≤u, v≤n且u≠v) 分别表示两个洞穴的编号。

Output

对每个Query指令,输出洞穴u和洞穴v是否互相连通:是输出”Yes”,否则输出”No”。(不含双引号)

Sample Input

样例输入1 cave.in

200 5

Query 123 127

Connect 123 127

Query 123 127

Destroy 127 123

Query 123 127

样例输入2 cave.in3 5

Connect 1 2

Connect 3 1

Query 2 3

Destroy 1 3

Query 2 3

Sample Output

样例输出1 cave.out

No

Yes

No样例输出2 cave.outYes

No

HINT

数据说明 10%的数据满足n≤1000, m≤20000 20%的数据满足n≤2000, m≤40000 30%的数据满足n≤3000, m≤60000 40%的数据满足n≤4000, m≤80000 50%的数据满足n≤5000, m≤100000 60%的数据满足n≤6000, m≤120000 70%的数据满足n≤7000, m≤140000 80%的数据满足n≤8000, m≤160000 90%的数据满足n≤9000, m≤180000 100%的数据满足n≤10000, m≤200000 保证所有Destroy指令将摧毁的是一条存在的通道本题输入、输出规模比较大,建议c\c++选手使用scanf和printf进行I\O操作以免超时

Solution

又是一道LCT模板题!只需检查一下两个点是否在一棵树上即可。

Code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
#include <iostream>
#include <cstdio>
using namespace std;
const int INF = 1<<30;
typedef long long LL;
int getint() {
	int r = 0, k = 1; char c = getchar();
	for (; '0' > c || c > '9'; c = getchar()) if (c == '-') k = -1;
	for (; '0' <= c && c <= '9'; c = getchar()) r = r * 10 - '0' + c;
	return r * k;
}
char getop() {
	char c = getchar();
	for (; 'A' > c || c > 'Z'; c = getchar());
	return c;
}
int n, m;
int fa[10005] , c[10005][2], rev[10005];

bool isroot(int x) { return c[fa[x]][0]!=x&&c[fa[x]][1]!=x; }
void pushdown(int x) {
	if (rev[x]) {
		rev[c[x][0]] ^= 1; rev[c[x][1]] ^= 1;
		swap(c[x][0], c[x][1]);
		rev[x] = 0;
	}
}
int wson(int x) {
	return c[fa[x]][1] == x;
}
void rotate(int x) {
	int y = fa[x], z = fa[y], a = wson(x), b = !a;
    if (!isroot(y)) {
    	if (y == c[z][0]) c[z][0] = x;
    	else c[z][1] = x;
    }
    fa[x] = z; fa[y] = x; fa[c[x][b]] = y;
    c[y][a] = c[x][b]; c[x][b] = y;
}
int st[10005];

void splay(int x) {
	int tail = 1; st[1] = x;
	for (int i = x; !isroot(i); i = fa[i])
		st[++tail] = fa[i];
	for (int i = tail; i; i --) pushdown(st[i]);
	while (!isroot(x)) {
		int y = fa[x], z = fa[y], a = wson(x), b = wson(y);
		if (!isroot(y)) {
			if (a == b) rotate(y);
			else rotate(x);
		}
		rotate(x);
	}
}

void access(int x) {
	int y = 0;
	while (x) {
		splay(x);
		c[x][1] = y;
		y = x; x = fa[x];
	}
} 

int find_root(int x) {
	access(x);
	splay(x);
	while (c[x][0]) x = c[x][0];
	splay(x);
	return x;
}

void rever(int x) {
	access(x);
	splay(x);
	rev[x] ^= 1;
}

void link(int x, int y) {
	rever(x); fa[x] = y; splay(x);
}

void cut(int x, int y) {
	rever(x);
	access(y);
	splay(y);
	fa[x] = 0;
	c[y][0] = 0;
}

int main() {
	n = getint(); m = getint();
	while (m--) {
		int u, v;
		char o = getop(); u = getint(); v = getint();
		if (o == 'Q') {
			if (find_root(u) == find_root(v)) printf("Yes\n");
			else printf("No\n");
		} else if (o == 'C'){
			link(u, v);
		} else {
			cut(u, v);
		}
	}
	return 0;	
}