原题传送门

这题用Link-Cut-Tree解决,Link-Cut-Tree详解

我不太会踩爆Link-Cut-Tree的并查集做法qaq

我们用Link-Cut-Tree维护连通性(十分无脑)

Connect操作:把u,v两个点连起来

Destroy操作:把u,v两个点分开来

Query操作:判断在这个森林里u的根和v的根是否相等

#include <bits/stdc++.h>
#define N 10005
using namespace std;
inline int read()
{
register int f=1,x=0;register char ch;
do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
do{x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');
return f*x;
}
inline void Swap(register int &a,register int &b)
{
a^=b^=a^=b;
}
struct Link_Cut_Tree{
int c[N][2],fa[N],top,q[N],rev[N];
inline void pushdown(register int x){
if(rev[x])
{
register int l=c[x][0],r=c[x][1];
rev[l]^=1,rev[r]^=1,rev[x]^=1;
Swap(c[x][0],c[x][1]);
}
}
inline bool isroot(register int x)
{
return c[fa[x]][0]!=x&&c[fa[x]][1]!=x;
}
inline void rotate(register int x)
{
int y=fa[x],z=fa[y],l,r;
l=c[y][0]==x?0:1;
r=l^1;
if(!isroot(y))
c[z][c[z][0]==y?0:1]=x;
fa[x]=z;
fa[y]=x;
fa[c[x][r]]=y;
c[y][l]=c[x][r];
c[x][r]=y;
}
inline void splay(register int x)
{
top=1;
q[top]=x;
for(register int i=x;!isroot(i);i=fa[i])
q[++top]=fa[i];
for(register int i=top;i;--i)
pushdown(q[i]);
while(!isroot(x))
{
int y=fa[x],z=fa[y];
if(!isroot(y))
rotate((c[y][0]==x)^(c[z][0]==y)?(x):(y));
rotate(x);
}
}
inline void access(register int x)
{
for(register int t=0;x;t=x,x=fa[x])
{
splay(x);
c[x][1]=t;
}
}
inline void makeroot(register int x)
{
access(x);
splay(x);
rev[x]^=1;
}
inline int findroot(register int x)
{
access(x);
splay(x);
while(c[x][0])
x=c[x][0];
return x;
}
inline void split(register int x,register int y)
{
makeroot(x);
access(y);
splay(y);
}
inline void cut(register int x,register int y)
{
split(x,y);
c[y][0]=0;
fa[x]=0;
}
inline void link(register int x,register int y)
{
makeroot(x);
fa[x]=y;
}
}T;
int n,m;
int main()
{
n=read(),m=read();
char ch[10];
while(m--)
{
scanf("%s",ch);
if(ch[0]=='C')
{
int x=read(),y=read();
T.link(x,y);
}
else if(ch[0]=='D')
{
int x=read(),y=read();
T.cut(x,y);
}
else
{
int x=read(),y=read();
puts(T.findroot(x)==T.findroot(y)?"Yes":"No");
}
}
return 0;
}

【题解】Luogu P2147 [SDOI2008]洞穴勘测的更多相关文章

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

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

  2. P2147 [SDOI2008]洞穴勘测

    P2147 [SDOI2008]洞穴勘测 思路 没办法,我就是喜欢板子都想发的人 都是基础操作,不多说了 代码 #include <bits/stdc++.h> #define ls ch ...

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

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

  4. [洛谷P2147][SDOI2008]洞穴勘测

    题目大意:有$n$个洞穴,$m$条指令,指令有三种 $Connect\;u\;v$:在$u,v$之间连一条边 $Destroy\;u\;v$:切断$u,v$之间的边 $Query\;u\;v$:询问$ ...

  5. 洛谷 P2147 [SDOI2008]洞穴勘测 (线段树分治)

    题目链接 题解 早就想写线段树分治的题了. 对于每条边,它存在于一段时间 我们按时间来搞 我们可把一条边看做一条线段 我们可以模拟线段树操作,不断分治下去 把覆盖\(l-r\)这段时间的线段筛选出来, ...

  6. 洛谷 P2147 [SDOI2008]洞穴勘测

    以下这个做法应该是叫线段树分治... 根据修改操作预处理出每条边存在的时间区间[l,r](以操作序号为时间),然后把所有形式化后的修改挂到线段树节点上. 处理完修改后,dfs一遍线段树,进入某个节点时 ...

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

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

  8. BZOJ2049:[SDOI2008]洞穴勘测——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2049 https://www.luogu.org/problemnew/show/P2147 辉辉热 ...

  9. 洛谷P2147 [SDOI2008] 洞穴勘探 [LCT]

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

随机推荐

  1. openCV学习——一、图像读取、显示、输出

    openCV学习——一.图像读取.显示.输出   一.Mat imread(const string& filename,int flags=1),用于读取图片 1.参数介绍 filename ...

  2. jQuery-切换事件2

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  3. 《大话设计模式》c++实现 外观模式

    外观模式:为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 外观模式在什么时候使用呢? 分为三个阶段: (1)首先,在设计初期阶段,应该要有意识的 ...

  4. steam Depot 生成与应用脚本

    Depot 生成脚本 首先为您上传的每个 Depot 创建一个 Depot 生成脚本.从复制 depot_build_1001.vdf 脚本开始,并按将上传的 Depot ID 命名. Depot 生 ...

  5. html5 手机端 通讯录 touch 效果

    不说那么多直接上代码. <html> <head> <meta http-equiv="Content-Type" content="tex ...

  6. CSS背景与边框属性-----box-shadow

    box-shadow:none | <shadow> [ , <shadow> ]*   <shadow> = inset? && <leng ...

  7. 【2017-2-23】C#switch case分支语句,for循环语句

    switch case分支语句 switch(一个变量值) { case 值:要执行的代码段;break; case 值:要执行的代码段;break; … default:代码段;break;(def ...

  8. 解读 JavaScript 之引擎、运行时和堆栈调用

    https://www.oschina.net/translate/how-does-javascript-actually-work-part-1 随着 JavaScript 变得越来越流行,很多团 ...

  9. win7 怎么设置开机直接进入桌面? netplwiz 命令

    电脑没设置密码,开机如何跳过帐户已锁定的界面,直接进入桌面呢? 1.单击[运行],或按快捷键:win+r2.输入命令:netplwiz 单击[确定]3.单击你的登录账户4.去掉[要使用本机,用户名必须 ...

  10. navicat远程连接阿里云ECS上的MYSQL报Lost connection to MySQL server at 'reading initial communication packet'

    问题现象 MySQL 远程连接报错:Lost connection to MySQL server at 'reading initial communication packet' 解决方案 1.检 ...