求助:为什么会T啊?
查看原帖
求助:为什么会T啊?
433966
_Agony楼主2021/9/16 11:04

蒟蒻刚刚学习LCT,看大佬写的题解准备自己淦,但是为什么我过不了?

#include <iostream>
#include <cstdio>

using namespace std;
const int N = 1e4 + 5;

int n, m;

int re() {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0'||ch > '9') {
		if(ch == '-') f = -1;
		ch = getchar();
	}
	while (ch <= '9'&&ch >= '0') {
		x = x * 10 + ch - '0';
		ch = getchar();
	}
	return x * f;
}

struct bal {
	int p;
	int s[2];
	bool flag;
};
bal t[N];

bool isrt(int x) {
	return t[t[x].p].s[0] != x&&t[t[x].p].s[1] != x;
}

void pushdown(int x) {
	if(!x || !t[x].flag)	
		return ;
	t[t[x].s[0]].flag ^= 1;
	t[t[x].s[1]].flag ^= 1;
	t[x].flag ^= 1;
	swap(t[x].s[1], t[x].s[0]);
}

void rotate(int x) {
	int y = t[x].p, z = t[y].p;
	int k = t[y].s[1] == x;
	if(!isrt(y)) {
		if(t[z].s[0] == y)
			t[z].s[0] = x;
		else
			t[z].s[1] == x;			
	}
	t[x].p = z; t[y].p = x;
	t[t[x].s[!k]].p = y;
	t[y].s[k] = t[x].s[!k];
	t[x].s[!k] = y;
}

void pushall(int x) {
	if(!isrt(x))
		pushall(t[x].p);
	pushdown(x);
}

void splay(int x) {
	pushall(x);
	while (!isrt(x)) {
		int y = t[x].p, z = t[y].p;
		if(!isrt(y)) {
			if((t[y].s[1] == x) ^ t[z].s[1] == y)
				rotate(x);
			else
				rotate(y);
		}
		rotate(x);
	}
}

void access(int x) {
	for(int y = 0; x; y = x, x = t[x].p) {
		splay(x);
		t[x].s[1] = y;
	}
}

void makert(int x) {
	access(x);
	splay(x);
	t[x].flag ^= 1;
}

void link(int u, int v) {
	makert(u);
	t[u].p = v;
}

void split(int u, int v) {
	makert(u);
	access(v);
	splay(v);
}

void cut(int u, int v) {
	split(u, v);
	t[v].s[0] = t[u].p = 0;
}

int find(int x) {
	access(x); splay(x);
	while (t[x].s[0])
		x = t[x].s[0];
	return x;
}

int main() {
	char op[10];
	int u, v;
	n =	re(); m = re();
	for(int i = 1; i <= m; i++) {
		scanf("%s", op);
		u = re(); v = re();
		if(u == v)	
			continue;
		if(op[0] == 'C')
			link(u, v);
		else if(op[0] == 'D')
			cut(u, v);
		else if(op[0] == 'Q')
			if(find(u) == find(v))
				printf("Yes\n");
			else
				printf("No\n");
	}
	return 0;
}
2021/9/16 11:04
加载中...