题面

传送门:https://www.luogu.org/problemnew/show/P2147

Solution

这题......

我们可以发现题目要求我们维护一个动态森林,而且只查询连通性....

显然LCT模板题啊,关于LCT玩法,可以猛戳这里学习

Code

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
long long read()
{
long long x=0,f=1; char c=getchar();
while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}
while(isdigit(c)){x=x*10+c-'0';c=getchar();}
return x*f;
}
const int N=10000+100;
struct LCT
{
int fa[N],son[N][2],lazy[N],mstack[N],top;
inline bool isRoot(int x)
{
return x!=son[fa[x]][0]&&x!=son[fa[x]][1];
}
inline void pushdown(int x)
{
if(lazy[x]==0) return;
lazy[son[x][0]]=!lazy[son[x][0]],swap(son[son[x][0]][0],son[son[x][0]][1]);
lazy[son[x][1]]=!lazy[son[x][1]],swap(son[son[x][1]][0],son[son[x][1]][1]);
lazy[x]=0;
}
inline void rotate(int x,int type)
{
int y=fa[x],z=fa[y];
if(isRoot(y)==false) son[z][y==son[z][1]]=x;
fa[x]=z;
fa[son[x][type]]=y,son[y][!type]=son[x][type];
son[x][type]=y,fa[y]=x;
}
void splay(int x)
{
mstack[top=1]=x;
for(int i=x;isRoot(i)==false;i=fa[i])
mstack[++top]=fa[i];
for(int i=top;i>=1;i--)
pushdown(mstack[i]);
while(isRoot(x)==false)
{
if(x==son[fa[x]][fa[x]==son[fa[fa[x]]][1]] and isRoot(fa[x])==false)
rotate(fa[x],x==son[fa[x]][0]),
rotate(x,x==son[fa[x]][0]);
else
rotate(x,x==son[fa[x]][0]);
}
}
void Access(int x)
{
for(int t=0;x!=0;t=x,x=fa[x])
splay(x),son[x][1]=t;
}
void MakeRoot(int x)
{
Access(x);
splay(x);
lazy[x]=!lazy[x],swap(son[x][0],son[x][1]);
}
int FindRoot(int x)
{
Access(x),splay(x);
while(son[x][0]!=0)
pushdown(x),x=son[x][0];
return x;
}
void Link(int x,int y)
{
MakeRoot(x);
fa[x]=y;
}
void split(int x,int y)
{
MakeRoot(x);
Access(y),splay(y);
}
void Cut(int x,int y)
{
split(x,y);
son[y][0]=fa[x]=0;
}
}lct;
int n,m;
int main()
{
n=read(),m=read(); char OP[15];
for(int i=1;i<=m;i++)
{
scanf("%s",OP+1);
int u=read(),v=read();
if(OP[1]=='C')
lct.Link(u,v);
else if(OP[1]=='D')
lct.Cut(u,v);
else
{
if(lct.FindRoot(u)==lct.FindRoot(v))
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}

[LuoguP2147] [SDOI2008]洞穴勘测 (LCT维护连通性)的更多相关文章

  1. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (LCT维护连通性)

    直接把x设为根,然后查询y所在联通块的根是不是x就行了. CODE #include <cstdio> #include <cstring> #include <algo ...

  2. BZOJ2049[Sdoi2008]洞穴勘测——LCT

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

  3. 洛谷P2147[SDOI2008]洞穴勘测(lct)

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

  4. BZOJ 2049 [SDOI2008]洞穴勘测 (LCT)

    题目大意:维护一个森林,支持边的断,连,以及查询连通性 LCT裸题 洛谷P2147传送门 1A了,给自己鼓鼓掌 #include <cstdio> #include <algorit ...

  5. [SDOI2008] 洞穴勘测 (LCT模板)

    bzoj 2049 传送门 洛谷P2147 传送门 这个大佬的LCT详解超级棒的! Link-Cut Tree的基本思路是用splay的森林维护一条条树链. splay的森林,顾名思义,就是若干spl ...

  6. BZOJ 2049 SDOI2008 洞穴勘测 LCT板子

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 题意概述:给出N个点,一开始不连通,M次操作,删边加边,保证图是一个森林,询问两点连 ...

  7. 洛谷 P2147 [SDOI2008]洞穴勘测 LCT

    Code: #include <cstdio> #include <algorithm> #include <string> #include <cstrin ...

  8. 【BZOJ2049】[SDOI2008] Cave 洞穴勘测(LCT维护连通性)

    点此看题面 大致题意: 有\(n\)个洞穴,\(3\)种操作:连一条边,删一条边,询问两点是否联通. \(LCT\)维护连通性 这道题应该是\(LCT\)动态维护连通性的一道模板题. 考虑将\(x\) ...

  9. P2147 [SDOI2008]洞穴勘测(LCT)

    P2147 [SDOI2008]洞穴勘测 裸的LCT. #include<iostream> #include<cstdio> #include<cstring> ...

随机推荐

  1. Azure 内容审查器之文本审查

    内容审查器 Azure 内容审查器也是一项认知服务.它支持对文本.图形.视频进行内容审核.可以过滤出某些不健康的内容,关键词.使你的网站内容符合当地的法律法规,提供更好的用户体验. 文本内容审核 其中 ...

  2. Android高级控件(下)

    计时器(Chronometer) getBase() 基准时间 setFormat() 设置显示格式 start() 开始计时 stop() 停止计时 setOnChronometerListener ...

  3. protoc-c 阅读笔记

    以前和山哥做过类似的,最近想起来,抽空又看了下 protoc-c. 山哥做的报文流向: rpc -> lydtree -> motree -> struct 涉及的细节很多 1) l ...

  4. linux块设备驱动---相关结构体(转)

    上回最后面介绍了相关数据结构,下面再详细介绍 块设备对象结构 block_device 内核用结构block_device实例代表一个块设备对象,如:整个硬盘或特定分区.如果该结构代表一个分区,则其成 ...

  5. 2017-18一《电子商务概论》本科作业-商A1551

    第1次作业: 1 2017年双十一新营销方案 2 销售额达1682亿元分析组成及了解猫狗大战 3 破亿店铺举例. 第2次作业: 1.你如何来定义和理解电子商务?电子商务对社会经济带了怎样的影响,企业. ...

  6. day20 Pyhton学习 面向对象-类与类之间的关系

    一.类与类之间的依赖关系 class Elphant: def __init__(self, name): self.name = name def open(self, ref): print(&q ...

  7. BOOST库 消息队列

    直接贴实验代码: /******* boost 消息队列 **********/ #if 1 #include <boost/thread/thread.hpp> #include < ...

  8. linux(centos8):使用namespace做资源隔离

    一,namespace是什么? namespace 是 Linux 内核用来隔离内核资源的方式. 它是对全局系统资源的封装隔离, 处于不同 namespace 的进程拥有独立的全局系统资源, 改变一个 ...

  9. python 安装matplotlib

    下载minianaconda 安装 进入cmd下 conda create --name python37 python=3.7  创建python3.7环境 conda activate pytho ...

  10. 一口气看完45个寄存器,CPU核心技术大揭秘

    序言 前段时间,我连续写了十来篇CPU底层系列技术故事文章,有不少读者私信我让我写一下CPU的寄存器. 寄存器这个太多太复杂,不适合写故事,拖了很久,总算是写完了,这篇文章就来详细聊聊x86/x64架 ...