BZOJ 2049 洞穴勘测
LCT判断联通性
没什么特别的。。还是一个普通的板子题,把LCT当并查集用了,只不过LCT灵活一些,还可以断边
话说自从昨天被维修数列那题榨干之后我现在写splay都不用动脑子了,,机械式的码splay23333
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
int X = 0, w = 0; char ch = 0;
while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
A ans = 1;
for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
return ans;
}
const int N = 10005;
int n, m, tot, ch[N][2], fa[N], rev[N], st[N];
int newNode(){
fa[++tot] = 0; ch[tot][0] = ch[tot][1] = 0;
return tot;
}
bool isRoot(int x){
return ch[fa[x]][0] != x && ch[fa[x]][1] != x;
}
void reverse(int x){
rev[x] ^= 1;
swap(ch[x][0], ch[x][1]);
}
void push_down(int x){
if(rev[x]){
rev[x] ^= 1;
reverse(ch[x][0]), reverse(ch[x][1]);
}
}
void rotate(int x){
int y = fa[x], z = fa[y], p = (ch[y][1] == x) ^ 1;
push_down(y), push_down(x);
ch[y][p^1] = ch[x][p], fa[ch[x][p]] = y;
if(!isRoot(y)) ch[z][ch[z][1] == y] = x;
fa[x] = z, fa[y] = x, ch[x][p] = y;
}
void splay(int x){
int pos = 0; st[++pos] = x;
for(int i = x; !isRoot(i); i = fa[i]) st[++pos] = fa[i];
while(pos) push_down(st[pos--]);
while(!isRoot(x)){
int y = fa[x], z = fa[y];
if(!isRoot(y)){
if((ch[y][0] == x) ^ (ch[z][0] == y)) rotate(x);
else rotate(y);
}
rotate(x);
}
}
void access(int x){
for(int p = 0; x; p = x, x = fa[x])
splay(x), ch[x][1] = p;
}
void makeRoot(int x){
access(x), splay(x), reverse(x);
}
int findRoot(int x){
access(x), splay(x);
while(ch[x][0]) x = ch[x][0];
return x;
}
void link(int x, int y){
makeRoot(x);
if(findRoot(y) != x) fa[x] = y;
}
void cut(int x, int y){
makeRoot(x), access(y), splay(y);
fa[x] = ch[y][0] = 0;
}
bool isConnect(int x, int y){
return findRoot(x) == findRoot(y);
}
int main(){
int n = read(), m = read();
for(int i = 1; i <= n; i ++) newNode();
while(m --){
char opt[20]; scanf("%s", opt);
int x = read(), y = read();
if(opt[0] == 'Q') printf(isConnect(x, y) ? "Yes\n" : "No\n");
else if(opt[0] == 'C') link(x, y);
else if(opt[0] == 'D') cut(x, y);
}
return 0;
}
BZOJ 2049 洞穴勘测的更多相关文章
- BZOJ 2049洞穴探测
辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假如两个洞穴可 ...
- [bzoj] 2049 洞穴勘探 || LCT
原题 这是一道LCT的板子题. 至于LCT--link cut tree,也叫动态树,用splay实现动态连边的树. 预备知识: 实边:一个非叶节点,向它的儿子中的一个连一条特殊的边,称为实边;该非叶 ...
- 【BZOJ】2049: [Sdoi2008]Cave 洞穴勘测(lct/并查集)
http://www.lydsy.com/JudgeOnline/problem.php?id=2049 bzoj挂了..在wikioi提交,,1A-写lct的速度越来越快了-都不用debug-- 新 ...
- 【BZOJ】【2049】【SDOI2008】洞穴勘测 Cave
LCT 哦……LCT的一道更水的裸题,适合学习access,link,cut等基本操作(其实这三个不是在一个层面上的?不要在意这些细节……) /**************************** ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 LCT
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测 动态树
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 3119 Solved: 1399[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 洞穴勘测 (动态树入门)
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1528 Solved: 644[Submit][ ...
- [BZOJ 2049] [Sdoi2008] Cave 洞穴勘测 【LCT】
题目链接:BZOJ - 2049 题目分析 LCT的基本模型,包括 Link ,Cut 操作和判断两个点是否在同一棵树内. Link(x, y) : Make_Root(x); Splay(x); F ...
随机推荐
- github/gitlab同时管理多个ssh key
之前一直用github,但是github有一个不好的地方,要是创建私有的项目的话需要付费,而gitlab上则可以免费创建管理私有的项目.由于最近想把自己论文的一些东西整理一下,很多东西还是不方便公开, ...
- 2198: 小P当志愿者送餐
题目描述 在ICPC程序设计大赛期间,小P作为志愿者的任务是给各个学校送盒饭,小P一次最多可以携带M份盒饭.总共有N个学校来参加比赛,这N个学校的休息点在一条笔直的马路边一字排开,路的一头是小P取盒饭 ...
- 16-使用Selenium模拟浏览器抓取淘宝商品美食信息
淘宝由于含有很多请求参数和加密参数,如果直接分析ajax会非常繁琐,selenium自动化测试工具可以驱动浏览器自动完成一些操作,如模拟点击.输入.下拉等,这样我们只需要关心操作而不需要关心后台发生了 ...
- 解决object at 0x01DB75F0
python在学习过程中吗,由于常常会出现代码运行没报错,但输出的却不是我们想要的结果(图表,列表等等),而出现类似 <filter object at 0x01DB75F0>的情况,比如 ...
- PHP开发web应用安全总结
XSS跨站脚本 概念:恶意攻击者往Web页面里插入恶意html代码,当用户浏览该页之时,嵌入其中Web里面的html代码会被执行,从而达到恶意用户的特殊目的. 危害: 盗取用户COOKIE信息. 跳转 ...
- SQL性能优化-order by语句的优化
原文:http://bbs.landingbj.com/t-0-243203-1.html 在某些情况中,MySQL可以使用一个索引来满足ORDER BY子句,而不需要额外的排序.where条件和or ...
- laravel服务容器
laravel框架底层解析 本文参考陈昊<Laravel框架关键技术解析>,搭建一个属于自己的简化版服务容器.其中涉及到反射.自动加载,还是需要去了解一下. laravel服务容器 建立项 ...
- lumen 5.6 设置APP_KEY为32位长的随机字符串
在 App\Console\Commands下 添加以下内容的KeyGenerateCommand.php文件 <?php namespace App\Console\Commands; use ...
- Git-用 cherry-pick 挑好看的小樱桃
版权声明:本文为博主原创文章,转载请在文章明显位置标明文章原属哦. https://blog.csdn.net/qq_32452623/article/details/79449534 ti ...
- java lang(Thread) 和 Runable接口
public interface Runnable { public abstract void run(); } public class Thread implements Runnable { ...