题目链接: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. Linux 下的多线程编程(1)

    #include<stdio.h> #include<pthread.h> #include<string.h> #include<sys/time.h> ...

  2. iview+axios实现文件取消上传

    iview+axios实现文件取消上传 iview框架的上传文件目前不支持在上传文件的过程中取消上传,结合axios请求可以实现:使用iview的上传和拖拽功能,却使用axios的上传文件功能来实现取 ...

  3. 配置隐私协议 - iOS

    根据苹果隐私协议新规的推出,要求所有应用包含隐私保护协议,故为此在 App 中添加了如下隐私协议模块. 首次安装 App 的情况下默认调用隐私协议模块展示相关信息一次,当用户点击同意按钮后,从此不再执 ...

  4. WKWebView进度及title

    WKWebView进度及title WKWebView进度及title WKWebView 的estimatedProgress和title 都是KVO模式,所以可以添加监控: [webView ad ...

  5. JAVA面向对象思想理解分析

    1.面向对象是面向过程而言.两者都是一种思想.面向过程:强调的是功能行为.(强调过程.动作)面向对象:将功能封装进对象,强调了具备了功能的对象.(强调对象.事物)面向对象是基于面向过程的.将复杂的事情 ...

  6. unexpected reloc type问题分析

    1.现象,程序在启动的时候报如下错误error while loading shared libraries: /home/test/lib/libtest.so: unexpected reloc ...

  7. 在C++中如何实现文件的读写

    一.ASCII 输出为了使用下面的方法, 你必须包含头文件<fstream.h>(译者注:在标准C++中,已经使用<fstream>取代< fstream.h>,所 ...

  8. 小程序登录 -41003: aes 小程序加密数据解密失败问题

    在微信小程的开发中,登录问题,一定要按照这样的顺序 1. 小程序请求login,拿到code 然后传给服务端:  2.服务端拿到code 到微信服务器拿到sessionKey :3.然后小程序调用ge ...

  9. 【操作系统作业—lab1】linux shell脚本 遍历目标文件夹和所有文件 | 包括特殊字符文件名的处理

    要求:写一个linux bash脚本来查看目标文件夹下所有的file和directory,并且打印出他们的绝对路径. 运行command:./myDir.sh  input_path  output_ ...

  10. 'sessionFactory' or 'hibernateTemplate' is required

    网上都是说在dao中未注入  sessionFactory,然而我有 于是排除 @Autowired public FlightDaoImpl(@Qualifier(value = "ses ...