【SDOI2008】【BZOJ2049】Cave 洞穴勘测

【题目链接】

  • BZOJ2049

【前置技能】

  • LCT

【题解】

  • LCT模板题,支持link,cut,以及判断树上两点间的连通性。
  • 时间复杂度 O ( Q l o g N ) O(QlogN) O(QlogN)

【代码】

#include
#define INF 0x3f3f3f3f
#define LL  long long
#define MAXN    10010
using namespace std;
int n, m;
char s[20];
 
template <typename T> void chkmin(T &x, T y){x = min(x, y);}
template <typename T> void chkmax(T &x, T y){x = max(x, y);}
template <typename T> void read(T &x){
    x = 0; int f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch)) {x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}
 
struct Link_Cut_Tree{
    struct info{int son[2], up, fa, tag;}a[MAXN];
    int cnt;
    int get(int pos){
        return (pos == a[a[pos].fa].son[1]);
    }
    void push_down(int pos){
        if (!a[pos].tag) return;
        swap(a[pos].son[0], a[pos].son[1]);
        if (a[pos].son[0]) a[a[pos].son[0]].tag ^= 1;
        if (a[pos].son[1]) a[a[pos].son[1]].tag ^= 1;
        a[pos].tag = 0;
    }
    void rotate(int pos){
        int fa = a[pos].fa, gr = a[fa].fa;
        a[pos].up = a[fa].up, a[fa].up = 0;
        push_down(fa), push_down(pos);
        int x = get(pos), f = get(fa);
        if (a[pos].son[x ^ 1]) a[a[pos].son[x ^ 1]].fa = fa;
        a[fa].son[x] = a[pos].son[x ^ 1];
        a[fa].fa = pos;
        a[pos].son[x ^ 1] = fa;
        if (gr) a[gr].son[f] = pos;
        a[pos].fa = gr;
    }
    void splay(int pos){
        push_down(pos);
        for (int fa = a[pos].fa; (fa = a[pos].fa) != 0; rotate(pos))
            if (a[fa].fa != 0) {
                if (get(pos) == get(fa)) rotate(fa);
                else rotate(pos);
            }
    }
    void access(int x){
        splay(x);
        int tmp = a[x].son[1];
        if (tmp) a[tmp].fa = 0, a[tmp].up = x;
        a[x].son[1] = 0;
        while (a[x].up){
            int f = a[x].up;
            splay(f);
            int tmp = a[f].son[1];
            if (tmp) a[tmp].fa = 0, a[tmp].up = f;
            a[f].son[1] = x, a[x].up = 0, a[x].fa = f;
            splay(x);
        }
    }
    void reverse(int x){
        access(x);
        a[x].tag ^= 1;
        push_down(x);
    }
    void link(int x, int y){
        access(x), reverse(y);
        a[x].son[1] = y;
        a[y].fa = x;
    }
    void cut(int x, int y){
        reverse(x), access(y), splay(x);
        a[x].son[1] = 0, a[y].fa = 0;
    }
    int findroot(int x){
        access(x);
        push_down(x);
        while (a[x].son[0]) {
            x = a[x].son[0];
            push_down(x);
        }
        splay(x);
        return x;
    }
}lct;
 
int main(){
    read(n), read(m);
    while (m--){
        scanf("%s", s);
        int u, v;
        read(u), read(v);
        if (s[0] == 'Q') {
            if (lct.findroot(u) == lct.findroot(v)) printf("Yes\n");
            else printf("No\n");
        } else if (s[0] == 'D') {
            lct.cut(u, v);
        } else {
            lct.link(u, v);
        }
    } 
    return 0;
}

你可能感兴趣的:(【类型】做题记录)