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. webpack 中版本兼容性问题错误总结

    一定不要运行npm i  XXX  -g(-d) 一定要指定版本,尽量低版本,也不最新版本,会导致不兼容和指令不一样的问题. 1.安装webpack-dev-server 报错,说需要webpack- ...

  2. Class AtomicInteger

    Overview Package Class Use Tree Deprecated Index Help Java™ PlatformStandard Ed. 7 Prev Class Next C ...

  3. 通过Webstorm上传代码到Github、更新代码后同步到github及克隆github代码到本地的方法

    导读: Github做为IT爱好者分享代码的一个知名的平台,广受大家喜欢,那么我们平时该怎么将自己写的代码上传到github上面保存并且提供给其他人参考? 我想方法不外乎如下几个: 1.直接在gith ...

  4. 如何让vba与java的TripleDES算法通用

    本文链接:http://www.cnblogs.com/Charltsing/p/TripleDES.html 众所周知,java默认采用的TripleDES算法是ECB+PKCS#5填充方式.网上可 ...

  5. 使用redis限制ip访问次数

    策略1: 在redis中保存一个count值(int),key为user:$ip,value为该ip访问的次数,第一次设置key的时候,设置expires. count加1之前,判断是否key是否存在 ...

  6. 【问题解决方案】之 hadoop 用jps命令后缺少namenode的问题

    用Xshell连接腾讯cloud里的虚拟机后,jps命令查无namenode导致过滤排序程序跑不起来,如图: 解决方案: Google之,说需要重启,格式化后再启动Hadoop.但鉴于本人不知道实现的 ...

  7. HowTos/Virtualization/VirtualBox - CentOS Wiki

    https://wiki.centos.org/HowTos/Virtualization/VirtualBox

  8. 搞站思路 <陆续完善中>

    只提供思路经验分享.不提供日站方法....一般站点那里最容易出现问题 入手思路: 主站一般都很安全.一般从二级域名下手 多看看那些大站新出来的测试分站点 猜路径别忘了google 考虑看站点下的rob ...

  9. Day 6-2简单的socket通信

    什么是socket? Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面 ...

  10. Kafka-Flume-elasticsearch

    a1.sources = kafkaSource a1.channels = memoryChannel a1.sinks = elasticsearch a1.sources.kafkaSource ...