题解 洛谷P2147/BZOJ2049【[SDOI2008]洞穴勘测】
Link-Cut-Tree的模板题啊......(听说还可以用其他的方法做,不管了,直接上LCT)
没有要求维护点权,只需要维护点的连通性即可。
就是朴素的LCT,居然还不要pushup。
感觉有些不适应啊.......不得不说LCT是个神器。
简单分析一下。
- 对于每种命令:
如果是
Connect x y (链接 x y):直接link(x,y)即可。如果是
Destroy x y (切断 x y):直接cut(x,y)即可。如果是
Query x y (询问 x y 的连通性):判断findroot(x)和findroot(y)是否一致,一致输出Yes,否则输出No。
然后就A了.......
Code:
#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define RI register int
#define A printf("A")
#define C printf(" ")
using namespace std;
const int N=2e5+2;
template <typename Tp> inline void IN(Tp &x){
int f=1;x=0;char ch=getchar();
while(ch<'0'||ch>'9')if(ch=='-')f=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();x*=f;
}int n,m,f[N],r[N],hep[N],ch[N][2];
inline int chk(int x){return ch[f[x]][1]==x;}
inline int get(int x){return ch[f[x]][0]==x||ch[f[x]][1]==x;}
inline void filp(int x){swap(ch[x][0],ch[x][1]);r[x]^=1;}
inline void pushdown(int x){
if(!r[x])return;r[x]=0;
if(ch[x][0])filp(ch[x][0]);
if(ch[x][1])filp(ch[x][1]);
}
inline void rotate(int x){
int y=f[x],z=f[y],k=chk(x),&v=ch[x][!k];
if(get(y))ch[z][chk(y)]=x;v=y,ch[y][k]=v;
if(v)f[v]=y;f[y]=x,f[x]=z;return;
}
inline void Splay(int x){
int y=x,top=0;hep[++top]=y;
while(get(y))hep[++top]=y=f[y];
while(top)pushdown(hep[top--]);
while(get(x)){
y=f[x],top=f[y];
if(get(y))rotate((ch[y][0]==x)^(ch[top][0]==y)?x:y);
rotate(x);
}return;
}
inline void Access(int x){
for(register int y=0;x;x=f[y=x])
Splay(x),ch[x][1]=y;
}
inline void makeroot(int x){
Access(x);Splay(x);filp(x);
}
inline int findroot(int x){
Access(x);Splay(x);
while(ch[x][0])pushdown(x),x=ch[x][0];
return x;
}
inline void split(int x,int y){
makeroot(x);Access(y);Splay(y);
}
inline void link(int x,int y){
makeroot(x);if(findroot(y)!=x)f[x]=y;
}
inline void cut(int x,int y){
makeroot(x);
if(findroot(y)==x&&f[x]==y&&!ch[x][1]){
f[x]=ch[y][0]=0;
}return;
}char op[10];
int main(){
scanf("%d%d",&n,&m);
for(register int x,y,i=1;i<=m;++i){
scanf("%s%d%d",op,&x,&y);
if(op[0]=='C')link(x,y);
else if(op[0]=='D')cut(x,y);
else if(op[0]=='Q'){
if(findroot(x)==findroot(y))printf("Yes\n");
else printf("No\n");
}
}return 0;
}
题解 洛谷P2147/BZOJ2049【[SDOI2008]洞穴勘测】的更多相关文章
- 【洛谷 P2147】 [SDOI2008]洞穴勘测(LCT)
题目链接 LCT裸题.. #include <cstdio> #define R register int #define I inline void #define lc c[x][0] ...
- [BZOJ2049] [SDOI2008] 洞穴勘测
题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假 ...
- BZOJ2049[Sdoi2008]洞穴勘测——LCT
题目描述 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假如 ...
- 题解 洛谷P2158 【[SDOI2008]仪仗队】
本文搬自本人洛谷博客 题目 本文进行了一定的更新 优化了 Markdown 中 Latex 语句的运用,加强了可读性 补充了"我们仍不曾知晓得 消失的 性质5 ",加强了推导的严谨 ...
- BZOJ-2049 [SDOI2008]洞穴勘测
LCT模版题.... #include <cstdlib> #include <cstdio> #include <cstring> #include <al ...
- P2147 [SDOI2008]洞穴勘测(LCT)
P2147 [SDOI2008]洞穴勘测 裸的LCT. #include<iostream> #include<cstdio> #include<cstring> ...
- P2147 [SDOI2008]洞穴勘测
P2147 [SDOI2008]洞穴勘测 思路 没办法,我就是喜欢板子都想发的人 都是基础操作,不多说了 代码 #include <bits/stdc++.h> #define ls ch ...
- 【BZOJ2049】洞穴勘测(Link-Cut Tree)
[BZOJ2049]洞穴勘测(Link-Cut Tree) 题面 题目描述 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别 ...
- BZOJ 2049 [SDOI2008]洞穴勘测 (LCT)
题目大意:维护一个森林,支持边的断,连,以及查询连通性 LCT裸题 洛谷P2147传送门 1A了,给自己鼓鼓掌 #include <cstdio> #include <algorit ...
随机推荐
- Java Socket实战之一:单线程通信
转自:http://developer.51cto.com/art/201202/317543.htm 现在做Java直接使用Socket的情况是越来越少,因为有很多的选择可选,比如说可以用sprin ...
- 2. Ext中关于Ext.QuickTips.init()的使用
转自:http://www.cnblogs.com/jianglan/archive/2011/08/26/2154120.html 在extJS的例子中,大部分都在程序第一行使用了如下语句:Ext. ...
- CollabNetSubversionEdge 4.0.4教程
CollabNetSubversionEdge是svn的集成环境,集合subversion,apache,viewvc, 参考网址:http://blog.miniasp.com/post/2011/ ...
- cardboard sdk for unity 系统分析 - 对像与类
一.cardboard通过monobehavior派生的脚本组件与UNITY3D联系起来: 二.相关的类及其关系如下图:
- Thinkpad x230i安装Ubuntu10.04发生no network devices available
这个是由于10.04版本没有集成x230i的网卡驱动导致,需要到http://sourceforge.net/projects/e1000/f ... %20stable/下载最新版本驱动,并安装,之 ...
- (矩阵快速幂)51NOD 1242斐波那契数列的第N项
斐波那契数列的定义如下: F(0) = 0 F(1) = 1 F(n) = F(n - 1) + F(n - 2) (n >= 2) (1, 1, 2, 3, 5, 8, 13, 21, ...
- Taro 小程序 自定义导航栏
在小程序中,有的页面需求可能需要我们做一个自定义的导航栏, 今天就来踩一踩坑 首先需要在app.js 中给全局的导航栏隐藏, // app.js window: { navigationStyle: ...
- 给Ambari集群里安装可视化分析利器工具Hue步骤(图文详解)
扩展博客 以下,是我在手动的CDH版本平台下,安装Hue. CDH版本大数据集群下搭建Hue(hadoop-2.6.0-cdh5.5.4.gz + hue-3.9.0-cdh5.5.4.tar.gz) ...
- 在xampp集成环境下使用 thinkphp 连接oracle
今天搞了大半天,终于成功了. 1. 首先需要让xampp支持oracle,直接按这个网页上说的做就行.http://nimal.info/blog/2009/activate-oracle-on-xa ...
- COT2 - Count on a tree II(树上莫队)
COT2 - Count on a tree II You are given a tree with N nodes. The tree nodes are numbered from 1 to N ...