[洛谷P2147][SDOI2008]洞穴勘测
题目大意:有$n$个洞穴,$m$条指令,指令有三种
- $Connect\;u\;v$:在$u,v$之间连一条边
- $Destroy\;u\;v$:切断$u,v$之间的边
- $Query\;u\;v$:询问$u,v$是否连通
(数据保证合法)
题解:$LCT$(潘佳奇的板子)
卡点:无(潘佳奇的板子)
C++ Code:
#include <cstdio>
#include <cstring>
#define maxn 10010
using namespace std;
int son[2][maxn], fa[maxn], tg[maxn];
inline bool get(int x, int flag = 1) {return son[flag][fa[x]] == x;}
inline bool is_root(int x) {return !(get(x, 0) || get(x, 1));}
inline void swap(int &x, int &y) {x ^= y ^= x ^= y;}
inline void rotate(int x) {
int y = fa[x], z = fa[y]; bool b = get(x);
if (!is_root(y)) son[get(y)][z] = x;
son[b][y] = son[!b][x]; son[!b][x] = y;
fa[y] = x; fa[x] = z; fa[son[b][y]] = y;
}
inline void pushdown(int x) {
if (tg[x]) {
tg[son[0][x]] ^= 1; tg[son[1][x]] ^= 1;
swap(son[0][x], son[1][x]); tg[x] ^= 1;
}
}
int st[maxn], top;
inline void splay(int x) {
st[top = 1] = x;
for (int y = x; !is_root(y); st[++top] = y = fa[y]);
for (; top; --top) if (tg[st[top]]) pushdown(st[top]);
for (; !is_root(x); rotate(x)) if (!is_root(fa[x]))
get(x) ^ get(fa[x]) ? rotate(x) : rotate(fa[x]);
}
inline void access(int x) {for (int t = 0; x; son[1][x] = t, t = x, x = fa[x]) splay(x);}
inline void make_root(int x) {access(x); splay(x); tg[x] ^= 1;}
inline void split(int x, int y) {make_root(x); access(y); splay(y);}
inline void link(int x, int y) {make_root(x); fa[x] = y;}
inline void cut(int x, int y) {split(x, y); son[0][y] = fa[x] = 0;}
inline bool query(int x, int y) {
split(x, y); int now = y;
while (son[0][now]) now = son[0][now];
return now == x;
}
int n, Q, x, y;
char s[30];
int main() {
scanf("%d%d", &n, &Q);
while (Q --> 0) {
scanf("%s%d%d", s, &x, &y);
if (s[0] == 'Q') {
if (query(x, y)) puts("Yes");
else puts("No");
} else {
if (s[0] == 'C') link(x, y);
else cut(x, y);
}
}
return 0;
}
[洛谷P2147][SDOI2008]洞穴勘测的更多相关文章
- 洛谷P2147[SDOI2008]洞穴勘测(lct)
题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假 ...
- 洛谷 P2147 [SDOI2008]洞穴勘测 (线段树分治)
题目链接 题解 早就想写线段树分治的题了. 对于每条边,它存在于一段时间 我们按时间来搞 我们可把一条边看做一条线段 我们可以模拟线段树操作,不断分治下去 把覆盖\(l-r\)这段时间的线段筛选出来, ...
- 洛谷 P2147 [SDOI2008]洞穴勘测
以下这个做法应该是叫线段树分治... 根据修改操作预处理出每条边存在的时间区间[l,r](以操作序号为时间),然后把所有形式化后的修改挂到线段树节点上. 处理完修改后,dfs一遍线段树,进入某个节点时 ...
- 洛谷 P2147 [SDOI2008]洞穴勘测 LCT
Code: #include <cstdio> #include <algorithm> #include <string> #include <cstrin ...
- 洛谷P2147 [SDOI2008] 洞穴勘探 [LCT]
题目传送门 洞穴勘探 题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道 ...
- 【洛谷P2147】洞穴勘测
题目大意:维护 N 个点的无向图,支持动态加边和删边,回答两点的连通性. 题解:线段树分治 + 可撤销并查集 询问可以离线,这是线段树分治的基础. 建立在操作时间轴上的线段树称为线段树分治算法. 本题 ...
- P2147 [SDOI2008]洞穴勘测(LCT)
P2147 [SDOI2008]洞穴勘测 裸的LCT. #include<iostream> #include<cstdio> #include<cstring> ...
- P2147 [SDOI2008]洞穴勘测
P2147 [SDOI2008]洞穴勘测 思路 没办法,我就是喜欢板子都想发的人 都是基础操作,不多说了 代码 #include <bits/stdc++.h> #define ls ch ...
- 洛谷P2147 [SDOI2008]Cave 洞穴勘测
题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假 ...
随机推荐
- 分享一个PC端六格密码输入框写法
如图.我们一般做商城类的项目不免会用到支付密码输入框,我研究了下并决定发上来,也当作是自己成长路上的一点小小的记录.本次介绍的是基于vue的项目 html: <template> < ...
- __name__ 和 "__main__"
本模块名: person 调用者模块名: start import sys def funcperson(): print('我是人') print(sys.modules[__name__]) # ...
- NAND Flash结构及驱动函数
目标:以NAND Flash K9F2G08U0M为例介绍其结构及其驱动程序的书写 1. 结构 由芯片手册中的图可知:K9F2G08U0M大小为2112Mbits(即 256MB = 2Gb ) 共有 ...
- Go生成UUID
Go生成UUID 在实际项目中,是经常会使用到一个唯一标识的,比如唯一标识一条记录等,使用C#得到唯一标识是很容易的.例 string guid = Guid.NewGuid().ToString() ...
- 生産管理(PP)
伝票系 製造指図 マスタ系 生産資源/治工具 作業区 能力 作業手順 作業バージョン 作業記録 需要予測プロファイル 計画手配 MRP レシピ その他 カスタマイズ系 BOM関連 製造指図確認 伝票系 ...
- PHP HashTable总结
本篇文章主要是对 PHP HashTable 总结,下面的参考链接是很好的学习资料. 总结 HashTable 又叫做散列表,是一种用于以常数平均时间执行插入.删除和查找的技术.不能有效的支持元素之间 ...
- 1 opencv2.4 + vs2013
http://blog.csdn.net/poem_qianmo/article/details/19809337 1.安装vs2013 2.安装opencv2.4 下载地址:https://sour ...
- P1189 SEARCH(逃跑的拉尔夫)
P1189 SEARCH 题目描述 年轻的拉尔夫开玩笑地从一个小镇上偷走了一辆车,但他没想到的是那辆车属于警察局,并且车上装有用于发射车子移动路线的装置. 那个装置太旧了,以至于只能发射关于那辆车的移 ...
- 破解PHPStrom 10 and Pycharm
注册时选择 License server http://idea.lanyus.com/ 然后点击OK Pycharm -- License server http://idea.lanyus.com ...
- Python进程、线程、协程及IO多路复用
详情戳击下方链接 Python之进程.线程.协程 python之IO多路复用