题目链接: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. Oracle之视图

    Oracle之视图 2018.9.12 由于视图的数据与表数据互相关联,所以切记谨慎操作 建立视图 使用下面sql语句来完成视图的创建 create or replace view 视图名 as se ...

  2. MySQL提升课程 全面讲解MySQL架构设计-索引

    索引是什么? 索引是帮助MySQL高效获取数据的数据结构. 索引能干什么? 提高数据查询的效率. 索引:排好序的快速查找数据结构!索引会影响where后面的查找,和order by 后面的排序. 一. ...

  3. Vue--- Vue(Pubsub + Ajax) 数据交互

    案例知识点 兄弟组件儿的通信     使用了Pubsub    订阅与发布 ajax数据请求    获取前   获取中   获取后   获取为空    获取异常 获取成功后显示数据给到  原先定义号的 ...

  4. 前端关于SEO

    提高页面加载速度. 能用css解决的不用背景图片,背景图片也尽量压缩大小,可以几个icons放在一个图片上,使用background-position找到需要的图片位置.可以减少HTTP请求数,提高网 ...

  5. SPOJ PRIME1 - Prime Generator(线性筛)

    Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate ...

  6. Sql Server 查看存储过程最后修改时间

    Sql Server 查看存储过程最后修改时间 select * from sys.procedures order by modify_date desc

  7. 爬虫——Selenium与PhantomJS

    Selenium Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,类型像我们玩游戏用的按键精灵,可以按指定的命令自动操作,不同的是Selenium可以直接运行在浏览器上, ...

  8. 初学Splunk

    splunk简介 https://www.splunk.com/zh-hans_cn/download.html splunk 简体中文版手册 http://docs.splunk.com/Docum ...

  9. 【Win10分区教程】

    Win10怎么分区?如何为Win10硬盘分区? 注:本教程适用于Win7.Win8.Win8.1和Win10系 到了Windows10时代,TB级硬盘已经很普及了,那么在Win10系统下如何为这些大容 ...

  10. IDEA无法引入已经创建的类

    解决办法: 清理一下缓存: File->Invalidate Caches/Restart... OVER!