您的位置:首页 > 其它

[BZOJ2049][SDOI2008]洞穴勘测(动态树LCT)

2017-10-22 10:29 465 查看
LCT模板题。

对于Connect操作,就执行Link(u,v),即连边(u,v)。

对于Destroy操作,就执行Cut(u,v),即删边(u,v)。

对于Query操作,就判断是否FindRoot(u)==FindRoot(v),即判断是否在同一个连通块内。

代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
inline int read() {
int res = 0; bool bo = 0; char c;
while (((c = getchar()) < '0' || c > '9') && c != '-');
if (c == '-') bo = 1; else res = c - 48;
while ((c = getchar()) >= '0' && c <= '9')
res = (res << 3) + (res << 1) + (c - 48);
return bo ? ~res + 1 : res;
}
inline char get() {
char c; while ((c = getchar()) != 'C' && c != 'D' && c != 'Q');
return c;
}
const int N = 5e4 + 5;
int n, Q, fa
, lc
, rc
, rev
, que
, len;
int which(int x) {return rc[fa[x]] == x;}
bool is_root(int x) {
return !fa[x] || (lc[fa[x]] != x && rc[fa[x]] != x);
}
void down(int x) {
if (rev[x]) {
swap(lc[x], rc[x]);
if (lc[x]) rev[lc[x]] ^= 1;
if (rc[x]) rev[rc[x]] ^= 1;
rev[x] = 0;
}
}
void rotate(int x) {
int y = fa[x], z = fa[y], b = lc[y] == x ? rc[x] : lc[x];
if (z && !is_root(y)) (lc[z] == y ? lc[z] : rc[z]) = x;
fa[x] = z; fa[y] = x; b ? fa[b] = y : 0;
if (lc[y] == x) rc[x] = y, lc[y] = b;
else lc[x] = y, rc[y] = b;
}
void splay(int x) {
int i, y; que[len = 1] = x;
for (y = x; !is_root(y); y = fa[y]) que[++len] = fa[y];
for (i = len; i >= 1; i--) down(que[i]);
while (!is_root(x)) {
if (!is_root(fa[x])) {
if (which(x) == which(fa[x])) rotate(fa[x]);
else rotate(x);
}
rotate(x);
}
}
void Access(int x) {
int y;
for (y = 0; x; y = x, x = fa[x]) {
splay(x); rc[x] = y;
if (y) fa[y] = x;
}
}
int Find_Root(int x) {
Access(x); splay(x);
while (down(x), lc[x]) x = lc[x];
splay(x); return x;
}
void Make_Root(int x) {
Access(x); splay(x);
rev[x] ^= 1;
}
void Link(int x, int y) {
Make_Root(x); fa[x] = y;
}
void Cut(int x, int y) {
Make_Root(x); Access(y); splay(y);
lc[y] = 0; fa[x] = 0;
}
int main() {
int i, x, y; n = read(); Q = read(); char op;
while (Q--) {
op = get(); x = read(); y = read();
if (op == 'C') Link(x, y);
else if (op == 'D') Cut(x, y);
else printf(Find_Root(x) == Find_Root(y) ? "Yes\n" : "No\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: