• [SDOI 2008] 洞穴勘测


    [题目链接]

             https://www.lydsy.com/JudgeOnline/problem.php?id=2049

    [算法]

              LCT动态维护森林连通性

              时间复杂度 : O(NlogN ^ 2)

    [代码]

            

    #include<bits/stdc++.h>
    using namespace std;
    #define MAXN 300010
    
    int n , m;
    
    struct Link_Cut_Tree
    {
        struct Node
        {
            int father , son[2];
            bool rev;
        } a[MAXN];
        inline void pushdown(int x)
        {
            if (a[x].rev)
            {
                swap(a[x].son[0] , a[x].son[1]);
                a[a[x].son[0]].rev ^= 1;
                a[a[x].son[1]].rev ^= 1;
                a[x].rev = false;
            }
        }
        inline bool get(int x)
        {
            pushdown(a[x].father);
            return a[a[x].father].son[1] == x;
        }
        inline bool nroot(int x)
        {
            return a[a[x].father].son[0] == x | a[a[x].father].son[1] == x;
        }
        inline void rotate(int x)
        {
            int f = a[x].father , g = a[f].father;                        
            int tmpx = get(x) , tmpf = get(f);
            int w = a[x].son[tmpx ^ 1];
            if (nroot(f)) a[g].son[tmpf] = x;
            a[x].son[tmpx ^ 1] = f;
            a[f].son[tmpx] = w;
            if (w) a[w].father = f;
            a[f].father = x;
            a[x].father = g;
        }
        inline void splay(int x)
        {
            int y = x , z = 0;
            static int st[MAXN];
            st[++z] = y;
            while (nroot(y))
                st[++z] = y = a[y].father;
            while (z) pushdown(st[z--]);
            while (nroot(x))
            {
                int y = a[x].father , z = a[y].father;
                if (nroot(y))
                    rotate((a[y].son[0] == x) ^ (a[z].son[0] == y) ? (y) : (x));
                rotate(x);
            }
        }
        inline void access(int x)
        {
            for (int y = 0; x; x = a[y = x].father)
            {
                splay(x);
                a[x].son[1] = y;
            }
        }
        inline void make_root(int x)
        {
            access(x);
            splay(x);
            a[x].rev ^= 1;
        }
        inline void cut(int x , int y)
        {
            make_root(x);
            if (find_root(y) == x && a[x].father == y && !a[x].son[1])
                a[x].father = a[y].son[0] = 0;
        }
        inline void link(int x , int y)
        {
            make_root(x);
            if (find_root(y) != x) a[x].father = y;
        }
        inline int find_root(int x)
        {
            access(x);
            splay(x);
            while (a[x].son[0])
            {
                pushdown(x);
                x = a[x].son[0];
            }
            return x;
        }
    } LCT;
    
    int main()
    {
        
        scanf("%d%d" , &n , &m);
        for (int i = 1; i <= m; i++)
        {
            char type[10];
            scanf("%s" , &type);
            if (type[0] == 'C')
            {
                int x , y;
                scanf("%d%d" , &x , &y);
                LCT.link(x , y);    
            } else if (type[0] == 'D')
            {
                int x , y;
                scanf("%d%d" , &x , &y);
                LCT.cut(x , y);
            } else
            {
                int x , y;
                scanf("%d%d" , &x , &y);
                if (LCT.find_root(x) == LCT.find_root(y)) printf("Yes
    ");
                else printf("No
    ");
            }
        }
            
        return 0;
    }
  • 相关阅读:
    【月度盘点】《金秋10月》
    selenium简单使用
    数据解析模块BeautifulSoup简单使用
    爬虫简介
    SQLAlchemy简介
    Flask Blueprint
    Flask基于websocket的简单聊天室
    Flask send_file request
    初识Flask
    Python pip简单使用
  • 原文地址:https://www.cnblogs.com/evenbao/p/10088932.html
Copyright © 2020-2023  润新知