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入门的更多相关文章

  1. BZOJ2049 SDOI2008 Cave 洞穴勘测 【LCT】

    BZOJ2049 SDOI2008 Cave 洞穴勘测 Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分 ...

  2. 【LCT】BZOJ2049 [SDOI2008]Cave 洞穴勘测

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 10059  Solved: 4863[Submit ...

  3. [BZOJ2049][Sdoi2008]Cave 洞穴勘测 LCT模板

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 9705  Solved: 4674[Submit] ...

  4. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (动态树入门)

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1528  Solved: 644[Submit][ ...

  5. [bzoj2049][Sdoi2008]Cave 洞穴勘测_LCT

    Cave 洞穴勘测 bzoj-2049 Sdoi-2008 题目大意:维护一个数据结构,支持森林中加边,删边,求两点连通性.n个点,m个操作. 注释:$1\le n\le 10^4$,$1\le m\ ...

  6. [BZOJ2049] [SDOI2008] Cave 洞穴勘测 (LCT)

    Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...

  7. [bzoj2049][Sdoi2008]Cave 洞穴勘测——lct

    Brief Description 给定一个森林,您需要支持两种操作: 链接两个节点. 断开两个节点之间的链接. Algorithm Design 对于树上的操作,我们现在已经有了树链剖分可以处理这些 ...

  8. BZOJ2049: [Sdoi2008]Cave 洞穴勘测 Link-Cut-Tree 模板题

    传送门 搞了这么长时间Splay终于可以搞LCT了,等等,什么是LCT? $LCT$就是$Link-Cut-Tree$,是维护动态树的一个很高效的数据结构,每次修改和查询的均摊复杂度为$O(logN) ...

  9. BZOJ2049——[Sdoi2008]Cave 洞穴勘测

    1.题目大意:就是一个动态维护森林联通性的题 2.分析:lct模板题 #include <stack> #include <cstdio> #include <cstdl ...

随机推荐

  1. 火币网API文档——REST API 签名认证

    安全认证 目前关于apikey申请和修改,请在“账户 - API管理”页面进行相关操作.其中AccessKey为API 访问密钥,SecretKey为用户对请求进行签名的密钥(仅申请时可见).Pro站 ...

  2. shell脚本循环和信号

    条件判断 if     条件1:then COMMAND elif  条件2:then COMMAND else COMMAND(:)        :  表示pass  不执行任何命令 fi 读取用 ...

  3. 【PCA】周志华

    一.书籍

  4. (转)MySQL排序原理与案例分析

    前言      排序是数据库中的一个基本功能,MySQL也不例外.用户通过Order by语句即能达到将指定的结果集排序的目的,其实不仅仅是Order by语句,Group by语句,Distinct ...

  5. DLNg第三周:序列模型和注意力机制

    1.基础模型 将法语翻译为英语,分为编码和解码阶段,将一个序列变为另一个序列.即序列对序列模型. 从图中识别出物体的状态,将图片转换为文字. 先使用CNN处理图片,再使用RNN将其转换为语言描述. 2 ...

  6. 重签名提示:无法对 jar 进行签名: java.util.zip.ZipException

    使用jarsigner对APK重签名,提示:jarsigner: 无法对 jar 进行签名: java.util.zip.ZipException: invalid entry compressed ...

  7. [LeetCode] 34. Find First and Last Position of Element in Sorted Array == [LintCode] 61. Search for a Range_Easy tag: Binary Search

    Description Given a sorted array of n integers, find the starting and ending position of a given tar ...

  8. android studio 编译sdk版降低报错解决方法

    解决办法如下: 步骤1:在gradle中修改 compile sdk 版本,比如 8. 步骤2:在gradle中删除v7包的依赖 步骤3:在manifest中修改theme,supportsRtl.t ...

  9. vue中检测敏感词,锚点

    当发布文章的时候,标题有敏感词 则检测有敏感词的接口成功的时候,写锚点 eg: _this .$alert("检测到标题有敏感词,请修改后再发布", "提示", ...

  10. C# And Java 3DES加解密 ECB模式/PKCS7

    c#: /// <summary>     /// 完整     /// </summary>     public class TripleDESHelper1     { ...