[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> ...
随机推荐
- C++指针delete后还要置为null
非常好的一篇说明: 转载:https://blog.csdn.net/qq_36570733/article/details/80043321 众所周知,最开始我们用new来创建一个指针,那么等我们用 ...
- Java 异常 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date'
查询时发送给服务器的日期的字符串格式:yyyy-MM-dd HH:mm:ss 服务器接收到日期的字符串之后,向 MySQL 数据库发起查询时,因为没有指定日期时间格式,导致字符串数据不能正确地转换为日 ...
- css实现中间横线俩边文字效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 实验五 用PS制作图文合成海报
实验五 用PS制作图文合成海报 [实验目的] ⑴.熟悉PS软件基本操作 ⑵.学会用PS制作内容较丰富的海报式广告 [实验条件] ⑴.个人计算机一台 ⑵.个人计算机中预装Windows7操作系统和浏览 ...
- c# 常用帮助类
C#常用帮助类 因为小土现在还是处于小白阶段,所以自己的知识技术还达不到要求,但是小土在网上找到一个大神的,等以后小土技术有了一定提升以后,在走自己的路,啥也不说了上货. 地址 :https://gi ...
- spring boot:用shardingjdbc实现多数据源的分库分表(shardingsphere 4.1.1/spring boot 2.3.1)
一,shardingjdbc的用途 1,官方站介绍: Apache ShardingSphere 是一套开源的分布式数据库中间件解决方案组成的生态圈, 它由 JDBC.Proxy 和 Sidecar( ...
- nfs4使用中的防火墙配置
一,查看本地centos的版本: [root@localhost lib]# cat /etc/redhat-release CentOS Linux release 8.1.1911 (Core) ...
- centos8平台yum无法安装一些常用软件的解决,如:screen,iftop,nethogs
一,例如:安装screen时报错: [root@localhost liuhongdi]# yum install screen 上次元数据过期检查:17:39:58 前,执行于 2020年03月18 ...
- nginx 是如何处理过期事件的?
目录 什么是过期事件 nginx 是如何处理过期事件的? 参考资料 什么是过期事件 对于不需要加入到 post 队列 延后处理的事件,nginx 的事件都是通过 ngx_epoll_process_e ...
- Java安全之Commons Collections3分析
Java安全之Commons Collections3分析 文章首发:Java安全之Commons Collections3分析 0x00 前言 在学习完成前面的CC1链和CC2链后,其实再来看CC3 ...