【题意】给定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. python、Eclipse、pydev环境配置

    转载来源:http://www.cnblogs.com/Bonker/p/3584707.html 编辑器: Eclipse + pydev插件: 1. Eclipse是写JAVA的IDE, 这样就可 ...

  2. centos 6 编译emacs-24.5

    yum install `yum deplist emacs | grep provider | awk -F: '{print $2}' | awk '{print $1}' | xargs` yu ...

  3. Alpha阶段敏捷冲刺 ADY8

    一.举行站立式例会 今天也没有拍照片,人不齐. 二.团队报告 1.昨日已完成的工作 (1)创建一个test,并且将图片导入进去使其可以显示. 2.今日计划完成的工作 完成收尾工作.实现代码的连接. 3 ...

  4. CentOS7 修改yum源为阿里云

    1,登陆root帐号 2,cd /etc/yum.repo.d 3,mv CentOS-Base.repo CentOS-Base.repo.bak4,wget http://mirrors.aliy ...

  5. JMeter脚本增强之参数化

    JMeter测试脚本录制或者编写,在Web应用和App上的操作方式可能有一点点区别(其实也差不多,哈哈),但是当脚本录制好了之后,对测试脚本的强化,包括参数化.关联.文本检查.集合点设置,甚至再往后的 ...

  6. ICPCCamp 2017 I Coprime Queries

    给出一个长度为\(n\)的正整数序列\(a\),\(m\)次询问\(l,r,x\),问\(max\{i|i\in[l,r],gcd(a_i,x)=1\}\). \(n,m,a_i\le 10^5\). ...

  7. [十五]SpringBoot 之 启动加载数据

    实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求. 为了解决这样的问题,spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来 ...

  8. IOS中手势UIGestureRecognizer

    通常在对视图进行缩放移动等操作的时候我们可以用UIScrollView,因为它里边自带了这些功能,我们要做的就是告诉UIScrollView的几个相关参数就可以了 但是没有实现旋转的手势即UIRota ...

  9. JDBC连接Oracle

    数据库的操作是当前系统开发必不可少的开发部分之一,尤其是在现在的大数据时代,数据库尤为重要.但是你真的懂得Java与数据库是怎么连接的么? 先给大家一个数据库连接的简单实例: package com. ...

  10. Redis存储系统

    一.QuickStart 1.Redis简介: redis是一个性能非常优秀的内存数据库,通过key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string ...