bzoj 2049 Cave 洞穴勘测(LCT)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
动态树入门题,不需要维护任何信息。
我用的是splay,下标实现的lct。
#include <iostream>
#include <sstream>
#include <ios>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <vector>
#include <string>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <climits>
#include <cctype>
using namespace std;
#define XINF INT_MAX
#define INF 0x3FFFFFFF
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
typedef long long ll;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
#define MAXN 100010
int ch[MAXN][],key[MAXN],pre[MAXN],size[MAXN],ss[MAXN];
int rev[MAXN];
void push_down(int r){
if(!r)return;
if(rev[r]){
rev[ch[r][]]^=;
rev[ch[r][]]^=;
swap(ch[r][],ch[r][]);
rev[r]=;
}
}
void rotate(int x,int d){
int y=pre[x];
ch[y][!d]=ch[x][d];
if(ch[x][d])pre[ch[x][d]]=y;
pre[x]=pre[y];
if(ch[pre[y]][]==y)ch[pre[x]][]=x;
else if(ch[pre[y]][]==y)ch[pre[x]][]=x;
pre[y]=x;
ch[x][d]=y;
}
bool check(int x,int y){
return y&&(ch[y][]==x||ch[y][]==x);
}
void splay(int x){
push_down(x);
int y,z;
while(check(x,y=pre[x])){
if(check(y,z=pre[y])){
push_down(z);
push_down(y);
push_down(x);
int d=(y==ch[z][]);
if(x==ch[y][d]) {rotate(x,!d),rotate(x,d);}
else {rotate(y,d),rotate(x,d);}
}else{
push_down(y);
push_down(x);
rotate(x,ch[y][]==x);
break;
}
}
}
int access(int u){
int v=;
for(;u;u=pre[u]){
splay(u);
ch[u][]=v;
v=u;
}
//splay(u);
return v;
}
int getroot(int x){
for(x=access(x);push_down(x),ch[x][];x=ch[x][]);
return x;
}
void makeroot(int x){
rev[access(x)]^=;
splay(x);
}
void link(int x,int y){
makeroot(x);
pre[x]=y;
access(x);
}
void cut(int x,int y){
makeroot(x);
access(y);
splay(y);
pre[ch[y][]]=;
ch[y][]=;
}
void init(int n){
for(int i=;i<=n;i++)
ch[i][]=ch[i][]=pre[i]=;
}
void query(int x,int y){
int ra=getroot(x);
int rb=getroot(y);
if(ra==rb&&ra)printf("Yes\n");
else printf("No\n");
} int main()
{
ios::sync_with_stdio(false);
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
init(n);
int x,y;
char a[];
for(int i=;i<m;i++){
scanf("%s%d%d",a,&x,&y);
if(a[]=='C')link(x,y);
else if(a[]=='D')cut(x,y);
else query(x,y);
}
}
return ;
}
bzoj 2049 Cave 洞穴勘测(LCT)的更多相关文章
- BZOJ 2049 SDOI2008 洞穴勘测 LCT板子
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 题意概述:给出N个点,一开始不连通,M次操作,删边加边,保证图是一个森林,询问两点连 ...
- BZOJ 2049 [SDOI2008]洞穴勘测 (LCT)
题目大意:维护一个森林,支持边的断,连,以及查询连通性 LCT裸题 洛谷P2147传送门 1A了,给自己鼓鼓掌 #include <cstdio> #include <algorit ...
- 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 ...
- [BZOJ2049][Sdoi2008]Cave 洞穴勘测 LCT模板
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 9705 Solved: 4674[Submit] ...
- 【BZOJ2049】 [Sdoi2008]Cave 洞穴勘测 LCT/并查集
两种方法: 1.LCT 第一次LCT,只有link-cut和询问,无限T,到COGS上找了数据,发现splay里的父亲特判出错了(MD纸张),A了,好奇的删了反转T了.... #include < ...
- [BZOJ2049] [SDOI2008] Cave 洞穴勘测 (LCT)
Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好 ...
- 【bzoj2049】[Sdoi2008]Cave 洞穴勘测 LCT
题目描述 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假如 ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测——LCT
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2049 省选之前来切一道数据结构模板题. 题意 这是一道模板题. N个点,M次操作,每次加边/ ...
随机推荐
- string 与char* char[]之间的转换
1.首先必须了解,string可以被看成是以字符为元素的一种容器.字符构成序列(字符串).有时候在字符序列中进行遍历,标准的string类提供了STL容器接口.具有一些成员函数比如begin().en ...
- linux查看磁盘空间
首先如果需要查看整个磁盘还剩多少空间,可以使用命令: df -h 如果你并不关心磁盘还剩余多少空间,只是需要知道当前的文件夹下的磁盘使用情况,可以使用如下命令: -h 上面使用du --max-dep ...
- php的一些小笔记-文件函数(2)
---恢复内容开始--- copy 文件的复制 echo copy('test.php','test1.php'); 如果成功的返回true,反之返回false 如何在多层目录中复制文件呢?也就是根据 ...
- 处理emacs-org模式TODO的一个脚本
处理前: 处理后: Table of Contents 1 前言 2 中文的处理 2.1 vim相关 2.2 perl 相关 3 时间相关 4 程序解析 1 前言 最近风帆问我一个问题,也就是处理or ...
- RFC3261--sip
本文转载自 http://www.ietf.org/rfc/rfc3261.txt 中文翻译可参考 http://wenku.baidu.com/view/3e59517b1711cc7931b716 ...
- 算法的稳定性(Stability of Sorting Algorithms)
如果具有同样关键字的纪录的在排序前和排序后相对位置保持不变.一些算法本身就是稳定的,如插入排序,归并排序,冒泡排序等,不稳定的算法有堆排序,快速排序等. 然而,一个本身不稳定的算法通过一点修正也能变成 ...
- 一个跨域请求的XSS漏洞
场景回顾 一个表单进行跨域提交的方式有很多,我们使用的采用隐藏iframe,在本域下放一个代理页面,通过服务端配合完成一次完整的请求. 首先,部署proxy.html代理页面.这个页面处理服务端返回的 ...
- MySQL 索引优化全攻略
所谓索引就是为特定的mysql字段进行一些特定的算法排序,比如二叉树的算法和哈希算法,哈希算法是通过建立特征值,然后根据特征值来快速查找.而用的最多,并且是mysql默认的就是二叉树算法 BTREE, ...
- hdu 3232 Crossing Rivers(期望 + 数学推导 + 分类讨论,水题不水)
Problem Description You live in a village but work in another village. You decided to follow the s ...
- linux文件权限整理
网上对linux文件权限的已经很多,不过还是要自己整理一下,不然每次都要查资料. linux下所有东西都是文件,包括设备,所以这里的文件也包括文件夹. 先是查看文件权限:ls -lh xzc@xzc- ...