沉迷Link-Cut tree无法自拔之:[BZOJ2049]洞穴勘探(蒟蒻的LCT板子)
来自蒟蒻 \(Hero \_of \_Someone\) 的 \(LCT\) 学习笔记
最近学了一波 \(LCT\) , 于是怒刷 \(LCT\) 合集......
$
$
学的时候借鉴了 Clove_unique的博客 以及 PoPoQQQ的PPT
写得很详细,初学者可以去看看...
$
$
先甩一道板子题...
$
$
辣鸡得不能再辣鸡的题...只有 \(cut\) 和 \(link\) 操作, 然后就是判连通性...
$
$
//made by Hero_of_Someone
#include<iostream>
#include<cstdio>
#include<cstdlib>
#define N (10010)
#define RG register
using namespace std;
inline int gi(){ RG int x=0,q=1; RG char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if(ch=='-') q=-1,ch=getchar(); while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=getchar(); return q*x; }
int n,m,ch[N][2],fa[N],rev[N];
inline void down(int x){
if(!rev[x]) return ;
rev[ch[x][0]]^=1;
rev[ch[x][1]]^=1;
swap(ch[x][0],ch[x][1]);
rev[x]=0;
}
inline bool is_root(int x){ return ch[fa[x]][0]!=x && x!=ch[fa[x]][1]; }
inline int lr(int x){ return x==ch[fa[x]][1]; }
inline void rotate(int x){
RG int y=fa[x],z=fa[y],k=lr(x);
if(!is_root(y)) ch[z][lr(y)]=x;
fa[x]=z; fa[ch[x][k^1]]=y; fa[y]=x;
ch[y][k]=ch[x][k^1]; ch[x][k^1]=y;
}
int st[N];
inline void splay(int x){
RG int y=x,top=0;
while(1){
st[++top]=y;
if(is_root(y)) break;
y=fa[y];
}
for(RG int i=top;i;i--) down(st[i]);
while(!is_root(x)){
if(!is_root(fa[x])) rotate(lr(x)^lr(fa[x])?x:fa[x]);
rotate(x);
}
}
inline void access(int x){
RG int y=0;
while(x){ splay(x);
ch[x][1]=y; fa[y]=x;
y=x; x=fa[x];
}
}
inline void make_root(int x){
access(x); splay(x); rev[x]^=1;
}
inline int find(int x){
access(x); splay(x);
while(ch[x][0]) x=ch[x][0];
return x;
}
inline void link(int x,int y){
if(find(x)==find(y)) return ;
make_root(x); fa[x]=y;
}
inline void cut(int x,int y){
make_root(x); access(y); splay(y);
if(ch[y][0]==x) ch[y][0]=0,fa[x]=0;
}
inline void init(){ n=gi(),m=gi(); }
inline void work(){
while(m--){
RG char s[10]; scanf("%s",s);
RG int x=gi(),y=gi();
if(s[0]=='C'){ link(x,y); continue; }
if(s[0]=='D'){ cut(x,y); continue; }
if(s[0]=='Q'){
if(find(x)!=find(y)) puts("No");
else puts("Yes");
}
}
}
int main(){ init(); work(); return 0; }
沉迷Link-Cut tree无法自拔之:[BZOJ2049]洞穴勘探(蒟蒻的LCT板子)的更多相关文章
- bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门
link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...
- link cut tree 入门
鉴于最近写bzoj还有51nod都出现写不动的现象,决定学习一波厉害的算法/数据结构. link cut tree:研究popoqqq那个神ppt. bzoj1036:维护access操作就可以了. ...
- Codeforces Round #339 (Div. 2) A. Link/Cut Tree 水题
A. Link/Cut Tree 题目连接: http://www.codeforces.com/contest/614/problem/A Description Programmer Rostis ...
- Link/cut Tree
Link/cut Tree 一棵link/cut tree是一种用以表示一个森林,一个有根树集合的数据结构.它提供以下操作: 向森林中加入一棵只有一个点的树. 将一个点及其子树从其所在的树上断开. 将 ...
- 洛谷P3690 Link Cut Tree (模板)
Link Cut Tree 刚开始写了个指针版..调了一天然后放弃了.. 最后还是学了黄学长的板子!! #include <bits/stdc++.h> #define INF 0x3f3 ...
- LCT总结——概念篇+洛谷P3690[模板]Link Cut Tree(动态树)(LCT,Splay)
为了优化体验(其实是强迫症),蒟蒻把总结拆成了两篇,方便不同学习阶段的Dalao们切换. LCT总结--应用篇戳这里 概念.性质简述 首先介绍一下链剖分的概念(感谢laofu的讲课) 链剖分,是指一类 ...
- P3690 【模板】Link Cut Tree (动态树)
P3690 [模板]Link Cut Tree (动态树) 认父不认子的lct 注意:不 要 把 $fa[x]$和$nrt(x)$ 混 在 一 起 ! #include<cstdio> v ...
- Link Cut Tree学习笔记
从这里开始 动态树问题和Link Cut Tree 一些定义 access操作 换根操作 link和cut操作 时间复杂度证明 Link Cut Tree维护链上信息 Link Cut Tree维护子 ...
- [CodeForces - 614A] A - Link/Cut Tree
A - Link/Cut Tree Programmer Rostislav got seriously interested in the Link/Cut Tree data structure, ...
随机推荐
- LiveCharts文档-3开始-8自定义工具提示
原文:LiveCharts文档-3开始-8自定义工具提示 LiveCharts文档-3开始-8自定义工具提示 默认每个需要tooltip或者legend的chart都会初始化一个DefaultLeng ...
- Ionic Android项目Splash设置
ionic项目中,在splashscreen消失后会出现零点几秒的白屏,再出现app页面. 1. 安装Cordova splash screen插件 ionic plugin add org.apac ...
- sklearn学习笔记之简单线性回归
简单线性回归 线性回归是数据挖掘中的基础算法之一,从某种意义上来说,在学习函数的时候已经开始接触线性回归了,只不过那时候并没有涉及到误差项.线性回归的思想其实就是解一组方程,得到回归函数,不过在出现误 ...
- 通用漏洞评估方法CVSS 3.0 计算公式及说明
CVSS 3.0 计算公式及说明 一.基础评价 1. 基础评价公式为: 当 影响度分值 <= 0: 基础分值 = 0 当 0 < 影响度分值 + 可利用度分值 < 10: 作用域 = ...
- 详解javascript中this的工作原理
在 JavaScript 中 this 常常指向方法调用的对象,但有些时候并不是这样的,本文将详细解读在不同的情况下 this 的指向. 一.指向 window: 在全局中使用 this,它将会指向全 ...
- webpack教程(一)——初体验
首先全局安装webpack,再npm初始化一个项目,并局部安装webpack开发工具 $ npm install webpack -g npm init (项目名称) $ npm install we ...
- 【nodejs】让nodejs像后端mvc框架(asp.net mvc )一样处理请求--自动路由篇(1/8)【route】
文章目录 前情概要 在使用express框架开发的时候,每加一个请求,都在增加一条route请求规则,类似于下面的代码,很烦有木有! app.use('/myroute path', (req, re ...
- 在 Ionic2 TypeScript 项目中导入第三方 JS 库
原文发表于我的技术博客 本文分享了在Ionic2 TypeScript 项目中导入第三方 JS 库的方法,供参考. 原文发表于我的技术博客 1. Typings 的方式 因在 TypeScript 中 ...
- yum源使用的几个报错小总结 (例如: python2.6.6 下yum不能使用: No module named yum)
服务器上的yum突然不好使用,使用yum时有如下几个保持,解决方案如下: 1)Error: Cannot retrieve repository metadata (repomd.xml) for r ...
- Linux内核分析——Linux内核学习总结
马悦+原创作品转载请注明出处+<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 Linux内核学习总结 一 ...