【ybt金牌导航5-4-1】【luogu P2147】洞穴勘测

26 篇文章 0 订阅
13 篇文章 0 订阅

洞穴勘测

题目链接:ybt金牌导航5-4-1 / luogu P2147

题目大意

LCT 的模板题,好像还是弱化版。

要求实现一些操作。
加一条边,删一条边,询问两点是否连通。

思路

这题好像比模板题还好做。

——>模板题点这里看<——

判断是否连通模板题也有提到,就是看它们的根是否相同。
LCT 怎么做的就看模板题吧,那个可以实现的操作比这个还多。

代码

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

struct Tree {
	int son[2], fa, val, lazy;
}tree[200001];
int n, m, root, tot;
int tmp[200001], x, y;
string s;

int read() {
	int re = 0;
	char c = getchar();
	while (c < '0' || c > '9') c = getchar();
	while (c >= '0' && c <= '9') {
		re = re * 10 + c - '0';
		c = getchar();
	}
	return re;
}

bool is_root(int now) {
	return tree[tree[now].fa].son[0] != now && tree[tree[now].fa].son[1] != now;
}

void down1(int now) {
	swap(tree[now].son[0], tree[now].son[1]);
	tree[now].lazy ^= 1;
}

void down(int now) {
	if (tree[now].lazy && now) {
		if (tree[now].son[0]) down1(tree[now].son[0]);
		if (tree[now].son[1]) down1(tree[now].son[1]);
		tree[now].lazy = 0;
	}
}

void rotate(int now) {
	int father = tree[now].fa;
	int grand = tree[father].fa;
	int k = (now == tree[tree[now].fa].son[0]);
	int son = tree[now].son[k];
	
	if (!is_root(father))
		tree[grand].son[father == tree[tree[father].fa].son[1]] = now;
	tree[now].son[k] = father;
	tree[father].son[!k] = son;
	tree[father].fa = now;
	tree[now].fa = grand;
	if (son) tree[son].fa = father;
}

void splay(int x) {
	tmp[0] = 1;
	tmp[1] = x;
	for (int i = x; !is_root(i); i = tree[i].fa)
		tmp[++tmp[0]] = tree[i].fa;
	
	while (tmp[0]) {
		down(tmp[tmp[0]]);
		tmp[0]--;
	}
	
	while (!is_root(x)) {
		if (!is_root(tree[x].fa)) {
			rotate(((x == tree[tree[x].fa].son[1]) ^ (tree[x].fa == tree[tree[tree[x].fa].fa].son[1])) ? x : tree[x].fa);
		}
		rotate(x);
	}
}

void access(int now) {
	int son = 0;
	while (now) {
		splay(now);
		tree[now].son[1] = son;
		if (son) tree[son].fa = now;
		son = now;
		now = tree[now].fa;
	}
}

int find_root(int now) {
	access(now);
	splay(now);
	down(now);
	while (tree[now].son[0]) {
		now = tree[now].son[0];
		down(now);
	}
	return now;
}

void make_root(int now) {
	access(now);
	splay(now);
	down1(now);
}

void select(int x, int y) {
	make_root(x);
	access(y);
	splay(y);
}

void link(int x, int y) {
	if (find_root(y) == find_root(x)) return ;
	make_root(x);
	tree[x].fa = y;
}

void delete_(int x, int y) {
	select(x, y);
	tree[x].fa = 0;
	tree[y].son[0] = 0;
}

int main() {
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= m; i++) {
		cin >> s;
		scanf("%d %d", &x, &y);
		
		if (s[0] == 'C') {
			link(x, y);
		}
		else if (s[0] == 'D') {
			delete_(x, y);
		}
		else if (s[0] == 'Q') {
			if (find_root(x) == find_root(y)) printf("Yes\n");
				else printf("No\n");
		}
	}
	
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值