【BZOJ】2049: [Sdoi2008]Cave 洞穴勘测 LCT
【题意】给定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的更多相关文章
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2049 题面: 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 LCT
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnli ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测——LCT
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 省选之前来切一道数据结构模板题. 题意 这是一道模板题. N个点,M次操作,每次加边/ ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (LCT维护连通性)
直接把x设为根,然后查询y所在联通块的根是不是x就行了. CODE #include <cstdio> #include <cstring> #include <algo ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (动态树入门)
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1528 Solved: 644[Submit][ ...
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测 动态树
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 3119 Solved: 1399[Submit] ...
- [BZOJ 2049] [Sdoi2008] Cave 洞穴勘测 【LCT】
题目链接:BZOJ - 2049 题目分析 LCT的基本模型,包括 Link ,Cut 操作和判断两个点是否在同一棵树内. Link(x, y) : Make_Root(x); Splay(x); F ...
- 【刷题】BZOJ 2049 [Sdoi2008]Cave 洞穴勘测
Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...
- bzoj 2049 [Sdoi2008]Cave 洞穴勘测(LCT)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2049 [题意] 给定森林,可能有连边或断边的操作,回答若干个连通性的询问. [思路] ...
随机推荐
- 搜索引擎Elasticsearch,了解一下?
ElasticSearch介绍 ElasticSearch是一个全文搜索服务器,也可以作为NoSql数据库,存储任意格式的文档和数据,同时可以做大数据的分析.ElasticSearch具有以下特点: ...
- WPF和Expression Blend开发实例:模拟QQ登陆界面打开和关闭特效
不管在消费者的心中腾讯是一个怎么样的模仿者抄袭者的形象,但是腾讯在软件交互上的设计一直是一流的.正如某位已故的知名产品经理所说的:设计并非外观怎样,感觉如何.设计的是产品的工作原理.我觉得腾讯掌握了其 ...
- 词法分析用c++实现的
#include<stdio.h>#include<string.h>int i,j,k,sign,flag,number,run;char ch;char word[10]; ...
- artdialog对话框 三种样式 网址:http://www.planeart.cn/demo/artDialog/_doc/labs.html
摇头效果 类似与wordpress登录失败后登录框可爱的左右晃动效果 // 2011-07-17 更新 artDialog.fn.shake = function (){ var style = th ...
- Filter2D卷积运算
图像处理中的卷积运算一般都用来平滑图像.尖锐图像求边缘等等.主要看你选择什么样的核函数了.现在核函数很多,比如高斯平滑核函数,sobel核函数,canny核函数等等.这里举一个sobel核函数的例子来 ...
- vue.js+vue-router+webpack keep-alive用法
本文是机遇 提纲: 现有需求 各个解决方案的优缺点 相关的问题延伸 keep-alive使用详解 现有需求 每个项目中都存在许多列表数据展示页面,而且通常包含一些筛选条件以及分页. 并 ...
- Codeforces 627D Preorder Test(二分+树形DP)
题意:给出一棵无根树,每个节点有一个权值,现在要让dfs序的前k个结点的最小值最大,求出这个值. 考虑二分答案,把>=答案的点标记为1,<答案的点标记为0,现在的任务时使得dfs序的前k个 ...
- FragmentTransaction add 和 replace 区别 转
使用 FragmentTransaction 的时候,它提供了这样两个方法,一个 add , 一个 replace . add 和 replace 影响的只是界面,而控制回退的,是事务. public ...
- (三)MySQL学习笔记
[Leecode]175. 组合两个表 解答:由于是组合两个表的信息,很容易想到连接查询,这里使用左连接 select p.Firstname,p.Lastname,q.City,q.State fr ...
- CF605E Intergalaxy Trips 贪心 概率期望
(当时写这篇题解的时候,,,不知道为什么,,,写的非常冗杂,,,不想改了...) 题意:一张有n个点的图,其中每天第i个点到第j个点的边都有$P_{i, j}$的概率开放,每天可以选择走一步或者留在原 ...