题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2049

题意概述:给出N个点,一开始不连通,M次操作,删边加边,保证图是一个森林,询问两点连通性。

N<=10000,M<=200000

实际上我就是想来放个LCT板子。。。。。。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cctype>
using namespace std; int N,M;
struct link_cut_tree{
static const int maxn=;
struct node{
int ch[],fa; bool res;
node(){ ch[]=ch[]=fa=,res=; }
}nd[maxn];
void init(int n) { for(int i=;i<=n;i++) nd[i].ch[]=nd[i].ch[]=nd[i].fa=,nd[i].res=; }
void link(int x,int d,int y) { nd[x].ch[d]=y,nd[y].fa=x; }
bool isrt(int x) { return nd[nd[x].fa].ch[]!=x&&nd[nd[x].fa].ch[]!=x; }
void pushdown(int x)
{
if(!nd[x].res) return;
int lc=nd[x].ch[],rc=nd[x].ch[];
if(lc) nd[lc].res^=,swap(nd[lc].ch[],nd[lc].ch[]);
if(rc) nd[rc].res^=,swap(nd[rc].ch[],nd[rc].ch[]);
nd[x].res=;
}
void rot(int x)
{
int y=nd[x].fa,z=nd[y].fa;
pushdown(y);pushdown(x);
if(!isrt(y)) link(z,nd[z].ch[]==y,x); nd[x].fa=z;
int d=nd[y].ch[]==x;
link(y,d^,nd[x].ch[d]);
link(x,d,y);
}
void splay(int x)
{
pushdown(x);
while(!isrt(x)){
int y=nd[x].fa,z=nd[y].fa;
if(!isrt(y)) rot((nd[y].ch[]==x)==(nd[z].ch[]==y)?y:x);
rot(x);
}
}
void access(int x)
{
int y=;
while(x){ splay(x); nd[x].ch[]=y,y=x,x=nd[x].fa; }
}
void mroot(int x)
{
access(x); splay(x);
nd[x].res^=,swap(nd[x].ch[],nd[x].ch[]);
}
void Link(int x,int y) { mroot(x); nd[x].fa=y; }
void Cut(int x,int y)
{
mroot(x); access(y); splay(y);
nd[x].fa=nd[y].ch[]=;
}
int find(int x)
{
access(x); splay(x);
while(nd[x].ch[]){ pushdown(nd[x].ch[]); x=nd[x].ch[]; }
return x;
}
}lct; void work()
{
scanf("%d%d",&N,&M);
char op[]; int x,y;
for(int i=;i<=M;i++){
scanf("%s%d%d",op,&x,&y);
if(op[]=='C') lct.Link(x,y);
else if(op[]=='D') lct.Cut(x,y);
else if(op[]=='Q') puts(lct.find(x)==lct.find(y)?"Yes":"No");
}
}
int main()
{
work();
return ;
}

BZOJ 2049 SDOI2008 洞穴勘测 LCT板子的更多相关文章

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

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

  2. bzoj 2049 Cave 洞穴勘测(LCT)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 动态树入门题,不需要维护任何信息. 我用的是splay,下标实现的lct. #in ...

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

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

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

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

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

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

  6. [LuoguP2147] [SDOI2008]洞穴勘测 (LCT维护连通性)

    题面 传送门:https://www.luogu.org/problemnew/show/P2147 Solution 这题...... 我们可以发现题目要求我们维护一个动态森林,而且只查询连通性.. ...

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

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

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

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

  9. P2147 [SDOI2008]洞穴勘测

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

随机推荐

  1. mysql慢查询开启及分析方法

    最近服务维护的公司的DB服务器,总是会出现问题,感觉需要优化一下了,登陆上去,发现慢查询日志都没有开,真是惭愧, 故果断加上慢查询日志, 经过分析sql记录,发现问题很多,开发人员很多没有对sql优化 ...

  2. java交换两个值的三种方法 经典

    1.中间变量(在开发中常用) int c=a; a=b; b=c; System.out.println("a的值: "+a+" b的值: "+b); 2.按位 ...

  3. mysql主从延时临时解决办法

    主从延时临时解决办法: 主从延迟严重的原因很多,其中有以下原因: 一.由于无主键大表的delete 和update导致. mysql  主从复制时.如果表上没有主键.会导致update和delete操 ...

  4. CentOS 手动配置本地yum源(参考CentOS7 制作 CentOS6本地yum源)

    将原有/etc/yum.repos.d/目录下的文件名全部改为(*.bak),如(红色标记) [root@localhost ~]# cd /etc/yum.repos.d/ [root@localh ...

  5. React学习(1)—— 基础项目搭建以及环境配置

    首先,我们需要安装node.js,直接搜索并在官网下载安装包. node.js官网:https://nodejs.org/en/ 现在我们成功安装了node和npm,然后我们来用npm创建新的项目,首 ...

  6. PHP如何实现99乘法表?

    看到这个问题,可能大家更多的是考虑到用for循环,个人觉得使用for循环太影响程序性能.推荐使用递归处理.  /** * Title : 递归实现99乘法表 * Author : Bruceqi * ...

  7. IDEA开发vue.js卡死问题

    在执行cnpm install后会在node_modules这个文件下面生成vue的相关依赖文件, 这个时候当执行cnpm run dev命令时,会导致IDEA出现卡死的问题,解决方法如下:

  8. keil5 mdk调用外部编辑器notepad++、sublime3、VSCode总结

    1.打开keil主界面,点击菜单栏Tools菜单,选择如下图所示的选项. 2.点击如下图所示的菜单上红笔标注的地方,给这个工具命名,如notepad++.sublime3.vscode等,如下图, 并 ...

  9. Go web表单验证

    开发Web的一个原则就是,不能信任用户输入的任何信息,所以验证和过滤用户的输入信息就变得非常重要 必填字段 if len(r.Form["username"][0])==0{ // ...

  10. 人工智能,图片识别,与GUI编程

    GUI编程: https://sourceforge.net/projects/pyqt/ 百度aip图片识别 https://pypi.python.org/pypi/baidu-aip