_bzoj2049 [Sdoi2008]Cave 洞穴勘测【LCT】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2049
裸的LCT,保存LCT模版。说一下出bug的几个地方叭:
①,rotate时,没有判断y是否为根,这点与普通的Splay有点差别。
②,循环变量是i,而不是x!
#include <cstdio>
#include <algorithm> const int maxn = 10005; int n, m, t1, t2;
int ch[maxn][2], fa[maxn], stk[maxn], top;
char opr[10], rev[maxn]; inline bool isroot(int x) {
return ch[fa[x]][0] != x && ch[fa[x]][1] != x;
}
inline void pushdown(int x) {
if (rev[x]) {
rev[x] = 0;
rev[ch[x][0]] ^= 1;
rev[ch[x][1]] ^= 1;
std::swap(ch[x][0], ch[x][1]);
}
}
inline void rotate(int x) {
int y = fa[x];
if (!isroot(y)) {
if (y == ch[fa[y]][0]) {
ch[fa[y]][0] = x;
}
else {
ch[fa[y]][1] = x;
}
}
fa[x] = fa[y];
int dir = x == ch[y][1];
ch[y][dir] = ch[x][dir ^ 1];
fa[ch[x][dir ^ 1]] = y;
ch[x][dir ^ 1] = y;
fa[y] = x;
}
inline void splay(int x) {
int p;
char flag1, flag2;
top = 0;
stk[top++] = x;
for (int i = x; !isroot(i); i = fa[i]) {
stk[top++] = fa[i];
}
for (int i = top - 1; ~i; --i) {
pushdown(stk[i]);
}
while (!isroot(x)) {
p = fa[x];
if (isroot(p)) {
rotate(x);
}
else {
flag1 = p == ch[fa[p]][1];
flag2 = x == ch[p][1];
if (flag1 ^ flag2) {
rotate(x);
}
else {
rotate(p);
}
rotate(x);
}
}
}
inline void acc(int x) {
int y = 0;
while (x) {
splay(x);
ch[x][1] = y;
y = x;
x = fa[x];
}
}
inline void make_root(int x) {
acc(x);
splay(x);
rev[x] ^= 1;
}
inline void link(int x, int y) {
make_root(x);
fa[x] = y;
}
inline void cutt(int x, int y) {
make_root(x);
acc(y);
splay(y);
ch[y][0] = fa[x] = 0;
}
inline int fnd_root(int x) {
acc(x);
splay(x);
int i;
for (i = x; ch[i][0]; i = ch[i][0]);
return i;
} int main(void) {
//freopen("in.txt", "r", stdin);
scanf("%d%d", &n, &m);
while (m--) {
scanf("%s%d%d", opr, &t1, &t2);
if (opr[0] == 'Q') {
make_root(t1);
puts(fnd_root(t2) == t1? "Yes": "No");
}
else if (opr[0] == 'C') {
link(t1, t2);
}
else {
cutt(t1, t2);
}
}
return 0;
}
_bzoj2049 [Sdoi2008]Cave 洞穴勘测【LCT】的更多相关文章
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2049 题面: 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 LCT
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...
- [BZOJ2049][Sdoi2008]Cave 洞穴勘测 LCT模板
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 9705 Solved: 4674[Submit] ...
- 【BZOJ2049】 [Sdoi2008]Cave 洞穴勘测 LCT/并查集
两种方法: 1.LCT 第一次LCT,只有link-cut和询问,无限T,到COGS上找了数据,发现splay里的父亲特判出错了(MD纸张),A了,好奇的删了反转T了.... #include < ...
- [BZOJ2049] [SDOI2008] Cave 洞穴勘测 (LCT)
Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...
- 【bzoj2049】[Sdoi2008]Cave 洞穴勘测 LCT
题目描述 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假如 ...
- bzoj2049: [Sdoi2008]Cave 洞穴勘测 lct裸题
题意:三种操作一种摧毁一条边,一种链接一条边,一种查询两个点是否联通 题解:lct的link和cut即可 /********************************************** ...
- 【BZOJ】2049: [Sdoi2008]Cave 洞穴勘测 LCT
[题意]给定n个点和m个操作,每次操作:1.连接2个点.2.断开2个点.3.查询2个点是否连通.m<=2*10^5. [算法]Link-Cut Tree [题解]LCT模板题,Link,Cut, ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测——LCT
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 省选之前来切一道数据结构模板题. 题意 这是一道模板题. N个点,M次操作,每次加边/ ...
随机推荐
- 【面试被虐】如何只用2GB内存从20亿,40亿,80亿个整数中找到出现次数最多的数?
这几天小秋去面试了,不过最近小秋学习了不少和位算法相关文章,例如 [面试现场]如何判断一个数是否在40亿个整数中? [算法技巧]位运算装逼指南 对于算法题还是有点信心的,,,,于是,发现了如下对话. ...
- kvm虚拟化学习笔记(三)之windows kvm虚拟机安装
KVM虚拟化学习笔记系列文章列表----------------------------------------kvm虚拟化学习笔记(一)之kvm虚拟化环境安装http://koumm.blog.51 ...
- django 简易博客开发 5 markdown支持、代码高亮、gravatar头像服务
上一篇博客介绍了comments库使用及ajax支持,现在blog已经具备了基本的功能,但是只能发表文字,不支持富文本编辑.今天我们利用markdown添加富文本支持. markdown语法说明: h ...
- 【网络】TCP的流量控制
一.利用滑动窗口实现流量控制 流量控制是让发送方的发生速率不要太快,要让接收方来得及接收. 发送方的发送窗口不能超过接收方给出的接收窗口的数值,TCP的窗口单位是字节,不是报文段. TCP为每一个连接 ...
- javascript/jquery模板引擎——Handlebars初体验
Handlebars.js下载地址:http://handlebarsjs.com/ 最近自己在建一个站,采用完全的前后端分离的方式,现在正在做前端的部分.其中有项功能是需要ajax调用后端接口,返回 ...
- js和jquery实现回到顶层
js <!DOCTYPE html> <html> <head> <title>返回顶部</title> <style> bod ...
- oracle官方文档_查看初始化參数(举例)
原创作品,出自 "深蓝的blog" 博客.深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/46864217 记录 ...
- 一个很小的C++写的MVC的例子
#include<iostream> #include<vector> //get namespace related stuff using std::cin; using ...
- 初探active mq
mq(message queue),即消息队列,目前比较流行消息队列是active mq 和kafka.本文介绍如何简单的使用active mq. ActiveMQ官网下载地址:http://acti ...
- FFT做题记录
FFT是用来快速求卷积的..... 那么卷积有什么作用呢 https://www.zhihu.com/question/22298352 看完就懂了