bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门
link cut tree入门题
首先说明本人只会写自底向上的数组版(都说了不写指针、不写自顶向下QAQ……)
突然发现link cut tree不难写。。。
说一下各个函数作用:
bool isroot(int x):判断x是否为所在重链(splay)的根
void down(int x):下放各种标记
void rotate(int x):在x所在重链(splay)中将x旋转到fa[x]的位置上
void splay(int x):在x坐在重链(splay)中将x旋转到根
void access(int x):把x到x所在树的根变为一条重链,根为链顶,x为链底
void reverse(int x):把x变为所在树的根(先access再打上reverse标记)
void link(int x,int y)连接x,y两点
void cut(int x,int y)断开x,y两点
int find(int x)找到x所在树的根
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define rre(i,r,l) for(int i=(r);i>=(l);i--)
#define re(i,l,r) for(int i=(l);i<=(r);i++)
#define Clear(a,b) memset(a,b,sizeof(a))
#define inout(x) printf("%d",(x))
#define douin(x) scanf("%lf",&x)
#define strin(x) scanf("%s",(x))
#define LLin(x) scanf("%lld",&x)
#define op operator
#define CSC main
typedef unsigned long long ULL;
typedef const int cint;
typedef long long LL;
using namespace std;
void inin(int &ret)
{
ret=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=;ch=getchar();}
while(ch>=''&&ch<='')ret*=,ret+=ch-'',ch=getchar();
ret=f?-ret:ret;
}
int fa[],ch[][],rev[];
bool isroot(int x){return ch[fa[x]][]!=x&&ch[fa[x]][]!=x;}
void down(int x)
{
if(!rev[x])return ;
swap(ch[x][],ch[x][]);
rev[ch[x][]]^=;
rev[ch[x][]]^=;
rev[x]=;
}
void rotate(int x)
{
int y=fa[x],z=fa[y];
if(!isroot(y))
if(ch[z][]==y)ch[z][]=x;
else ch[z][]=x;else ;
fa[x]=z,fa[y]=x;
int d=ch[y][]==x;
fa[ch[x][d^]]=y;
ch[y][d]=ch[x][d^];
ch[x][d^]=y;
}
int sta[],top;
void splay(int x)
{
top=;sta[++top]=x;
for(int i=x;!isroot(i);i=fa[i])sta[++top]=fa[i];
while(top)down(sta[top--]);
while(!isroot(x))
{
int y=fa[x],z=fa[y];
if(!isroot(y))
if((ch[y][]==x)^(ch[z][]==y))rotate(x);
else rotate(y);else
rotate(x);
}
}
void access(int x)
{
int temp=;
while(x)
{
splay(x);
ch[x][]=temp;
temp=x,x=fa[x];
}
}
void reverse(int x)
{
access(x);splay(x),rev[x]^=;
}
void link(int x,int y)
{
reverse(x);fa[x]=y,splay(x);
}
void cut(int x,int y)
{
reverse(x);access(y);splay(y);ch[y][]=fa[x]=;
}
int find(int x)
{
access(x),splay(x);
int y=x;
while(ch[y][])y=ch[y][];
return y;
}
int n,m;
int main()
{
char s[];
inin(n),inin(m);
re(i,,m)
{
strin(s);
int x,y;inin(x),inin(y);
if(s[]=='C')link(x,y);
else if(s[]=='D')cut(x,y);
else
{
if(find(x)==find(y))printf("Yes\n");
else printf("No\n");
}
}
return ;
}
bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门的更多相关文章
- 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 ...
- [BZOJ2049][Sdoi2008]Cave 洞穴勘测 LCT模板
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 9705 Solved: 4674[Submit] ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (动态树入门)
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1528 Solved: 644[Submit][ ...
- [bzoj2049][Sdoi2008]Cave 洞穴勘测_LCT
Cave 洞穴勘测 bzoj-2049 Sdoi-2008 题目大意:维护一个数据结构,支持森林中加边,删边,求两点连通性.n个点,m个操作. 注释:$1\le n\le 10^4$,$1\le m\ ...
- [BZOJ2049] [SDOI2008] Cave 洞穴勘测 (LCT)
Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...
- [bzoj2049][Sdoi2008]Cave 洞穴勘测——lct
Brief Description 给定一个森林,您需要支持两种操作: 链接两个节点. 断开两个节点之间的链接. Algorithm Design 对于树上的操作,我们现在已经有了树链剖分可以处理这些 ...
- BZOJ2049: [Sdoi2008]Cave 洞穴勘测 Link-Cut-Tree 模板题
传送门 搞了这么长时间Splay终于可以搞LCT了,等等,什么是LCT? $LCT$就是$Link-Cut-Tree$,是维护动态树的一个很高效的数据结构,每次修改和查询的均摊复杂度为$O(logN) ...
- BZOJ2049——[Sdoi2008]Cave 洞穴勘测
1.题目大意:就是一个动态维护森林联通性的题 2.分析:lct模板题 #include <stack> #include <cstdio> #include <cstdl ...
随机推荐
- what's the python之字符编码与文件处理
用文本编辑器打开一个文件就是把一个文件读入了内存中 ,所以打开文件的操作也是在内存中的,断电即消失,所以若要保存其内容就必须点击保存让其存入硬盘中 python解释器执行py文件的原理 : 第一阶段: ...
- Linux系统启动排错实验集合
Centos6系统启动流程 1. post 加电自检 检查硬件环境 2. 选择一个硬件类型引导启动 mbr 446字节 grub stage1 3. 加载boot分区的文 ...
- 并发编程---线程---开启方式---进程线程的区别----Thread的其他属性
线程 进程只是用来把资源集中到一起(进程只是一个资源单位,或者说资源集合)而线程才是cpu上的执行单位 1.同一个进程内的多个线程共享该进程内的地址资源 2.创建线程的开销远小于创建进程的开销(创建一 ...
- Windows XP解决显示桌面图标消失的问题
1.依次单击“开始”,“运行”,在运行对话框中输入regsvr32 /n /i:u shell32.dll,按回车键 2.弹出“shell32.dll中的DllInstall成功”的窗口,表明已将数据 ...
- eclipse 假死
由于电脑关机,导致Eclipse非正常关闭,之后启动Eclipse发现一直启动不起来,于是从网上找了一些方法如下: 1.删除文件.snap 到<workspace>\.metadata\. ...
- 死锁与递归锁 信号量 event 线程queue
1.死锁现象与递归锁 死锁:是指两个或两个以上的进程或线程在执行过程中,因争抢资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去,此时称系统处于死锁状态或系统产生了死锁,这些永远在互相 ...
- [py]软件编程知识骨架+py常见数据结构
认识算法的重要性 - 遇到问题? 学完语言,接到需求,没思路? 1.学会了语言,能读懂别人的代码, 但是自己没解决问题的能力,不能够把实际问题转换为代码,自己写出来.(这是只是学会一门语言的后果),不 ...
- Python 全栈开发六 常用模块学习
本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve configparser hashlib 一. ...
- spring + mybatis配置及网络异常设置
Spring引入mybatis <beans xmlns="http://www.springframework.org/schema/beans" xmlns:contex ...
- Case when then esle end
Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex ' THEN '男' ' THEN '女' ELSE '其他' END --Case搜索函数 ' T ...