刚学LCT,求助QAQ,全re,对了一个点
查看原帖
刚学LCT,求助QAQ,全re,对了一个点
342584
流夏的美楼主2021/1/15 14:29
#include <stdio.h>
#include <iostream>
const int N = 10005;

int n, m;
int fa[N], chi[N][2], tag[N], sta[N];

bool ifroot(int rt) {
	return chi[fa[rt]][1] == rt || chi[fa[rt]][0] == rt;
}

void push(int rt) {
	tag[rt] ^= 1;
	std :: swap(chi[rt][1], chi[rt][0]);
	return ;
}

void pushdown(int rt) {
	if(tag[rt] && rt) {
		tag[chi[rt][1]] ^= 1;
		tag[chi[rt][0]] ^= 1;
		std :: swap(chi[rt][1], chi[rt][0]);
		tag[rt] = 0;
	}
	return ;
}

void reve(int x) {
	int y = fa[x], z = fa[y], f = chi[y][1] == x, t = chi[x][!f];
	if(ifroot(y)) 
		chi[z][chi[z][1] == y] = x;
	chi[x][!f] = y, chi[y][f] = t;
	if(t)
		fa[t] = y;
	fa[y] = x, fa[x] = z;
	return ;
}

void splay(int x) {
	int y = x, top = 0, z;
	sta[++top] = y;
	while(ifroot(y)) sta[++top] = y = fa[y];
	while(top) pushdown(sta[top--]);
	while(ifroot(x)) {
		y = fa[x], z = fa[y];
		if(ifroot(y))
			reve((chi[y][0] == x) ^ (chi[z][0] == y) ? x : y);
		reve(x);
	}
	return ;
}

void access(int x) {
	for(int i = 0; x; x = fa[i = x])
		splay(x), chi[x][1] = i;
	return ;
}

int findrt(int x) {
	access(x);
	splay(x);
	while(chi[x][0]) pushdown(x), x = chi[x][0];
	splay(x);
	return x;
}

void makeroot(int x) {
	access(x), splay(x);
	push(x);
	return ;
}

void split(int x, int y) {
	makeroot(x);
	access(y), splay(y);
	return ;
}

void cut(int x, int y) {
	split(x, y);
	fa[x] = chi[y][0] = 0;
	return ;
}

void link(int x, int y) {
	makeroot(x);
	if(findrt(y) != x) fa[x] = y;
	return ;
}

bool que(int x, int y) {
	if(x == y)
		return true;
	return findrt(x) == findrt(y);
}

int main() {
	std :: cin >> n >> m;
	std :: string c;
	int x, y;
	for(int i = 1; i <= m; ++i) {
		std :: cin >> c >> x >> y;
		if(c[0] == 'Q') {
			if(que(x, y))
				std :: cout << "Yes" << std :: endl;
			else
				std :: cout << "No" << std :: endl;
		}
		if(c[0] == 'C')
			if(x != y)
				link(x, y);
		if(c[0] == 'D')
			cut(x, y);
	}
	return 0;
}
2021/1/15 14:29
加载中...