【题意】给定n个点和m个操作,每次操作:1.连接2个点。2.断开2个点。3.查询2个点是否连通。m<=2*10^5。

【算法】Link-Cut Tree

【题解】LCT模板题,Link,Cut,和查询两个点的root是否相同。

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
int read(){
int s=,t=;char c;
while(!isdigit(c=getchar()))if(c=='-')t=-;
do{s=s*+c-'';}while(isdigit(c=getchar()));
return s*t;
}
const int maxn=;
int t[maxn][],f[maxn],g[maxn],a[maxn];
bool isroot(int x){return !x||(t[f[x]][]!=x&&t[f[x]][]!=x);}//
void down(int x){
if(g[x]){
swap(t[x][],t[x][]);
g[t[x][]]^=;g[t[x][]]^=;
g[x]=;
}
}
void rotate(int y){
int x=f[y];
int k=y==t[x][];
t[x][!k]=t[y][k];f[t[y][k]]=x;
if(!isroot(x))t[f[x]][x==t[f[x]][]]=y;f[y]=f[x];f[x]=y;
t[y][k]=x;
}
void splay(int x){
int top=x,tot=;a[]=x;
while(!isroot(top))top=f[top],a[++tot]=top;
for(int i=tot;i>=;i--)down(a[i]);
while(!isroot(x)){//top X
if(isroot(f[x])){rotate(x);break;}
int X=x==t[f[x]][],Y=f[x]==t[f[f[x]]][];
if(X^Y)rotate(x),rotate(x);
else rotate(f[x]),rotate(x);
}
}
void access(int x){
int y=;
while(x){
splay(x);
t[x][]=y;
y=x;x=f[x];
}
}
void reserve(int x){access(x);splay(x);g[x]^=;}
void link(int x,int y){reserve(x);f[x]=y;}
void cut(int x,int y){reserve(x);access(y);splay(y);t[y][]=f[x]=;}
int root(int x){access(x);splay(x);while(t[x][])x=t[x][];return x;}
char s[];
int main(){
int n=read(),m=read();
while(m--){
scanf("%s",s);
int x=read(),y=read();
if(s[]=='C')link(x,y);
if(s[]=='D')cut(x,y);
if(s[]=='Q'){if(root(x)==root(y))printf("Yes\n");else printf("No\n");}
}
return ;
}

【BZOJ】2049: [Sdoi2008]Cave 洞穴勘测 LCT的更多相关文章

  1. bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2049 题面: 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 ...

  2. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 LCT

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...

  3. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测——LCT

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 省选之前来切一道数据结构模板题. 题意 这是一道模板题. N个点,M次操作,每次加边/ ...

  4. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (LCT维护连通性)

    直接把x设为根,然后查询y所在联通块的根是不是x就行了. CODE #include <cstdio> #include <cstring> #include <algo ...

  5. BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (动态树入门)

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1528  Solved: 644[Submit][ ...

  6. bzoj 2049: [Sdoi2008]Cave 洞穴勘测 动态树

    2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 3119  Solved: 1399[Submit] ...

  7. [BZOJ 2049] [Sdoi2008] Cave 洞穴勘测 【LCT】

    题目链接:BZOJ - 2049 题目分析 LCT的基本模型,包括 Link ,Cut 操作和判断两个点是否在同一棵树内. Link(x, y) : Make_Root(x); Splay(x); F ...

  8. 【刷题】BZOJ 2049 [Sdoi2008]Cave 洞穴勘测

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

  9. bzoj 2049 [Sdoi2008]Cave 洞穴勘测(LCT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2049 [题意] 给定森林,可能有连边或断边的操作,回答若干个连通性的询问. [思路] ...

随机推荐

  1. java一些知识

    类名前只有两种修饰符:不写(即default,但不能把default写上去)或public.默认不写则此类只能被同一包下的类调用以生成相应的实例.但若是public,则可以被不同包下的类调用以生成其实 ...

  2. bash 基础

    bash是一些基本的命令组成,要想学好bash脚本,基本命令是必须要记住 红色标记为着重点 1执行命令,利用状态返回值来判断 0 : 成功 1-255:失败 2: 测试表达式 [  expressio ...

  3. [转帖] 部分收费的Oracle JDK VS 完全免费的OpenJDK

    来源: http://www.flammulina.com/2018/10/28/%E9%83%A8%E5%88%86%E6%94%B6%E8%B4%B9%E7%9A%84oracle-jdk-vs- ...

  4. gulp 定义依赖关系

    var gulp = require('gulp'); // 返回一个 callback,因此系统可以知道它什么时候完成 gulp.task('one', function(cb) { // 做一些事 ...

  5. list+map

    通常读取数据库表中的一条记录后,可以存储于Hashmap变量中:若要读取多条记录,则依次读取每个记录时,先用Hashmap变量存取,然后将Hashmap加到ArrayList变量中. 注意: 每次读取 ...

  6. BZOJ4898/5367 Apio2017商旅(分数规划+floyd)

    如果要在某点买入某物品并在另一点卖出,肯定是走其间最短路径.于是预处理任意两点间的收益和最短路径,连完边二分答案判负环即可,可以全程floyd.注意inf大小. #include<iostrea ...

  7. windows 网络共享无法用

    可以远程电脑,但是无法网卡共享 原因是  远程电脑的Server服务停掉了,再开启下就行了

  8. 【刷题】BZOJ 1453 [Wc]Dface双面棋盘

    Description Input Output Sample Input Sample Output HINT Solution 不强制在线的动态图问题,那就LCT了 类似二分图那道题目 对于四个方 ...

  9. 洛谷 P3312 [SDOI2014]数表 解题报告

    P3312 [SDOI2014]数表 题目描述 有一张\(N*M\)的数表,其第\(i\)行第\(j\)列(\(1\le i \le n\),\(1 \le j \le m\))的数值为能同时整除\( ...

  10. Linux之Makefile20160707

    说一下LINUX下的Makefile,直接根据实际碰到的Makefile进行解读: 当make的目标为all时,-C $(KDIR) 指明跳转到内核源码目录下读取那里的Makefile:M=$(PWD ...