[LuoguP2147] [SDOI2008]洞穴勘测 (LCT维护连通性)
题面
传送门: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维护连通性)的更多相关文章
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (LCT维护连通性)
直接把x设为根,然后查询y所在联通块的根是不是x就行了. CODE #include <cstdio> #include <cstring> #include <algo ...
- BZOJ2049[Sdoi2008]洞穴勘测——LCT
题目描述 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假如 ...
- 洛谷P2147[SDOI2008]洞穴勘测(lct)
题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假 ...
- BZOJ 2049 [SDOI2008]洞穴勘测 (LCT)
题目大意:维护一个森林,支持边的断,连,以及查询连通性 LCT裸题 洛谷P2147传送门 1A了,给自己鼓鼓掌 #include <cstdio> #include <algorit ...
- [SDOI2008] 洞穴勘测 (LCT模板)
bzoj 2049 传送门 洛谷P2147 传送门 这个大佬的LCT详解超级棒的! Link-Cut Tree的基本思路是用splay的森林维护一条条树链. splay的森林,顾名思义,就是若干spl ...
- BZOJ 2049 SDOI2008 洞穴勘测 LCT板子
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 题意概述:给出N个点,一开始不连通,M次操作,删边加边,保证图是一个森林,询问两点连 ...
- 洛谷 P2147 [SDOI2008]洞穴勘测 LCT
Code: #include <cstdio> #include <algorithm> #include <string> #include <cstrin ...
- 【BZOJ2049】[SDOI2008] Cave 洞穴勘测(LCT维护连通性)
点此看题面 大致题意: 有\(n\)个洞穴,\(3\)种操作:连一条边,删一条边,询问两点是否联通. \(LCT\)维护连通性 这道题应该是\(LCT\)动态维护连通性的一道模板题. 考虑将\(x\) ...
- P2147 [SDOI2008]洞穴勘测(LCT)
P2147 [SDOI2008]洞穴勘测 裸的LCT. #include<iostream> #include<cstdio> #include<cstring> ...
随机推荐
- QT记录
/******************************************************************************************/ .
- 074 01 Android 零基础入门 01 Java基础语法 09 综合案例-数组移位 06 综合案例-数组移位-主方法功能3的实现
074 01 Android 零基础入门 01 Java基础语法 09 综合案例-数组移位 06 综合案例-数组移位-主方法功能3的实现 本文知识点:综合案例-数组移位-主方法功能3的实现 说明:因为 ...
- 用集装箱装ASP。带有Docker和Azure Kubernetes服务的NET Core应用程序
介绍 曾经有一个单一软件应用程序的时代,整个应用程序被打包并部署在作为单个进程运行的单个服务器上.我们都知道,在这个模型中,单点故障可能会导致整个应用程序崩溃. 微服务体系结构的发展是为了解决单片应用 ...
- php上传图片,网站代码
1.php代码 2.表设置结构 3.jquery代码 4.显示网站的数据 6.上传图片 7.上传图片的php代码
- Redis 中 HyperLogLog 的使用场景
什么是基数估算 HyperLogLog 是一种基数估算算法.所谓基数估算,就是估算在一批数据中,不重复元素的个数有多少. 从数学上来说,基数估计这个问题的详细描述是:对于一个数据流 {x1,x2,.. ...
- RFC 8684---TCP Extensions for Multipath Operation with Multiple Addresses
https://datatracker.ietf.org/doc/rfc8684/?include_text=1 TCP Extensions for Multipath Operation with ...
- 转一个veth的文章
这篇写的很好,清晰明白,保存一下https://www.cnblogs.com/bakari/p/10613710.html
- linux centos 02
1.PS1变量,命令提示符的修改 PS1="[\u@\h \W]\$" \u 代表 用户 @ 占位符 \h 主机名 \W 工作路径的最后一位 \t \w 工作路径 ...
- github 如何解决error: failed to push some refs
错误 error: failed to push some refs to 'https://github.com/whitclass/scrapy-spider.git' hint: Updates ...
- 什么是 C 和 C ++ 标准库?学编程的你应该知道这些知识!
简要介绍编写C/C ++应用程序的领域,标准库的作用以及它是如何在各种操作系统中实现的. 我已经接触C++一段时间了,一开始就让我感到疑惑的是其内部结构:我所使用的内核函数和类从何而来? 谁发明了它们 ...