zoukankan      html  css  js  c++  java
  • 洛谷 P2147 [SDOI2008]洞穴勘测 LCT

    Code:

    #include <cstdio>
    #include <algorithm>
    #include <string>
    #include <cstring>
    
    
    
    using namespace std;
    
    void setIO(string a){
        freopen((a+".in").c_str(),"r",stdin);
    }
    
    #define maxn 10008
    
    int n,m;
    struct Link_Cut_Tree{
    
        int ch[maxn][2],f[maxn];
        int tag[maxn],sta[maxn];
        int get(int x)
        {
            return ch[f[x]][1]==x;
        }
        int which(int x)
        {
            return ch[f[x]][1]==x;
        }
        int isRoot(int x)
        {
            return !(ch[f[x]][1]==x||ch[f[x]][0]==x);
        }
        int lson(int x)
        {
            return ch[x][0];
        }
        int rson(int x)
        {
            return ch[x][1];
        }
        void mark(int x)
        {
            if(!x) return;
            swap(ch[x][0],ch[x][1]), tag[x]^=1;
        }
        void pushdown(int x)
        {
            if(tag[x]) mark(lson(x)), mark(rson(x)),tag[x]=0;
        }
        void rotate(int x)
        {
            int old=f[x],fold=f[old],which=get(x);
            if(!isRoot(old)) ch[fold][ch[fold][1]==old]=x;
            ch[old][which]=ch[x][which^1],f[ch[old][which]]=old;
            ch[x][which^1]=old,f[old]=x,f[x]=fold;
        }
        void splay(int x)
        {
            int v=0,u=x;
            sta[++v]=u;
            while(!isRoot(u)) sta[++v]=f[u],u=f[u];
            while(v) pushdown(sta[v--]);
            u=f[u];
            for(int fa;(fa=f[x])!=u;rotate(x))
                if(f[fa]!=u) rotate(get(fa)==get(x)?fa:x);
        }
        void Access(int x)
        {
            for(int y=0;x;y=x,x=f[x])
                splay(x),ch[x][1]=y;
        }
        void makeRoot(int x)
        {
            Access(x), splay(x), mark(x);
        }
        void link(int a,int b)
        {
            makeRoot(a), f[a]=b;
        }
        int findRoot(int a)
        {
            Access(a),splay(a);
            while(ch[a][0]) a=ch[a][0];
            return a;
        }
        void cut(int a,int b)
        {
            makeRoot(a),Access(b),splay(b);
            f[a]=ch[b][0]=0;
        }
        bool judge(int a,int b)
        {
            int x=findRoot(a),y=findRoot(b);
            if(x!=y)return true;
            return false;
        }
    }tree;
    
    int main()
    {
        //setIO("input");
        scanf("%d%d",&n,&m);
        char opt[20];
        int a,b;
        while(m--)
        {
            scanf("%s%d%d",opt,&a,&b);
            switch(opt[0])
            {
                case 'C': 
                {
                    if(tree.judge(a,b)) tree.link(a,b);
                    break;
                }
                case 'Q':
                {
                    bool ans=tree.judge(a,b);
                    if(ans==0) printf("Yes
    ");
                    else printf("No
    ");
                    break;
                }
                case 'D':
                {
                    tree.cut(a,b);
                    break;
                }
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    vue 项目中assets文件夹与static文件夹引用的区别
    v-on绑定特性命名带小横杠 ‘-’与props属性中变量怎么对应
    解决 The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
    解决win10无法完成更新 正在撤销更改
    Felix HttpServer call iPojo Demo
    Felix Http server Demo
    osgi学习
    windows一个目录下最大文件数目
    oracle默认配置ora文件位置
    iptables配置(/etc/sysconfig/iptables)
  • 原文地址:https://www.cnblogs.com/guangheli/p/10066136.html
Copyright © 2011-2022 走看看