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);char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+c-'0',c=getchar();
    return x*f;
}
LL n,m;
LL son[maxn][2],fa[maxn],r[maxn];
inline void Update(LL x){return;}
inline bool Notroot(LL x){
    return son[fa[x]][0]==x||son[fa[x]][1]==x;
}
inline void Pushr(LL x){
    swap(son[x][0],son[x][1]);r[x]^=1;
}
inline void Pushdown(LL x){
    if(r[x]){
        if(son[x][0])Pushr(son[x][0]);
        if(son[x][1])Pushr(son[x][1]);
        r[x]=0;
    }
}
inline void Rotate(LL x){
    LL y(fa[x]),lz=(son[y][1]==x),z(fa[y]);
    if(Notroot(y))
        son[z][son[z][1]==y]=x;fa[x]=z;
    son[y][lz]=son[x][lz^1];
    if(son[y][lz]) fa[son[y][lz]]=y;
    son[x][lz^1]=y; fa[y]=x;
    Update(y),Update(x);
}
LL sta[maxn];
inline void Splay(LL x){
    LL y(x),top(0);
    sta[++top]=y;
    while(Notroot(y)) sta[++top]=y=fa[y];
    while(top) Pushdown(sta[top--]);
    while(Notroot(x)){
        y=fa[x];
        if(Notroot(y)){
            LL z(fa[y]);
            if(((son[y][1]==x)^(son[z][1]==y))==0) Rotate(y);
            else Rotate(x);
        }
        Rotate(x);
    }
}
inline void Access(LL x){
    for(LL y=0;x;y=x,x=fa[x]){
        Splay(x); son[x][1]=y; Update(x);
    }
}
inline void Makeroot(LL x){
    Access(x),Splay(x),Pushr(x);
}
inline void Split(LL x,LL y){
    Makeroot(x),Access(y),Splay(y);
}
inline void Link(LL x,LL y){
    Makeroot(x);
    fa[x]=y;
}
inline void Delet(LL x,LL y){
    Split(x,y);
    fa[x]=son[y][0]=0;
    Update(y);
}
inline LL Findroot(LL x){
    Access(x),Splay(x);
    while(son[x][0])Pushdown(x),x=son[x][0];
    Splay(x);
    return x;
}
char s[maxn];
int main(){
    n=Read(),m=Read();
    while(m--){
        scanf(" %s",s);
        LL x(Read()),y(Read());
        if(s[0]=='Q'){
            Makeroot(x);
            if(Findroot(y)==x)
                printf("Yes\n");
            else
                printf("No\n");
        }else if(s[0]=='D')
            Delet(x,y);
        else
            Link(x,y);
    }
}/*
200 5
Query 123 127
Connect 123 127
Query 123 127
Destroy 127 123
Query 123 127
*/

原文地址:https://www.cnblogs.com/y2823774827y/p/10323613.html

时间: 2024-08-02 16:07:01

P2147 [SDOI2008]洞穴勘测的相关文章

【题解】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:[SDOI2008]洞穴勘测——题解

http://www.lydsy.com/JudgeOnline/problem.php?id=2049 https://www.luogu.org/problemnew/show/P2147 辉辉热衷于洞穴勘测. 某天,他按照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴.假如两个洞穴可以通过一条或者多条通道按一定顺序连接起来,那么这两个洞穴就是连通的,按顺序连接在一起的这些通道则被称之为

[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

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

[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]); } }

洛谷P2147 [SDOI2008]Cave 洞穴勘测

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

BZOJ 2049([Sdoi2008]Cave 洞穴勘测-LCT)[Template:LCT]

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

BZOJ 题目2049: [Sdoi2008]Cave 洞穴勘测(link cut tree)

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