[SDOI2008]洞穴勘测
嘟嘟嘟
写完lct的板儿后觉得这就是一道大水题。
连pushup都不用。
不过还是因为一个zz的错误debug了一小会儿(Link的时候连出自环……)
还有一件事就是Cut的时候判断条件还得加上,因为我这种写法会改变树的形态,不加后面就删错边了。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e4 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, m;
char c[10];
struct Tree
{
int ch[2], fa, rev;
}t[maxn];
In void change(int now)
{
swap(t[now].ch[0], t[now].ch[1]); t[now].rev ^= 1;
}
In void pushdown(int now)
{
if(t[now].rev)
{
if(t[now].ch[0]) change(t[now].ch[0]);
if(t[now].ch[1]) change(t[now].ch[1]);
t[now].rev = 0;
}
}
In bool n_root(int x)
{
return t[t[x].fa].ch[0] == x || t[t[x].fa].ch[1] == x;
}
void rotate(int x)
{
int y = t[x].fa, z = t[y].fa, k = (t[y].ch[1] == x);
if(n_root(y)) t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;
t[y].ch[k] = t[x].ch[k ^ 1]; t[t[x].ch[k ^ 1]].fa = y;
t[x].ch[k ^ 1] = y; t[y].fa = x;
}
int st[maxn], top = 0;
In void splay(int x)
{
int y = x;
st[top = 1] = y;
while(n_root(y)) y = t[y].fa, st[++top] = y;
while(top) pushdown(st[top--]);
while(n_root(x))
{
int y = t[x].fa, z = t[y].fa;
if(n_root(y)) rotate(((t[z].ch[0] == y) ^ (t[y].ch[0] == x)) ? x : y);
rotate(x);
}
}
In void access(int x)
{
int y = 0;
while(x)
{
splay(x); t[x].ch[1] = y;
y = x; x = t[x].fa;
}
}
In int find_root(int x)
{
access(x); splay(x);
while(t[x].ch[0]) pushdown(x), x = t[x].ch[0];
return x;
}
In void make_root(int x)
{
access(x); splay(x);
change(x);
}
In void Link(int x, int y)
{
make_root(x);
if(find_root(y) != x) t[x].fa = y;
}
In void Cut(int x, int y)
{
make_root(x);
if(find_root(y) == x && t[x].fa == y && !t[x].ch[1])
t[x].fa = t[y].ch[0] = 0;
}
In bool query(int x, int y)
{
make_root(x);
return find_root(y) == x;
}
int main()
{
n = read(); m = read();
for(int i = 1; i <= m; ++i)
{
scanf("%s", c); int x = read(), y = read();
if(c[0] == 'C') Link(x, y);
else if(c[0] == 'D') Cut(x, y);
else puts(query(x, y) ? "Yes" : "No");
}
return 0;
}
[SDOI2008]洞穴勘测的更多相关文章
- P2147 [SDOI2008]洞穴勘测(LCT)
P2147 [SDOI2008]洞穴勘测 裸的LCT. #include<iostream> #include<cstdio> #include<cstring> ...
- P2147 [SDOI2008]洞穴勘测
P2147 [SDOI2008]洞穴勘测 思路 没办法,我就是喜欢板子都想发的人 都是基础操作,不多说了 代码 #include <bits/stdc++.h> #define ls ch ...
- BZOJ2049[Sdoi2008]洞穴勘测——LCT
题目描述 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假如 ...
- 洛谷P2147[SDOI2008]洞穴勘测(lct)
题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假 ...
- [BZOJ2049] [SDOI2008] 洞穴勘测
题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假 ...
- BZOJ2049:[SDOI2008]洞穴勘测——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2049 https://www.luogu.org/problemnew/show/P2147 辉辉热 ...
- LG3690 【模板】Link Cut Tree 和 SDOI2008 洞穴勘测
UPD:更新了写法. [模板]Link Cut Tree 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 后接两个整数(x,y),代表询问从x到y ...
- [洛谷P2147][SDOI2008]洞穴勘测
题目大意:有$n$个洞穴,$m$条指令,指令有三种 $Connect\;u\;v$:在$u,v$之间连一条边 $Destroy\;u\;v$:切断$u,v$之间的边 $Query\;u\;v$:询问$ ...
- 【题解】Luogu P2147 [SDOI2008]洞穴勘测
原题传送门 这题用Link-Cut-Tree解决,Link-Cut-Tree详解 我不太会踩爆Link-Cut-Tree的并查集做法qaq 我们用Link-Cut-Tree维护连通性(十分无脑) Co ...
随机推荐
- LINUX 下Jexus部署ASP.NET Core WebApi
服务器:LINUX ubuntu16.04 开发软件:VS2015 Update3 dotnet sdk: DotNetCore.1.0.0-VS2015Tools.Preview2 1. ...
- 数据结构与算法--最短路径之Bellman算法、SPFA算法
数据结构与算法--最短路径之Bellman算法.SPFA算法 除了Floyd算法,另外一个使用广泛且可以处理负权边的是Bellman-Ford算法. Bellman-Ford算法 假设某个图有V个顶点 ...
- redis服务部署脚本
yum install -y gcc jemalloc-devel cd /usr/local/src curl -L -O http://download.redis.io/releases/red ...
- C# 免客户端访问Oracle的DLL
代码示例: OracleConnection con = new OracleConnection(); con.ConnectionString ="user ...
- 自定义MVC框架之工具类-图像处理类
截止目前已经改造了4个类: ubuntu:通过封装验证码类库一步步安装php的gd扩展 自定义MVC框架之工具类-分页类的封装 自定义MVC框架之工具类-文件上传类 图像处理类: 1,图片加水印处理( ...
- HDU6198
number number number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- mysql小试题3
查询结果:
- BOM(JavaScript高程笔记)
再次编辑于20160115 一.window对象 双重角色 JS访问浏览器窗口的接口 ECAMAscript规定的Global对象 1.全局作用域 所有在全局作用域中声明的变量.函数都会变成windo ...
- PDO异常处理
PDO提供了三种处理错误的方式 PDO::ERRMODE_SILENT:静默模式(默认) PDO::ERRMODE_WARNING:警告模式 PDO::ERRMODE_EXCEPTION:异常模式 示 ...
- 字符串以及for循环
1.基本数据类型概况 1, int整数 2, str字符串 3, bool类型 4, list列表,一般存放大量数据["明星XXX","NBA球星XXX"]里边 ...