LCT判断联通性

没什么特别的。。还是一个普通的板子题,把LCT当并查集用了,只不过LCT灵活一些,还可以断边

话说自从昨天被维修数列那题榨干之后我现在写splay都不用动脑子了,,机械式的码splay23333

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 10005;
int n, m, tot, ch[N][2], fa[N], rev[N], st[N]; int newNode(){
fa[++tot] = 0; ch[tot][0] = ch[tot][1] = 0;
return tot;
} bool isRoot(int x){
return ch[fa[x]][0] != x && ch[fa[x]][1] != x;
} void reverse(int x){
rev[x] ^= 1;
swap(ch[x][0], ch[x][1]);
} void push_down(int x){
if(rev[x]){
rev[x] ^= 1;
reverse(ch[x][0]), reverse(ch[x][1]);
}
} void rotate(int x){
int y = fa[x], z = fa[y], p = (ch[y][1] == x) ^ 1;
push_down(y), push_down(x);
ch[y][p^1] = ch[x][p], fa[ch[x][p]] = y;
if(!isRoot(y)) ch[z][ch[z][1] == y] = x;
fa[x] = z, fa[y] = x, ch[x][p] = y;
} void splay(int x){
int pos = 0; st[++pos] = x;
for(int i = x; !isRoot(i); i = fa[i]) st[++pos] = fa[i];
while(pos) push_down(st[pos--]);
while(!isRoot(x)){
int y = fa[x], z = fa[y];
if(!isRoot(y)){
if((ch[y][0] == x) ^ (ch[z][0] == y)) rotate(x);
else rotate(y);
}
rotate(x);
}
} void access(int x){
for(int p = 0; x; p = x, x = fa[x])
splay(x), ch[x][1] = p;
} void makeRoot(int x){
access(x), splay(x), reverse(x);
} int findRoot(int x){
access(x), splay(x);
while(ch[x][0]) x = ch[x][0];
return x;
} void link(int x, int y){
makeRoot(x);
if(findRoot(y) != x) fa[x] = y;
} void cut(int x, int y){
makeRoot(x), access(y), splay(y);
fa[x] = ch[y][0] = 0;
} bool isConnect(int x, int y){
return findRoot(x) == findRoot(y);
} int main(){ int n = read(), m = read();
for(int i = 1; i <= n; i ++) newNode();
while(m --){
char opt[20]; scanf("%s", opt);
int x = read(), y = read();
if(opt[0] == 'Q') printf(isConnect(x, y) ? "Yes\n" : "No\n");
else if(opt[0] == 'C') link(x, y);
else if(opt[0] == 'D') cut(x, y);
}
return 0;
}

BZOJ 2049 洞穴勘测的更多相关文章

  1. BZOJ 2049洞穴探测

    辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假如两个洞穴可 ...

  2. [bzoj] 2049 洞穴勘探 || LCT

    原题 这是一道LCT的板子题. 至于LCT--link cut tree,也叫动态树,用splay实现动态连边的树. 预备知识: 实边:一个非叶节点,向它的儿子中的一个连一条特殊的边,称为实边;该非叶 ...

  3. 【BZOJ】2049: [Sdoi2008]Cave 洞穴勘测(lct/并查集)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2049 bzoj挂了..在wikioi提交,,1A-写lct的速度越来越快了-都不用debug-- 新 ...

  4. 【BZOJ】【2049】【SDOI2008】洞穴勘测 Cave

    LCT 哦……LCT的一道更水的裸题,适合学习access,link,cut等基本操作(其实这三个不是在一个层面上的?不要在意这些细节……) /**************************** ...

  5. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 LCT

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...

  6. bzoj 2049: [Sdoi2008]Cave 洞穴勘测 动态树

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 3119  Solved: 1399[Submit] ...

  7. bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2049 题面: 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 ...

  8. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (动态树入门)

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1528  Solved: 644[Submit][ ...

  9. [BZOJ 2049] [Sdoi2008] Cave 洞穴勘测 【LCT】

    题目链接:BZOJ - 2049 题目分析 LCT的基本模型,包括 Link ,Cut 操作和判断两个点是否在同一棵树内. Link(x, y) : Make_Root(x); Splay(x); F ...

随机推荐

  1. H5 68-伪元素选择器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. ajax成功后XML 解析错误:格式不佳

    就是Ajax发送请求后,意图回显数据时会出现这个错误,貌似chrome浏览器不会报用火狐能看到: 可能的原因有两个,就是后台应该返回一个json格式的字符串,但是你返回的是浏览器看不懂的,也就是返回格 ...

  3. Johnson算法

    用于求稀疏图上的全局最短路. 考虑将带负权的图变为不带负权的图,再跑\(n\)次Dijkstra. 方法:新建点S,向所有点连边权为\(0\)的边,然后以S为起点跑SPFA.然后将每条边的权值重新赋为 ...

  4. Latex(表格|图片(一丢丢))

    目录 普通的例子 Notation 例子 p{width} 列分割符 @{} \multicolumn supertabular | longtabular 浮动体 table 浮动体 图片 \use ...

  5. es6在网页中模块引入的方法

    前言: 以前,当然包括现在的大部分js引入,我们都是利用<script></script>这种全局的方式进行引入,当然这种弊端还是用的,比如这样直接利用script引入的话,会 ...

  6. TCP 原理

    一.分组交换网络   古老的电话通信,一根电缆,两个用户设备通信 计算机中的两个设备节点通信:分组网络 计算机网络采取分组交换技术,意思就是我有[一块数据]要发给对方,那我会把这[一块数据]分成N份[ ...

  7. Python之缩进块

    pycharm编辑器识别冒号,当换行后下一行默认是缩进块的位置:

  8. C99标准的柔性数组 (Flexible Array)

    [什么是柔性数组(Fliexible Array)] 柔性数组在C99中的定义是: 6.7.2.1 Structure and union specifiers As a special case, ...

  9. Git-用 cherry-pick 挑好看的小樱桃

         版权声明:本文为博主原创文章,转载请在文章明显位置标明文章原属哦. https://blog.csdn.net/qq_32452623/article/details/79449534 ti ...

  10. Windows NT 的历史

    Windows NT 的版本历史 https://blog.csdn.net/flyingpig2016/article/details/53282895/ 按照自己找到的资料:windows NT ...