题面

传送门: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. Python-属性描叙符协议ORM实现原理依据- __set__ __get__ __delete__

    class CheckString: def __init__(self, variable_type): self.variable_type = variable_type def __set__ ...

  2. django rest_framework serializer的ManyRelatedField 和 SlugRelatedField使用

    class BlogListSerializer(serializers.Serializer): id = serializers.IntegerField() user = BlogUserInf ...

  3. SQL实战——04. 查找所有已经分配部门的员工的last_name和first_name以及dept_no (一个逗号引发的血案)

    查找所有已经分配部门的员工的last_name和first_name以及dept_noCREATE TABLE `dept_emp` (`emp_no` int(11) NOT NULL,`dept_ ...

  4. 084 01 Android 零基础入门 02 Java面向对象 01 Java面向对象基础 02 构造方法介绍 03 构造方法-this关键字

    084 01 Android 零基础入门 02 Java面向对象 01 Java面向对象基础 02 构造方法介绍 03 构造方法-this关键字 本文知识点:构造方法-this关键字 说明:因为时间紧 ...

  5. Java知识系统回顾整理01基础05控制流程04 for

    一.for 比较for和while public class HelloWorld { public static void main(String[] args) { //使用while打印0到4 ...

  6. Python中matplotlib.pyplot.imshow画灰度图的多种方法

    转载:https://www.jianshu.com/p/8f96318a153f matplotlib库的教程和使用方法此处就不累赘了,网上有十分多优秀的教程资源.此处直接上代码: def demo ...

  7. ATMEGA的SPI总线 - 第2部分

    参考: 1.https://www.yiboard.com/thread-783-1-1.html 2.https://mansfield-devine.com/speculatrix/2018/01 ...

  8. 为什么很多国内公司在做 AI 芯片?

    据网上搜到的新闻报道,截止2019年,已经有20家企业投入到 AI 芯片的研发中,其中有很多厂商的芯片已经流片甚至商用了.为何有这么多公司在做AI芯片呢?简单来讲就是四个字:有利可图.具体来说有以下三 ...

  9. BUUCTF-[极客大挑战 2019]BabySQL 1 详解

    打开靶机 应该是love sql惹的事吧,来了个加强版本的sql注入,不过我们先输入账号密码看有什么反应 整一手万能密码,闭合双引号?username=admin&password=admin ...

  10. GDB将所有线程堆栈输出到文件

    在调试多线程程序时,经常需要查看线程堆栈信息,如果线程数目过多,每次查看一个线程堆栈,繁琐耗时.下面介绍一种一次性将所有线程堆栈输出到文件的方法. 首先,将gdb attach到调试线程 gdb -p ...