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次操作,每次加边/ ...
随机推荐
- java基础知识3
58.线程的基本概念.线程的基本状态以及状态之间的关系线程指在程序执行过程中,能够执行程序代码的一个执行单位,每个程序至少都有一个线程,也就是程序本身.Java中的线程有四种状态分别是:运行.就绪.挂 ...
- ZigZag-LeetCode
题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows l ...
- jQuery插件学习(一)
由于项目开发需要,经常会用到一些jquery插件,但网上已有的插件常常又不能100%满足业务需求,所以就想自己能看懂插件的代码,进行一些功能上的改动和补充,或者能自己自定义插件就更好了.所以这段时间会 ...
- destoon控制标题长度,title中显示全标题 标题字符长度怎么控制?
如题商品调用出来后,标题的字符长度怎么控制?有哪位高手能帮我解决吗? 小弟在此感谢了. &length=30 //30表示30个字节 <!--{tag("moduleid=5& ...
- DataTables选择行并删除(删除单行)
$(document).ready(function() { var table = $('#example').DataTable(); $('#example tbody').on( 'click ...
- Hql 子查询
直接上代码: public virtual IList<VoucherLog> GetMaxResultVoucherLog() { string orgaizationCode = Ht ...
- eclipse编辑工具小结
eclipse编辑工具小结 这两天从myeclipse转入eclipse,整体感觉不错,速度更快些,也没在出现各种意外的调试错误.不能断点等情况,并且对整个编辑环境的使用有了更深入的认识,再次对主要几 ...
- ASCII码、base64编码 为什么有的代码要用 base64 进行编码?
百度百科 ASCII码:http://baike.baidu.com/link?url=bNtzytBhlSUt_l3pwpfICxCxqgAfqsBMaeWX6QF7gH46Tg4pQtKM2aAV ...
- mysql 中文配置(转)
Dos下连接mysql后,运行一下几项就可以插入中文了: SET character_set_client = gbk; SET character_set_connection = gbk; SET ...
- mysql-error --(ERROR 1135 (HY000): Can't create a new thread (errno 11); if you are not out)
报错信息: ERROR 1135 (HY000): Can't create a new thread (errno 11); if you are not out 解决办法: # 查看限制情况 [r ...