Loading [Contrib]/a11y/accessibility-menu.js


BZOJ2049 [Sdoi2008]Cave 洞穴勘测 LCT

欢迎访问~原文出处——博客园-zhouzhendong

去博客园看该题解


题目传送门 - BZOJ2049


题意概括

  有一堆点,一开始没有连边。

  有3种操作,一种是连接某两个点,一种是断开某一条边。还有一种是询问两个点是否连通。

  操作过程中保证整个图是森林。

  点数<=10000,操作数<=200000


题解

  LCT板子题。

  对于询问,我们只需要access一下,然后splay一下,然后比较所在连通块的最左位置就可以了。


代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=10005;
int n,m,fa[N],son[N][2],size[N],rev[N];
void pushup(int x){
    size[x]=size[son[x][0]]+size[son[x][1]]+1;
}
bool isroot(int x){
    return son[fa[x]][0]!=x&&son[fa[x]][1]!=x;
}
void pushdown(int x){
    if (rev[x]){
        rev[x]=0;
        rev[son[x][0]]^=1;
        rev[son[x][1]]^=1;
        swap(son[x][0],son[x][1]);
    }
}
void pushadd(int x){
    if (!isroot(x))
        pushadd(fa[x]);
    pushdown(x);
}
int wson(int x){
    return son[fa[x]][1]==x;
}
void rotate(int x){
    if (isroot(x))
        return;
    int y=fa[x],z=fa[y],L=wson(x),R=L^1;
    if (!isroot(y))
        son[z][wson(y)]=x;
    fa[x]=z;
    fa[y]=x;
    fa[son[x][R]]=y;
    son[y][L]=son[x][R];
    son[x][R]=y;
    pushup(y);
    pushup(x);
}
void splay(int x){
    pushadd(x);
    for (int y=fa[x];!isroot(x);rotate(x),y=fa[x])
        if (!isroot(y))
            rotate(wson(x)==wson(y)?y:x);
}
void access(int x){
    int t=0;
    while (x){
        splay(x);
        son[x][1]=t;
        t=x;
        x=fa[x];
    }
}
void rever(int x){
    access(x);
    splay(x);
    rev[x]^=1;
}
void link(int x,int y){
    rever(x);
    fa[x]=y;
}
void cut(int x,int y){
    rever(x);
    access(y);
    splay(y);
    fa[x]=son[y][0]=0;
}
int find(int x){
    access(x);
    splay(x);
    while (son[x][0])
        x=son[x][0];
    return x;
}
int main(){
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)
        size[i]=1,rev[i]=fa[i]=son[i][0]=son[i][1]=0;
    while (m--){
        char op[10];
        int a,b;
        scanf("%s%d%d",op,&a,&b);
        if (op[0]=='C')
            link(a,b);
        else if (op[0]=='D')
            cut(a,b);
        else
            puts(find(a)==find(b)?"Yes":"No");
    }
    return 0;
}

  

posted @   zzd233  阅读(228)  评论(0编辑  收藏  举报
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)

点击右上角即可分享
微信分享提示