Luogu 2147 洞穴勘测 - LCT
Solution
$LCT$ 打上 $cut$ , $link$ 和 $finroot$ 即可
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rd read()
using namespace std; const int N = 1e4 + ; int n, m; int read() {
int X = , p = ; char c = getchar();
for (; c > '' || c < ''; c = getchar())
if (c == '-') p = -;
for (; c >= '' && c <= ''; c = getchar())
X = X * + c - '';
return X * p;
} namespace LCT {
int f[N], ch[N][], tun[N];
#define lc(x) ch[x][0]
#define rc(x) ch[x][1] int isroot(int x) {
return rc(f[x]) != x && lc(f[x]) != x;
} int get(int x) {
return rc(f[x]) == x;
} void rev(int x) {
swap(lc(x), rc(x));
tun[x] ^= ;
} void pushdown(int x) {
if (tun[x]) {
if (lc(x)) rev(lc(x));
if (rc(x)) rev(rc(x));
tun[x] = ;
}
} void pd(int x) {
if (!isroot(x))
pd(f[x]);
pushdown(x);
} void rotate(int x) {
int old = f[x], oldf = f[old], son = ch[x][get(x) ^ ];
if (!isroot(old)) ch[oldf][get(old)] = x;
ch[x][get(x) ^ ] = old;
ch[old][get(x)] = son;
f[old] = x; f[x] = oldf; f[son] = old;
} void splay(int x) {
pd(x);
for (; !isroot(x); rotate(x))
if (!isroot(f[x]))
rotate(get(f[x]) == get(x) ? f[x] : x);
} void access(int x) {
for (int y = ; x; y = x, x = f[x])
splay(x), ch[x][] = y;
} void mroot(int x) {
access(x); splay(x); rev(x);
} int findr(int x) {
access(x); splay(x);
while(lc(x)) pushdown(x), x = lc(x);
return x;
} void split(int x, int y) {
mroot(x); access(y); splay(y);
} void cut(int x, int y) {
split(x, y);
f[x] = ch[y][] = ;
} void link(int x, int y) {
mroot(x);
f[x] = y;
}
}using namespace LCT; int main()
{
n = rd; m = rd;
for (; m; m--) {
char op[];
scanf("%s", op);
if (op[] == 'Q') {
int u = rd, v = rd;
mroot(u);
if (findr(v) != u)
puts("No");
else puts("Yes");
}
if (op[] == 'C') {
int u = rd, v = rd;
link(u, v);
}
if (op[] == 'D') {
int u = rd, v = rd;
cut(u, v);
}
}
}
Luogu 2147 洞穴勘测 - LCT的更多相关文章
- 【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)以及若干通道组成,并且每条通道连接了恰好 ...
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2049 题面: 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 ...
- BZOJ2049[Sdoi2008]洞穴勘测——LCT
题目描述 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假如 ...
- 洛谷P2147[SDOI2008]洞穴勘测(lct)
题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假 ...
- 【bzoj2049】[Sdoi2008]Cave 洞穴勘测 LCT
题目描述 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假如 ...
- 【BZOJ2049】【SDOI2008】洞穴勘测 [LCT]
洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MB[Submit][Status][Discuss] Description 辉辉热衷于洞穴勘测.某天,他按照地 ...
- 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] ...
随机推荐
- property 、classmethod 、 staticmethod 的用法
@property # property是一个装饰器函数 ,作用:将一个方法伪装成属性 # 所有的装饰器函数都怎么用? 在函数.方法.类的上面一行直接@装饰器的名字 # 装饰器的分类: # 装饰函数 ...
- ADO.Net 数据库查询
数据库中的表: VS查询代码: using System; using System.Collections.Generic; using System.Linq; using System.Text ...
- 编译模块的Makefile解析
Makefile # if not defined KERNELRELEASE, command is running from command line,need invoke kbuild sys ...
- rem是如何实现自适应布局的
原文链接:http://caibaojian.com/web-app-rem.html 摘要:rem是相对于根元素<html>,这样就意味着,我们只需要在根元素确定一个px字号,则可以来算 ...
- 配置 redis 外网访问
redis采用的安全策略,默认会只准许本地访问 1 2 3 4 5 6 7 8 9 10 通过简单配置,完成允许外网访问 [root@cache01 conf]# egrep "(^bind ...
- Dictionary在多线程情况下
Add时出错 错误信息: Index was outside the bounds of the array. 详细信息: at System.Collections.Generic.Dictiona ...
- SQL Server 2008中SQL增强之一:Values新用途 001
连接集合 select '1' as id,'wang' as name union select '2' as id,'admin' as name 现在可以这么写了 select id,name ...
- JMeter学习(三十七)Jmeter录制手机app脚本(转载)
转载自 http://www.cnblogs.com/yangxia-test 环境准备: 1.手机 2.wifi 3.Jmeter 具体步骤: 1.启动Jmeter: 2.“测试计划”中添加“线 ...
- 新版本PHP使用更方便了
$hostname_conn = "";$database_conn = "";$username_conn = "root";$passw ...
- sqoop2问题解决
sqoop:000> show version --serverException has occurred during processing command Exception: org.a ...