[bzoj2049][Sdoi2008]Cave 洞穴勘测——lct
Brief Description
给定一个森林,您需要支持两种操作:
- 链接两个节点。
- 断开两个节点之间的链接。
Algorithm Design
对于树上的操作,我们现在已经有了树链剖分可以处理这些问题。然而树链剖分不支持动态维护树上的拓扑结构。所以我们需要Link-Cut Tree(lct)来解决这种动态树问题。顺带一提的是,动态树也是Tarjan发明的。
首先我们介绍一个概念:Preferred path(实边),其他的边都是虚边。我们使用splay来实时地维护这条路径。
lct的核心操作是access。access操作可以把虚边变为实边,通过改变splay的拓扑结构来维护实边。
有了这个数据结构,我们依次来考虑两个操作。
对于链接两个节点,我们需要首先把x节点变为他所在树的根节点,然后直接令fa[x] = y即可。
怎样换根呢?稍微思考一下可以发现,我们直接把从根到他的路径反转即可。
对于第二种操作,我们直接断开拓扑关系即可。
另外实现的时候要注意,splay的根节点的父亲是他的上一个节点。所以zig和splay的写法应该格外注意。
Code
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <stack>
using std::stack;
const int maxn = 10005;
inline int read() {
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-')
f = -1;
ch = getchar();
}
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
int n, m;
int fa[maxn], ch[maxn][2];
bool rev[maxn];
inline bool isroot(int x) { return ch[fa[x]][0] != x && ch[fa[x]][1] != x; }
void pushdown(int k) {
if (rev[k]) {
rev[k] = 0;
rev[ch[k][0]] ^= 1;
rev[ch[k][1]] ^= 1;
std::swap(ch[k][0], ch[k][1]);
}
}
void zig(int x) {
int y = fa[x], z = fa[y], l = (ch[y][1] == x), r = l ^ 1;
if (!isroot(y))
ch[z][ch[z][1] == y] = x;
fa[ch[y][l] = ch[x][r]] = y;
fa[ch[x][r] = y] = x;
fa[x] = z;
}
void splay(int x) {
stack<int> st;
st.push(x);
for (int i = x; !isroot(i); i = fa[i])
st.push(fa[i]);
while (!st.empty()) {
pushdown(st.top());
st.pop();
}
for (int y = fa[x]; !isroot(x); zig(x), y = fa[x])
if (!isroot(y))
zig((ch[fa[y]][0] == y) == (ch[y][0] == x) ? y : x);
}
void access(int x) {
int t = 0;
while (x) {
splay(x);
ch[x][1] = t;
t = x;
x = fa[x];
}
}
void rever(int x) {
access(x);
splay(x);
rev[x] ^= 1;
}
void link(int x, int y) {
rever(x);
fa[x] = y;
splay(x);
}
void cut(int x, int y) {
rever(x);
access(y);
splay(y);
ch[y][0] = fa[x] = 0;
}
int find(int x) {
access(x);
splay(x);
int y = x;
while (ch[y][0])
y = ch[y][0];
return y;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("input", "r", stdin);
#endif
char ch[10];
int x, y;
n = read(), m = read();
for (int i = 1; i <= m; i++) {
scanf("%s", ch);
x = read();
y = read();
if (ch[0] == 'C')
link(x, y);
else if (ch[0] == 'D')
cut(x, y);
else {
if (find(x) == find(y))
printf("Yes\n");
else
printf("No\n");
}
}
}
[bzoj2049][Sdoi2008]Cave 洞穴勘测——lct的更多相关文章
- [BZOJ2049][Sdoi2008]Cave 洞穴勘测 LCT模板
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 9705 Solved: 4674[Submit] ...
- [BZOJ2049] [SDOI2008] Cave 洞穴勘测 (LCT)
Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...
- bzoj2049: [Sdoi2008]Cave 洞穴勘测 lct裸题
题意:三种操作一种摧毁一条边,一种链接一条边,一种查询两个点是否联通 题解:lct的link和cut即可 /********************************************** ...
- BZOJ2049 SDOI2008 Cave 洞穴勘测 【LCT】
BZOJ2049 SDOI2008 Cave 洞穴勘测 Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分 ...
- 【LCT】BZOJ2049 [SDOI2008]Cave 洞穴勘测
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 10059 Solved: 4863[Submit ...
- 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
Cave 洞穴勘测 bzoj-2049 Sdoi-2008 题目大意:维护一个数据结构,支持森林中加边,删边,求两点连通性.n个点,m个操作. 注释:$1\le n\le 10^4$,$1\le m\ ...
- 【BZOJ2049】 [Sdoi2008]Cave 洞穴勘测 LCT/并查集
两种方法: 1.LCT 第一次LCT,只有link-cut和询问,无限T,到COGS上找了数据,发现splay里的父亲特判出错了(MD纸张),A了,好奇的删了反转T了.... #include < ...
随机推荐
- [转][赞]Android开发者必知的开发资源
英文原文:Bongzimo 翻译: ImportNew-黄小非 随着Android平台市场份额的持续猛增 ,越来越多的开发者开始投入Android应用程序的开发大潮.如果您是一位2013年刚刚入行的 ...
- Loadrunner11.0安装与简单使用
公司开发了APP或者微信小程序啊什么的,都会先进行性能测试,而性能测试一般肯定会来测试接口的压测,并发.Loadrunner是一个很强大的测试工具,它是一种预测系统行为和性能的负载测试工具.通过以模拟 ...
- JS运行在服务器端注意事项
<script runat="server" language="javascript"> </script> 1. ASP利于JS重载 ...
- 《python机器学习—预测分析核心算法》:构建预测模型的一般流程
参见原书1.5节 构建预测模型的一般流程 问题的日常语言表述->问题的数学语言重述重述问题.提取特征.训练算法.评估算法 熟悉不同算法的输入数据结构:1.提取或组合预测所需的特征2.设定训练目标 ...
- %matplotlib inline
整理摘自 https://zhidao.baidu.com/question/1387744870700677180.html %matplotlib inline是jupyter notebook里 ...
- 九度OJ--1167(C++)
#include <iostream>#include <algorithm>#include <map> using namespace std; int mai ...
- memcached的认识
<?php /* memcached概念: Memcached是一个免费开源的,高性能的,具有分布式对象的缓存系统,它可以用来保存一些经常存取的对象或数据,保存的数据像一张巨大的HASH表,该表 ...
- [译]如何去除Git的unstaged的文件提示“old mode 100755 new mode 100644”?
原文来源:https://stackoverflow.com/questions/1257592/how-do-i-remove-files-saying-old-mode-100755-new-mo ...
- 关于<!DOCTYPE html>的学习(转)
DOCTYPE是对Document type的缩写,说明用XHTML或者HTML是什么版本的.必须出现在<html>标签的前面,不需要关闭标签. <!DOCTYPE>声明不是标 ...
- 物联网PPT智能家居王思齐和陈由钧第10组
ppt做完了但是不知道怎么用博客园发ppt!只能发几个图片了