BZOJ 2049 SDOI2008 洞穴勘测 LCT板子
题目链接: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板子的更多相关文章
- BZOJ 2049 [SDOI2008]洞穴勘测 (LCT)
题目大意:维护一个森林,支持边的断,连,以及查询连通性 LCT裸题 洛谷P2147传送门 1A了,给自己鼓鼓掌 #include <cstdio> #include <algorit ...
- bzoj 2049 Cave 洞穴勘测(LCT)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 动态树入门题,不需要维护任何信息. 我用的是splay,下标实现的lct. #in ...
- BZOJ2049[Sdoi2008]洞穴勘测——LCT
题目描述 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假如 ...
- 洛谷P2147[SDOI2008]洞穴勘测(lct)
题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假 ...
- [SDOI2008] 洞穴勘测 (LCT模板)
bzoj 2049 传送门 洛谷P2147 传送门 这个大佬的LCT详解超级棒的! Link-Cut Tree的基本思路是用splay的森林维护一条条树链. splay的森林,顾名思义,就是若干spl ...
- [LuoguP2147] [SDOI2008]洞穴勘测 (LCT维护连通性)
题面 传送门:https://www.luogu.org/problemnew/show/P2147 Solution 这题...... 我们可以发现题目要求我们维护一个动态森林,而且只查询连通性.. ...
- 洛谷 P2147 [SDOI2008]洞穴勘测 LCT
Code: #include <cstdio> #include <algorithm> #include <string> #include <cstrin ...
- P2147 [SDOI2008]洞穴勘测(LCT)
P2147 [SDOI2008]洞穴勘测 裸的LCT. #include<iostream> #include<cstdio> #include<cstring> ...
- P2147 [SDOI2008]洞穴勘测
P2147 [SDOI2008]洞穴勘测 思路 没办法,我就是喜欢板子都想发的人 都是基础操作,不多说了 代码 #include <bits/stdc++.h> #define ls ch ...
随机推荐
- 运行Python
安装好python环境,在Windows系统下运行cmd命令行,是windows提供的命令行模式. 在命令行下,可以执行python进入Python交互式环境,也可以执行python hello.py ...
- 用c#语言编写1000以内能整除3的数字
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- SQL Server笔记——sql语句创建数据库
MS SQLServer的每个数据库包含: 1个主数据文件(.mdf)必须. 1个事务日志文件(.ldf)必须. 可以包含: 任意多个次要数据文件(.ndf) 多个事务日志文件 CREATE DATA ...
- 配置web项目session永不超时
众所周知,当用户登录网站后较长一段时间没有与服务器进行交互,将会导致服务器上的用户会话数据(即session)被销毁.此时,当用户再次操作网页时,如果服务器进行了session校验,那么浏览器将会提醒 ...
- 封装全局icon组件 svg (仿造element-ui源码)
一.引入 svg-sprite-loader 插件 npm install svg-sprite-loader --save-dev vue-cli项目默认情况下会使用 url-loader 对sv ...
- 菜鸟笔记 -- Chapter 6.2.4 成员方法
6.2.4 成员方法 在Java中使用成员方法对应于类对象的行为,在有些地方也会将方法称之为函数,成员方法是定义在类中具有特定功能的一段独立小程序.方法格式如下: 修饰符 返回值类型 成员方法名 ( ...
- qt项目:员工信息管理系统
开发一个员工信息管理系统 一.项目具体要求: 1.用qt开发界面,数据库用QSqlite 数据库文件名:demostudent.db 2.通过界面能够查看到数据库中员工信息表中内容,包括员工姓名.年龄 ...
- 查看系统PCI设备
# lspci Host bridge:主板 VGA compatible controller:VGA显卡设备 Class 0403:声卡设备 USB Controller:USB接口设备 SATA ...
- .Net core NPOI导入导出Excel
最近在想.net core NPOI 导入导出Excel,一开始感觉挺简单的,后来真的遇到很多坑.所以还是写一篇博客让其他人少走一些弯路,也方便忘记了再重温一遍.好了,多的不说,直接开始吧. 在.Ne ...
- docker搭建基于percona-xtradb-cluster方案的mysql集群
一.部署环境 序号 hostname ip 备注 1 manager107 10.0.3.107 centos7;3.10.0-957.1.3.el7.x86_64 2 worker68 10.0.3 ...