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] ...
随机推荐
- 学习BOS物流项目第十天
1 教学计划 1.演示权限demo 2.权限概述 a. 认证 b. 授权 3.常见的权限控制方式 a. url拦截权限控制 b. 方法注解权限控制 4.创建权限数据模型 a. 权限表 b. 角 ...
- Python Try Except
Python Try: Except Except 类型一: try: file_size = os.path.getsize('maoyan.csv'); except OSError as err ...
- quast-lg
1.官网简介 http://cab.spbu.ru/software/quast-lg/ QUAST- lg是QUAST的一个扩展,用于评估大型基因组装配(直至哺乳动物大小).QUAST- lg从5. ...
- @JsonInclude(JsonInclude.Include.NON_NULL) 加在对象上
@JsonInclude(JsonInclude.Include.NON_NULL) public class ViewWorkermessage implements Serializable { ...
- Jenkins-cli基本用法
基本的格式为 java -jar jenkins-cli.jar [-s JENKINS_URL] command [options][args] 下面具体介绍各个命令的作用及基本使用方法 1. ...
- numpy 之矩阵的认知
di numpy 矩阵的创建与应用 可以用np.mat(a) 将a转变成矩阵 矩阵的加减法和 array相同 矩阵的乘法,如果矩阵要相乘的话就要A矩阵的行数,和B矩阵的列数相同才可以 这是查看数组不重 ...
- TOJ3097: 单词后缀 (字典树 or map瞎搞)
传送门 (<---可以点击的~) 时间限制(普通/Java):1000MS/3000MS 内存限制:65536KByte 描述 有些英语单词后缀都是一样的,现在我们需要从给定的一堆单词里 ...
- WdatePicker控件中日期的范围选择
1.开始日期不能大于结束日期,结束日期只能选择今天之前(不包括今天) <asp:TextBox ID="T_CREATION_DATE_Start" Width=" ...
- VCSA 6.5, 初始化设置root密码失败can't set root password 或 安装时卡在80%
是因为下载的非官方的包密码过期了,如果是Windows引导安装: 安装完虚拟机的时候就马上执行下面步骤,修改密码有效期即可. 重启vcsa,在引导界面(photon的图形界面)里按e编辑启动项 在co ...
- vue-app项目,将px自动转化为rem
1. 安装lib-flexible: npm install --save lib-flexible 2.安装postcss-loader和postcss-px2rem: npm install -- ...