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 ...
随机推荐
- Java基础知识(JAVA基本数据类型包装类)
基本数据类型的包装类 为什么需要包装类? Java并不是纯面向对象的语言.Java语言是一个面向对象的语言,但是Java的基本数据类型却不是面向对象的.但是我们在实际使用中经常需要将基本数据转化成对象 ...
- 009-Iterator 和 for...of 循环
一.概述 原文地址:http://es6.ruanyifeng.com/#docs/iterator 二.详细 2.1.概念 遍历器(Iterator)是一种机制.它是一种接口,为各种不同的数据结构提 ...
- select 数字/字符串/count(参数)/sum(数字) from table
第一种的写法是增加临时列,每行的列值是写在select后的数: --1select 1 from W_EC_PLACESTATION_COLLECT t--2select 2 from W_EC_PL ...
- abp中linq的应用
private IQueryable<MembershipEntity> SelectOrScrrenMember(GetMemberInput input) { string[] use ...
- vue中indexDB的应用
// indexedDB.js,浏览器本地数据库操作 export default { // indexedDB兼容 indexedDB: window.indexedDB || window.web ...
- PHPstorm破解方法及xdebug的断点调试
原文地址:http://www.php.cn/php-weizijiaocheng-381903.html 相信用PHPstorm的程序员肯定很多,令人头疼的是下载的PHPstorm是有使用期限的,小 ...
- PHP策略模式2
<?php /** PHP 策略模式 * 策略模式是对象的行为模式,用意是对一组算法的封装.动态的选择需要的算法并使用. * 策略模式指的是程序中涉及决策控制的一种模式.策略模式功能非常强大,因 ...
- [django]django配合前端vue前后端联调,django服务端解决跨域(django-cors-headers)
django内部csrf post提交数据解决 https://www.cnblogs.com/iiiiiher/articles/9164940.html 前端写了个页面,里面$.post发现403 ...
- sklearn.svm.LinearSVC文档学习
https://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.html#sklearn.svm.LinearSVC 1 ...
- python中的filter、map、reduce、apply用法
1. filter 功能: filter的功能是过滤掉序列中不符合函数条件的元素,当序列中要删减的元素可以用某些函数描述时,就应该想起filter函数. 调用: filter(function,seq ...