【题意】给定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. ArrayList和LinkedList源码分析

    ArrayList 非线程安全 ArrayList内部是以数组存储元素的.类有以下变量: /*来自于超类AbstractList,使用迭代器时可以通过该值判断集合是否被修改*/ protected t ...

  2. Java 静态代码块&构造代码块&局部代码块

    /* 静态代码块. 随着类的加载而执行.而且只执行一次. 作用: 用于给类进行初始化. */ class StaticCode { static int num ; static { num = 10 ...

  3. iOS- Swift:如何使用iOS8中的UIAlertController

    1.前言 在前段时间手机QQ:升级iOS8.3后,发图就崩的情况, 就是因为iOS8更新UIAlertController后,仍然使用UIAlertview导致的 具体原因分析 这个可以看腾讯团队发出 ...

  4. 使用windows live writer发表的博客

    试插入代码 #include <iostream.h> using namespace std; int main() { cout<<"hello world&qu ...

  5. Windows下IntelliJ IDEA中调试Spark Standalone

    参考:http://dataknocker.github.io/2014/11/12/idea%E4%B8%8Adebug-spark-standalone/ 转载请注明来自:http://www.c ...

  6. redis 同步化操作

    异步化操作是很麻烦的的.不好控.下面介绍个同步化的库bluebird.用法很简单.看下你还子就知道了 const redis = require('redis'); const bluebird = ...

  7. python的N个小功能(连接数据库并下载相应位置的图片)

    #################################################################################################### ...

  8. 【Jmeter】集合点Synchronizing Timer

    集合点: 简单来理解一下,虽然我们的“性能测试”理解为“多用户并发测试”,但真正的并发是不存在的,为了更真实的实现并发这感念,我们可以在需要压力的地方设置集合点,每到输入用户名和密码登录时,所有的虚拟 ...

  9. [AT2148] [arc063_c] Integers on a Tree

    题目链接 AtCoder:https://arc063.contest.atcoder.jp/tasks/arc063_c 洛谷:https://www.luogu.org/problemnew/sh ...

  10. 【BZOJ2141】排队(CDQ分治)

    [BZOJ2141]排队(CDQ分治) 题面 题面以及树套树做法见这里 题解 大部分树套树/主席树这类题目都可以用整体二分/CDQ分治来做. 这题考虑一下,在不考虑修改的情况下 贡献是如何产生的? 我 ...