BZOJ2049:[SDOI2008]洞穴勘测——题解

http://www.lydsy.com/JudgeOnline/problem.php?id=2049

https://www.luogu.org/problemnew/show/P2147

辉辉热衷于洞穴勘测。

某天,他按照地图来到了一片被标记为JSZX的洞穴群地区。经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴。假如两个洞穴可以通过一条或者多条通道按一定顺序连接起来,那么这两个洞穴就是连通的,按顺序连接在一起的这些通道则被称之为这两个洞穴之间的一条路径。 洞穴都十分坚固无法破坏,然而通道不太稳定,时常因为外界影响而发生改变,比如,根据有关仪器的监测结果,123号洞穴和127号洞穴之间有时会出现一条通道,有时这条通道又会因为某种稀奇古怪的原因被毁。

辉辉有一台监测仪器可以实时将通道的每一次改变状况在辉辉手边的终端机上显示:

如果监测到洞穴u和洞穴v之间出现了一条通道,终端机上会显示一条指令 Connect u v

如果监测到洞穴u和洞穴v之间的通道被毁,终端机上会显示一条指令 Destroy u v

经过长期的艰苦卓绝的手工推算,辉辉发现一个奇怪的现象:无论通道怎么改变,任意时刻任意两个洞穴之间至多只有一条路径。

因而,辉辉坚信这是由于某种本质规律的支配导致的。因而,辉辉更加夜以继日地坚守在终端机之前,试图通过通道的改变情况来研究这条本质规律。 然而,终于有一天,辉辉在堆积成山的演算纸中崩溃了……他把终端机往地面一砸(终端机也足够坚固无法破坏),转而求助于你,说道:“你老兄把这程序写写吧”。

辉辉希望能随时通过终端机发出指令 Query u v,向监测仪询问此时洞穴u和洞穴v是否连通。现在你要为他编写程序回答每一次询问。 已知在第一条指令显示之前,JSZX洞穴群中没有任何通道存在。

LCT模板题,你可以很轻松的百度出一个LCT写法教程,这里就不多言了。

提供一个模板代码。

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cctype>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
const int N=1e4+5;
int n,m,r,fa[N],tr[N][2],rev[N],q[N];
inline int read(){
    int X=0,w=0;char ch=0;
    while(!isdigit(ch)){w|=ch==‘-‘;ch=getchar();}
    while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
inline bool get(int x){
    return tr[fa[x]][1]==x;
}
inline bool isroot(int x){
    if(!fa[x])return 1;
    return tr[fa[x]][0]!=x&&tr[fa[x]][1]!=x;
}
inline void pushrev(int x){
    if(!rev[x])return;
    swap(tr[x][0],tr[x][1]);
    if(tr[x][0])rev[tr[x][0]]^=1;
    if(tr[x][1])rev[tr[x][1]]^=1;
    rev[x]=0;
}
inline void rotate(int x){
    int y=fa[x],z=fa[y],b=tr[y][0]==x?tr[x][1]:tr[x][0];
    if(z&&!isroot(y))(tr[z][0]==y?tr[z][0]:tr[z][1])=x;
    fa[x]=z;fa[y]=x;b?fa[b]=y:0;
    if(tr[y][0]==x)tr[x][1]=y,tr[y][0]=b;
    else tr[x][0]=y,tr[y][1]=b;
}
inline void splay(int x){
    q[r=0]=x;
    for(int y=x;!isroot(y);y=fa[y])q[++r]=fa[y];
    for(int i=r;i>=0;i--)pushrev(q[i]);
    while(!isroot(x)){
    if(!isroot(fa[x]))
        rotate((get(x)==get(fa[x])?fa[x]:x));
    rotate(x);
    }
}
inline void access(int x){
    for(int y=0;x;y=x,x=fa[x]){
    splay(x);tr[x][1]=y;
    if(y)fa[y]=x;
    }
}
inline int findroot(int x){
    access(x);splay(x);
    while(pushrev(x),tr[x][0])x=tr[x][0];
    splay(x);
    return x;
}
inline void makeroot(int x){
    access(x);splay(x);
    rev[x]^=1;
}
inline void link(int x,int y){
    makeroot(x);fa[x]=y;
}
inline void cut(int x,int y){
    makeroot(x);access(y);splay(y);
    tr[y][0]=0;fa[x]=0;
}
int main(){
    n=read(),m=read();
    for(int i=1;i<=m;i++){
    char ch=getchar();
    while(ch<‘A‘||ch>‘Z‘)ch=getchar();
    if(ch==‘Q‘){
        if(findroot(read())==findroot(read()))puts("Yes");
        else puts("No");
    }else if(ch==‘C‘)link(read(),read());
    else cut(read(),read());
    }
    return 0;
}

+++++++++++++++++++++++++++++++++++++++++++

+本文作者:luyouqi233。               +

+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+

+++++++++++++++++++++++++++++++++++++++++++

原文地址:https://www.cnblogs.com/luyouqi233/p/8469233.html

时间: 2024-08-04 14:49:14

BZOJ2049:[SDOI2008]洞穴勘测——题解的相关文章

BZOJ2049 SDOI2008 洞穴勘测 LCT

题意:给定一棵树,维护:1.删除一条边  2.添加一条边  3.询问u和v是否连通 题解:LCT维护连通性 #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; const int MAXN=10000+2; typedef struct NODE{ NODE *ch

BZOJ-2049 [Sdoi2008]洞穴勘测

LCT模版题.... #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <cctype> #include <cmath> #define rep(i, l, r) for(int i=l; i<=r; i++) #define clr(x, c) mems

P2147 [SDOI2008]洞穴勘测

题目 P2147 [SDOI2008]洞穴勘测 做法 说实话如果你在看这篇题解的话应该也没有人劝你回去打模板吧 My complete code #include<cstdio> #include<cstring> #include<iostream> #include<string> using namespace std; typedef int LL; const LL maxn=1e6; inline LL Read(){ LL x(0),f(1);

【题解】Luogu P2147 [SDOI2008]洞穴勘测

原题传送门 这题用Link-Cut-Tree解决,Link-Cut-Tree详解 我不太会踩爆Link-Cut-Tree的并查集做法qaq 我们用Link-Cut-Tree维护连通性(十分无脑) Connect操作:把u,v两个点连起来 Destroy操作:把u,v两个点分开来 Query操作:判断在这个森林里u的根和v的根是否相等 #include <bits/stdc++.h> #define N 10005 using namespace std; inline int read() {

【BZOJ2049】洞穴勘测(LCT)

题意:一张图,要求支持以下操作: 1.加边 2.删边 3.询问两点之间是否联通 100%的数据满足n≤10000, m≤200000 思路:LCT裸题,不需要维护任何信息 1 var t:array[0..500000,0..1]of longint; 2 fa,rev,q:array[0..500000]of longint; 3 n,m,i,x,y,k,j,top,s:longint; 4 ch:string; 5 6 procedure swap(var x,y:longint); 7 v

bzoj-2049 Cave 洞穴勘测

题意: 给出n个点和m次操作,操作有三种: 1.连接两个点: 2.删除两个点之间的连接: 3.查询两个点是否连通: 保证任何时刻图为一个森林: n<=10000,m<=200000: 题解: LCT模板题,看起来LCT是个好东西: LCT是一个用数据结构来维护一个支持动态加边删边的森林: 内部实现是用多个Splay来维护树上的重链,用Splay间的边维护树上的轻边: 这里的定义与树剖不同了,重边与轻边指的是在不在Splay内: 上一幅好图: 辅助树就是我们要维护的东西: 图中双向边所连接的是一

[SDOI2008]洞穴勘测

SOL: LCT维护联通性,当然,启发式合并并查集可能会更好写. #include<bits/stdc++.h> #define N 200007 int rev[N],ch[N][2],f[N],q[N],top; using namespace std; inline void push_down(int x) { if (rev[x]) { rev[ch[x][0]]^=1; rev[ch[x][1]]^=1; rev[x]=0; swap(ch[x][0],ch[x][1]); } }

[LuoguP2147] [SDOI2008]洞穴勘测 (LCT维护连通性)

题面 传送门:https://www.luogu.org/problemnew/show/P2147 Solution 这题...... 我们可以发现题目要求我们维护一个动态森林,而且只查询连通性.... 显然LCT模板题啊,关于LCT玩法,可以猛戳这里学习 Code #include<iostream> #include<cstdio> #include<vector> using namespace std; long long read() { long long

【LCT】BZOJ2049 [SDOI2008]Cave 洞穴勘测

2049: [Sdoi2008]Cave 洞穴勘测 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 10059  Solved: 4863[Submit][Status][Discuss] Description 辉辉热衷于洞穴勘测.某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假如两个洞穴可以通过一条或者多条通道按一定顺序连接起