传送门

1.并查集骗分(数据太水,比正解还快。。。)

我们知道,并查集有一步操作叫“路径压缩”,但是本题的并查集我们不能路径压缩,否则就无法进行Destroy操作。那每一步操作我们应该怎么做呢?

对于Connect x y操作,先把x变成集合的根,之后root[x] = y;

对于Destroy x y操作,先把x变成集合的根,此时root[y]必然为x,令root[y] = y即可。

对于Query x y操作,看看x和y所在集合的根是不是一样的就好了。

那么如何把x变成集合的根呢?只要把从x到根路径上的每一条边反向即可,所以不能进行路径压缩。

其实并查集的解法也有用 lct 的思想的。

在这里,并查集中的两点之间的边就表示连接两个洞穴之间的边,非常的直接。。

注意一个细节 : 题目中说——无论通道怎么改变,任意时刻任意两个洞穴之间至多只有一条路径

也就是说不会有环!这也正是能用并查集做的原因之一。

——代码

 #include <cstdio>
#include <iostream>
#define N 10001 int n, m;
int f[N]; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline int find(int x)
{
while(x ^ f[x]) x = f[x];
return x;
} inline void make_root(int x, int c)
{
if(x ^ f[x]) make_root(f[x], x);
f[x] = c;
} int main()
{
int i, j, x, y, fx, fy;
char s[];
n = read();
m = read();
for(i = ; i <= n; i++) f[i] = i;
for(i = ; i <= m; i++)
{
scanf("%s", s);
x = read();
y = read();
if(s[] == 'Q') find(x) == find(y) ? puts("Yes") : puts("No");
else if(s[] == 'C') make_root(x, x), f[x] = y;
else make_root(x, x), f[y] = y;
}
return ;
}

2.lct(正解)

就是模板啦

——代码

 #include <cstdio>
#include <iostream>
#define N 10001
#define swap(x, y) ((x) ^= (y) ^= (x) ^= (y)) int n, m;
int f[N], rev[N], son[N][], s[N], size[N]; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline bool isroot(int x)
{
return son[f[x]][] ^ x && son[f[x]][] ^ x;
} inline int get(int x)
{
return son[f[x]][] == x;
} inline void pushdown(int x)
{
if(x && rev[x])
{
swap(son[x][], son[x][]);
if(son[x][]) rev[son[x][]] ^= ;
if(son[x][]) rev[son[x][]] ^= ;
rev[x] = ;
}
} inline void rotate(int x)
{
int old = f[x], oldf = f[old], wh = get(x); if(!isroot(old))
son[oldf][son[oldf][] == old] = x;
f[x] = oldf; son[old][wh] = son[x][wh ^ ];
f[son[old][wh]] = old; son[x][wh ^ ] = old;
f[old] = x;
} inline void splay(int x)
{
int i, fa, t = ;
s[++t] = x;
for(i = x; !isroot(i); i = f[i]) s[++t] = f[i];
for(i = t; i >= ; i--) pushdown(s[i]);
for(; !isroot(x); rotate(x))
if(!isroot(fa = f[x]))
rotate(get(x) == get(fa) ? fa : x);
} inline void access(int x)
{
for(int t = ; x; t = x, x = f[x]) splay(x), son[x][] = t;
} inline void reverse(int x)
{
access(x);
splay(x);
rev[x] ^= ;
} inline int find(int x)
{
access(x);
splay(x);
while(son[x][]) x = son[x][];
return x;
} inline void link(int x, int y)
{
reverse(x);
f[x] = y;
splay(x);
} inline void cut(int x, int y)
{
reverse(x);
access(y);
splay(y);
son[y][] = f[x] = ;
} int main()
{
int i, j, x, y;
char s[];
n = read();
m = read();
for(i = ; i <= m; i++)
{
scanf("%s", s);
x = read();
y = read();
if(s[] == 'Q') find(x) == find(y) ? puts("Yes") : puts("No");
if(s[] == 'C') link(x, y);
if(s[] == 'D') cut(x, y);
}
return ;
}

[luoguP2147] [SDOI2008]Cave 洞穴勘测(并查集 || lct)的更多相关文章

  1. 【bzoj2049】[Sdoi2008]Cave 洞穴勘测——线段树上bfs求可撤销并查集

    题面 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 12030 Solved: 6024 Desc ...

  2. 【bzoj2049】[Sdoi2008]Cave 洞穴勘测 link-cut-tree

    2016-05-30  11:04:51 学习了link-cut-tree 二中神犇封禹的讲义感觉讲的超级清晰易懂啊(没有的可以q窝 算是模板吧 #include<bits/stdc++.h&g ...

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

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

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

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

  5. 【LCT】BZOJ2049 [SDOI2008]Cave 洞穴勘测

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

  6. BZOJ_2049_[Sdoi2008]Cave 洞穴勘测_LCT

    BZOJ_2049_[Sdoi2008]Cave 洞穴勘测_LCT Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由 ...

  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. 2049: [Sdoi2008]Cave 洞穴勘测

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MB Submit: 7475  Solved: 3499 [Submi ...

随机推荐

  1. UVA12906 Maximum Score (组合)

    对于每个元素,最理想的情况就是都在它的左边或者右边,那么sort一下就可以得到一个特解了,然后大的中间不能有小的元素,因为如果有的话,那么无论选小的还是选大的都不是最优.对小的元素来说,比它大的元素在 ...

  2. kubernetes-核心概念及创建应用(六)

    kubernetes是什么: •Kubernetes是Google在2014年开源的一个容器集群管理系统,Kubernetes简称K8S.•K8S用于容器化应用程序的部署,扩展和管理.•K8S提供了容 ...

  3. CPP-网络/通信:经典HTTP协议详解

    2008-11-03 09:11 by Hundre, 266688 阅读, 23 评论, 收藏, 编辑 转自:http://blog.csdn.net/gueter/archive/2007/03/ ...

  4. centos Chrony设置服务器集群同步时间

    Chrony是一个开源的自由软件,像CentOS 7或基于RHEL 7操作系统,已经是默认服务,默认配置文件在 /etc/chrony.conf 它能保持系统时间与时间服务器(NTP)同步,让时间始终 ...

  5. Java删除开头和末尾字符串

    //扩展2个String方法 /* * 删除开头字符串 */ public static String trimstart(String inStr, String prefix) { if (inS ...

  6. B1002 写出这个数

    读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 1. 输出格式: 在一行内输出 n 的 ...

  7. [vijos]P1514 天才的记忆

    背景 神仙飞啊飞 描述 从前有个人名叫W and N and B,他有着天才般的记忆力,他珍藏了许多许多的宝藏.在他离世之后留给后人一个难题(专门考验记忆力的啊!),如果谁能轻松回答出这个问题,便可以 ...

  8. destoon 后台管理左侧新增菜单项

    destoon 后台菜单设置在对应模块的admin/menu.inc.php 例如要在后台会员管理里增加会员承包和股东管理 $menu = array( array('添加会员', '?modulei ...

  9. Altium Designer入门学习笔记1.软件安装与资料收集

    一.软件安装 微信:http://url.cn/5Eudzt9 关注微信公众号"软件安装管家",点击"软件目录",弹出"软件目录",点击进入 ...

  10. UIAutomator输入中文

    之前一直是英文的测试环境,包括手机也是英文的,app也是英文的,涉及不到中文输入法的东西.但现在在写中文的app,所以需要输入中文.看到网上的解决办法如下: 下载https://github.com/ ...