zoukankan      html  css  js  c++  java
  • bzoj2049: [Sdoi2008]Cave 洞穴探测

    bzoj2049: [Sdoi2008]Cave 洞穴探测

    给n个点,每次连接两个点或切断一条边,保证是树结构,多次询问两个点是否联通

    Lct裸题

    //Achen
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<cstdio>
    #include<vector>
    #include<queue>
    #include<ctime>
    #include<cmath>
    const int N=200000+7;
    typedef long long LL;
    using namespace std;
    int n,m,ch[N][2],p[N],flip[N];
    char op[20];
    
    template<typename T> void read(T &x) {
        char ch=getchar(); x=0; T f=1;
        while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
        if(ch=='-') f=-1,ch=getchar();
        for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
    }
    
    #define lc ch[x][0]
    #define rc ch[x][1]
    void down(int x) {
        if(!flip[x]) return;
        swap(lc,rc);
        flip[lc]^=1;
        flip[rc]^=1;
        flip[x]^=1;
    } 
    
    int isroot(int x) {return (ch[p[x]][0]!=x&&ch[p[x]][1]!=x);}
    
    void rotate(int x) {
        int y=p[x],z=p[y],l=(x==ch[y][1]),r=l^1;
        if(!isroot(y)) ch[z][y==ch[z][1]]=x; p[x]=z;
        ch[y][l]=ch[x][r]; p[ch[x][r]]=y;
        ch[x][r]=y; p[y]=x;
    }
    
    void splay(int x) {
        static int g[N],top=0,tp;
        for(tp=x;!isroot(tp);tp=p[tp]) g[++top]=tp;
        g[++top]=tp;
        while(top) down(g[top--]);
        for(;!isroot(x);rotate(x)) {
            int y=p[x],z=p[y];
            if(!isroot(y))
                ((x==ch[y][1])^(y==ch[z][1]))?rotate(x):rotate(y); 
        }
    }
    
    void access(int x) {
        for(int t=0;x;x=p[t=x]) {
            splay(x);
            ch[x][1]=t;
        }
    }
    
    int findroot(int x) {
        access(x);
        splay(x);
        while(lc) x=lc;
        return x;
    }
    
    void newroot(int x) {
        access(x);
        splay(x);
        flip[x]^=1;
    }
    
    void lik(int x,int y) {
        newroot(x);
        splay(x);
        p[x]=y;
    }
    
    void del(int x,int y) {
        newroot(x);
        access(y);
        splay(y);
        if(ch[y][0]==x) ch[y][0]=p[x]=0; 
    }
    
    int main() {
    #ifdef DEBUG
        freopen(".in","r",stdin);
        freopen(".out","w",stdout);
    #endif
        read(n); read(m);
        while(m--) {
            scanf("%s",op);
            int x,y;
            read(x); read(y);
            if(op[0]=='Q') {
                if(findroot(x)==findroot(y)) printf("Yes
    ");
                else printf("No
    ");
            }
            if(op[0]=='C') lik(x,y);
            if(op[0]=='D') del(x,y);
        }
        return 0;
    }
    /*
    200 5
    Query 123 127
    Connect 123    127
    Query 123 127
    Destroy    127    123
    Query 123 127
    */
    View Code
  • 相关阅读:
    毕设进度28
    任务27
    任务26
    任务25
    任务24
    第二次冲刺
    课堂作业-搜狗输入法
    课堂作业-寻找水王
    博客花园典型用户和场景
    第十天
  • 原文地址:https://www.cnblogs.com/Achenchen/p/8021441.html
Copyright © 2011-2022 走看看