传送门: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】的更多相关文章

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

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

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

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

  3. [BZOJ2049][Sdoi2008]Cave 洞穴勘测 LCT模板

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

  4. 【BZOJ2049】 [Sdoi2008]Cave 洞穴勘测 LCT/并查集

    两种方法: 1.LCT 第一次LCT,只有link-cut和询问,无限T,到COGS上找了数据,发现splay里的父亲特判出错了(MD纸张),A了,好奇的删了反转T了.... #include < ...

  5. [BZOJ2049] [SDOI2008] Cave 洞穴勘测 (LCT)

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

  6. 【bzoj2049】[Sdoi2008]Cave 洞穴勘测 LCT

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

  7. bzoj2049: [Sdoi2008]Cave 洞穴勘测 lct裸题

    题意:三种操作一种摧毁一条边,一种链接一条边,一种查询两个点是否联通 题解:lct的link和cut即可 /********************************************** ...

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

    [题意]给定n个点和m个操作,每次操作:1.连接2个点.2.断开2个点.3.查询2个点是否连通.m<=2*10^5. [算法]Link-Cut Tree [题解]LCT模板题,Link,Cut, ...

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

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 省选之前来切一道数据结构模板题. 题意 这是一道模板题. N个点,M次操作,每次加边/ ...

随机推荐

  1. 拷贝地图 CopyAndOverwriteMap()

    private void CopyAndOverwriteMap() { //Get IObjectCopy interface IObjectCopy objectCopy = new Object ...

  2. linux之rsync远程数据同步备份

    rsync服务是一种高效的远程数据备份的工具,该服务的port号为873, 是Liunx下的一种非独立服务.由xinetd超级服务管理,取代监听873port. 长处: 1.rsync能够利用ssh和 ...

  3. js中字符串的拼接的另一种方法

    // 按一定长度截断字符串,并使用 + 运算符进行连接. // 分隔字符串尽量按语义进行,如不要在一个完整的名词中间断开. // 特别的,对于HTML片段的拼接,通过缩进,保持和HTML相同的结构. ...

  4. [libcurl]_[0基础]_[使用libcurl下载大文件]

    场景: 1. 在Windows编程时, 下载http页面(html,xml)能够使用winhttp库,可是并非非常下载文件,由于会失败. 由此引出了WinINet库,无奈这个库的稳定性比較低,使用样例 ...

  5. eclipse中导入其它的webproject遇到和解决的问题

    注:下面为我从网上搜来的方法.经使用及学习后整理. 学习javaweb有段时间了.对于导入新项目.遇到好多问题.但终于成功了. 错误1:string cannot be resolved to a t ...

  6. C语言-- static 全局使用示例

    C语言-- static 全局使用示例  前言:看到很多使用Objective-C开发IOS的大牛,有时候会使用static全局变量,相比之下,我却很少用这个,从而很少对其有着比较有实质意义的理解,甚 ...

  7. Android Service 不被杀死并提高优先级

    Android Service 不被杀死有两种思路,一种是将APP设置为系统应用.还有一种是增强service的生命力.即使屏幕背光关闭时也能执行. 因为设置为系统应用须要root.所以一般使用后一种 ...

  8. 【iOS系列】-iOS开发常用库文件总结

    这里是列举出得一部分,更多内容可参考https://github.com/darren90/Gather_iOS 码农周刊的总结 - 覆盖很广 调调的 - 很多开发相关内容都有体现 右滑返回的解决 - ...

  9. SpringMVC_基本配置 --跟海涛学SpringMVC(和自己在项目中的实际使用的对比)

    ☆依赖jar包: 1.Spring框架jar 包: 为了简单,将spring-framework-3.1.1.RELEASE-with-docs.zip/dist/下的所有jar包拷贝到项目的WEB- ...

  10. Mac Launchpad图标调整

    Launchpad图标大小怎么调整?,很多人觉得默认Launchpad的应用程序图标很大,空间比较拥挤,看起来一点也不精致,那么我们怎样才能调整Launchpad的图标大小呢?其实可以通过调整Laun ...