[BZOJ2049][SDOI2008]洞穴勘测(动态树LCT)
来源:互联网 发布:c语言获取网页内容 编辑:程序博客网 时间:2024/05/22 04:45
LCT模板题。
对于Connect操作,就执行
对于Destroy操作,就执行
对于Query操作,就判断是否
代码:
#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[N], lc[N], rc[N], rev[N], que[N], 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;}
阅读全文
0 0
- [BZOJ2049][SDOI2008]洞穴勘测(动态树LCT)
- [BZOJ2049][SDOI2008]Cave 洞穴勘测(LCT)
- bzoj2049: [Sdoi2008]Cave 洞穴勘测(lct)
- 【bzoj2049】【SDOI2008】【洞穴勘测】【lct】
- 【bzoj2049】[Sdoi2008]Cave 洞穴勘测 动态树
- 【BZOJ2049】洞穴勘测(SDOI2008)-LCT真·模板题
- bzoj2049: [Sdoi2008]Cave 洞穴勘测 LCT
- 【LCT】BZOJ2049[Sdoi2008]Cave 洞穴勘测
- [BZOJ2049][[Sdoi2008]Cave 洞穴勘测][LCT]
- LCT裸题-[BZOJ2049][Sdoi2008]Cave 洞穴勘测
- bzoj2049 [Sdoi2008]Cave 洞穴勘测 [LCT]
- 【动态树LCT】[SDOI2008]洞穴勘测
- [BZOJ2049] [SDOI2008] 洞穴勘测
- 【SDOI2008】bzoj2049 洞穴勘测
- bzoj2049 洞穴勘测 lct
- BZOJ2049 洞穴勘测 LCT
- 【bzoj2049】【LCT】洞穴勘测
- [BZOJ2049]SDOI2008洞穴勘测|LCT|并查集
- Android Mvp+DataBinding架构模式详解
- EL表达式 (详解)
- C语言 婚礼上的谎言
- Spring 框架学习(二):Spring 应用配置文件讲解
- 机器学习实战(python)——kNN问题解析
- [BZOJ2049][SDOI2008]洞穴勘测(动态树LCT)
- Spring aop 原理及各种应用场景
- 算法第七次作业
- NYoj 16 矩形嵌套 (DAG上最长路
- Mybatis入门
- Java中类的单继承,接口的多继承
- Qt中的基本文件操作
- AttributeError: module 'tensorflow' has no attribute 'mul'
- SalesOrder 合同表一些SQL操作