洛谷 2147 SDOI2008 Cave 洞穴勘测

【题解】
动态树模板题,只要求维护森林的连通性,直接上板子即可。
#include<cstdio>
#include<algorithm>
#define N 500010
#define ls (c[u][0])
#define rs (c[u][1])
using namespace std;
int n,m;
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
struct Link_Cut_Tree{
int top,c[N][],fa[N],rev[N],q[N];
inline void pushdown(int u){if(rev[u]) rev[ls]^=,rev[rs]^=,rev[u]^=,swap(ls,rs);}
inline bool isroot(int u){return c[fa[u]][]!=u&&c[fa[u]][]!=u;}
inline bool which(int u){return c[fa[u]][]==u;}
void rotate(int u){
int f=fa[u],gf=fa[f],wh=which(u);
if(!isroot(f)){c[gf][which(f)]=u;}
fa[u]=gf; fa[f]=u; fa[c[u][wh^]]=f;
c[f][wh]=c[u][wh^]; c[u][wh^]=f;
}
void splay(int u){
top=; q[top]=u;
for(int i=u;!isroot(i);i=fa[i]) q[++top]=fa[i];
for(int i=top;i;i--) pushdown(q[i]);
while(!isroot(u)){
if(!isroot(fa[u])) rotate(which(u)==which(fa[u])?fa[u]:u);
rotate(u);
}
}
void access(int u){for(int son=;u;son=u,u=fa[u]) splay(u),c[u][]=son;}
void makeroot(int u){
access(u); splay(u); rev[u]^=;
}
int find(int u){
access(u); splay(u);
while(ls) u=ls; return u;
}
void split(int x,int y){
makeroot(x); access(y); splay(y);
}
void cut(int x,int y){
split(x,y);
if(c[y][]==x) c[y][]=,fa[x]=;
}
void link(int x,int y){
makeroot(x); fa[x]=y;
}
}t;
int main(){
n=read(); m=read();
while(m--){
char c=getchar();
while(c!='C'&&c!='D'&&c!='Q') c=getchar();
char c2=getchar();
while(c2!=' ') c2=getchar();
int x=read(),y=read(),xrt=t.find(x),yrt=t.find(y);
if(c=='C') if(xrt!=yrt) t.link(x,y);
if(c=='D') if(xrt==yrt) t.cut(x,y);
if(c=='Q') puts(xrt==yrt?"Yes":"No");
}
return ;
}
洛谷 2147 SDOI2008 Cave 洞穴勘测的更多相关文章
- 洛谷P2147 [SDOI2008]Cave 洞穴勘测
题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假 ...
- 【bzoj2049】[Sdoi2008]Cave 洞穴勘测 link-cut-tree
2016-05-30 11:04:51 学习了link-cut-tree 二中神犇封禹的讲义感觉讲的超级清晰易懂啊(没有的可以q窝 算是模板吧 #include<bits/stdc++.h&g ...
- 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 洞穴勘测 动态树
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 3119 Solved: 1399[Submit] ...
- 【LCT】BZOJ2049 [SDOI2008]Cave 洞穴勘测
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 10059 Solved: 4863[Submit ...
- BZOJ_2049_[Sdoi2008]Cave 洞穴勘测_LCT
BZOJ_2049_[Sdoi2008]Cave 洞穴勘测_LCT Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由 ...
- bzoj 2049: [Sdoi2008]Cave 洞穴勘测 (LCT)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2049 题面: 2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 1 ...
- BZOJ 2049: [Sdoi2008]Cave 洞穴勘测 (动态树入门)
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1528 Solved: 644[Submit][ ...
- 2049: [Sdoi2008]Cave 洞穴勘测
2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 7475 Solved: 3499 [Submi ...
随机推荐
- 命令行下mysql的部分操作
远程链接数据库: mysql –u用户名 [–h主机名或者IP地址] –p密码 (用户名是登录的用 户,主机名或者IP地址为可选项,如果是本地连接则不需要,远程连接需要填写,密码是对应用户的密码.) ...
- dotnet core 文档链接
The installation was successful The following were installed at C:\Program Files\dotnet • .NET Core ...
- StackExchange.Redis 使用资料
在StackExchange.Redis中最重要的对象是ConnectionMultiplexer类, 它存在于StackExchange.redis命名空间中.这个类隐藏了Redis服务的操作细节, ...
- [LeetCode] Unique Binary Search Tree
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- Flink之Window Operation
目录 Configuring Time Characteristics Process Functions Window Operators Applying Functions on Windows ...
- PCB genesis孔符制作实现方法
一.先看genesis原始孔符 孔符的作用:用于表示孔径的大小的一种代号, 当孔径检测时,可以按分孔图中的孔符对应的孔径尺寸对孔径检测. 在实际PCB行业通常不使用原始(图形)孔符,而使用字母孔符(如 ...
- PCB 使用Nginx让IIS7实现负载均衡
在PCB行业众多系统中PCB工程系统是主要的数据生产者, 它与外部系统数据交互是最多的,经统计接口数超过100个之多;这么多接口调用与管理起来是混乱的,所以今年年初对工程集成方式改造,将原来的点对点的 ...
- php使用163邮箱发送邮件
email.class.php文件 <? class smtp { /* Public Variables */ var $smtp_port; var $time_out; var $host ...
- [App Store Connect帮助]三、管理 App 和版本(6.1)转让 App:App 转让概述
当您将某个 App 出售给其他开发者,或想要将其移至其他 App Store Connect 帐户或组织时,您需要转让该 App. 您无需将 App 从 App Store 下架,即可将其所有权转让给 ...
- Codeforces 718C 线段树+矩乘
题意: 维护一个序列,支持两种操作:1.区间[l,r]的权值+x2.询问区间[l,r]的函数和,即∑fib(x)这里的函数即斐波那契函数数据范围:1≤n,q≤105 思路:一般求斐波那契函数的方法可以 ...