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次操作,每次加边/ ...
随机推荐
- HDU 5904 - LCIS (BestCoder Round #87)
HDU 5904 - LCIS [ DP ] BestCoder Round #87 题意: 给定两个序列,求它们的最长公共递增子序列的长度, 并且这个子序列的值是连续的 分析: 状态转移方程式 ...
- Broadcom网卡linux系统下无法连接到网络问题(某种情况- -||)的解决办法
国际惯例,先闲扯: 其实我是个电脑白痴,至于为什么一个电脑白痴会来做开发呢?原因非常匪夷所思且简单--“因为做开发看起来很酷!”,那为什么要一把年纪了才来做开发呢?原因更简单,“当我回过神,就发现我已 ...
- EF 执行顺序--先删除在更新和添加
public void AcceptAllChanges() { if (this.ObjectStateManager.SomeEntryWithConceptualNullExists()) { ...
- 在树莓派上安装leanote
作者:冥王星 "noright0@163.com" 前言 宿舍有个树莓派2B,连接到电视机,安装OSMC系统,USB接口连接移动硬盘一块,平时用来BT下载和看电影.美剧. OSMC ...
- AngularJS 父子控制器
<!doctype html> <html ng-app="myApp"> <head> <script src="C:\\Us ...
- Linux - Reset a MySQL root password
Use the following steps to reset a MySQL root password by using the command line interface. Stop the ...
- Core Data (一)备
序 恩,用Core Data也有一段时间了.大大小小的坑也都坑过了.重来没有认真的记录一次.这次需要好好的理一理Core Data.就当一次绝好的机会记录下来.也为了自己加深认识. 为什么要用Core ...
- asp.net mvc4中自定义404
原文地址:http://www.chuchur.com/asp-net-mvc4-404/ 定义404 方法当然有很多种.不同的方法所展现的形式也不一样,用户所体验也不一样.以下提供2两种 方法一: ...
- Spark Streaming--实战篇
摘要: Sprak Streaming属于Saprk API的扩展,支持实时数据流(live data streams)的可扩展,高吞吐(hight-throughput) 容错(fault ...
- Javascript常用正则表达式
一.校验数字的表达式 数字:^[0-9]*$ n位的数字:^\d{n}$ 至少n位的数字:^\d{n,}$ m-n位的数字:^\d{m,n}$ 零和非零开头的数字:^(0|[1-9][0-9]*)$ ...