bzoj 2049 洞穴勘测 lct

2049: [Sdoi2008]Cave 洞穴勘测

Time Limit: 10 Sec  Memory Limit: 259 MB
Submit: 11475  Solved: 5683
[Submit][Status][Discuss]

Description

辉辉热衷于洞穴勘测。某天,他按照地图来到了一片被标记为JSZX的洞穴群地区。经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴。假如两个洞穴可以通过一条或者多条通道按一定顺序连接起来,那么这两个洞穴就是连通的,按顺序连接在一起的这些通道则被称之为这两个洞穴之间的一条路径。洞穴都十分坚固无法破坏,然而通道不太稳定,时常因为外界影响而发生改变,比如,根据有关仪器的监测结果,123号洞穴和127号洞穴之间有时会出现一条通道,有时这条通道又会因为某种稀奇古怪的原因被毁。辉辉有一台监测仪器可以实时将通道的每一次改变状况在辉辉手边的终端机上显示:如果监测到洞穴u和洞穴v之间出现了一条通道,终端机上会显示一条指令 Connect u v 如果监测到洞穴u和洞穴v之间的通道被毁,终端机上会显示一条指令 Destroy u v 经过长期的艰苦卓绝的手工推算,辉辉发现一个奇怪的现象:无论通道怎么改变,任意时刻任意两个洞穴之间至多只有一条路径。因而,辉辉坚信这是由于某种本质规律的支配导致的。因而,辉辉更加夜以继日地坚守在终端机之前,试图通过通道的改变情况来研究这条本质规律。然而,终于有一天,辉辉在堆积成山的演算纸中崩溃了……他把终端机往地面一砸(终端机也足够坚固无法破坏),转而求助于你,说道:“你老兄把这程序写写吧”。辉辉希望能随时通过终端机发出指令 Query u v,向监测仪询问此时洞穴u和洞穴v是否连通。现在你要为他编写程序回答每一次询问。已知在第一条指令显示之前,JSZX洞穴群中没有任何通道存在。

Input

第一行为两个正整数n和m,分别表示洞穴的个数和终端机上出现过的指令的个数。以下m行,依次表示终端机上出现的各条指令。每行开头是一个表示指令种类的字符串s("Connect”、”Destroy”或者”Query”,区分大小写),之后有两个整数u和v (1≤u, v≤n且u≠v) 分别表示两个洞穴的编号。

Output

对每个Query指令,输出洞穴u和洞穴v是否互相连通:是输出”Yes”,否则输出”No”。(不含双引号)

Sample Input

样例输入1 cave.in
200 5
Query 123 127
Connect 123 127
Query 123 127
Destroy 127 123
Query 123 127
样例输入2 cave.in

3 5
Connect 1 2
Connect 3 1
Query 2 3
Destroy 1 3
Query 2 3


 

Sample Output

样例输出1 cave.out
No
Yes
No


样例输出2 cave.out

Yes
No

lct维护动态集合

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
 
#define ll long long
#define lson tr[x][0]
#define rson tr[x][1]
const int maxn = 1e4 + 10;
const int INF = 1e9;
int top;
int  fa[maxn], tr[maxn][2];
int tag[maxn], ve[maxn];
char op[10];
 
int judge(int x) {
    return tr[fa[x]][1] == x;
}
 
int isroot(int x) {
    return (tr[fa[x]][0] != x && tr[fa[x]][1] != x);
}
 
void pushdown(int x) {
    if (tag[x]) {
        tag[lson] ^= 1;
        tag[rson] ^= 1;
        swap(lson, rson);
        tag[x] = 0;
    }
}
 
void rotate(int x) {
    int y = fa[x], d = judge(x);
    if (tr[y][d] = tr[x][d ^ 1]) fa[tr[y][d]] = y;
    if ((fa[x] = fa[y]) && !isroot(y)) tr[fa[y]][judge(y)] = x;
    tr[fa[y] = x][d ^ 1] = y;
}
 
void splay(int x) {
    for (int y = ve[top = 1] = x; !isroot(y); y = fa[y])
        ve[++top] = fa[y];
    while (top) pushdown(ve[top--]);
    for (int y; !isroot(x); rotate(x))
        if (!isroot(y = fa[x])) rotate(judge(x) == judge(y) ? y : x);
}
 
void access(int x) {
    for (int y = 0; x; x = fa[y = x]) {
        splay(x);
        tr[x][1] = y;
    }
}
 
void makeroot(int x) {
    access(x);
    splay(x);
    tag[x] ^= 1;
}
 
int findroot(int x) {
    access(x);
    splay(x);
    pushdown(x);
    while (lson) pushdown(x = lson);
    return x;
}
 
int query(int x, int y) {
    return findroot(x) == findroot(y);
}
 
void link(int x, int y) {
    makeroot(x);
    if (findroot(y) != x) fa[x] = y;
}
 
void cut(int x, int y) {
    makeroot(x);
    if (findroot(y) == x && fa[x] == y && tr[y][0] == x && !tr[x][1]) { 
        fa[x] = tr[y][0] = 0;
    }
}
 
int main() {
    int n, m, u, v;
    while (scanf("%d%d", &n, &m) != EOF) {
        memset(fa, 0, sizeof(fa));
        memset(tag, 0, sizeof(tag));
        memset(tr, 0, sizeof(tr));
        for (int i = 0; i < m; i++) {
            scanf("%s", op);
            if (op[0] == 'D') {
                scanf("%d%d", &u, &v);
                cut(u, v);
            }
            else if (op[0] == 'C') {
                scanf("%d%d", &u, &v);
                link(u, v);
            }
            else if (op[0] == 'Q') {
                scanf("%d%d", &u, &v);
                if (query(u, v)) printf("Yes\n");
                else printf("No\n");
            }
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/KIDGIN7439/article/details/85931857