zoukankan      html  css  js  c++  java
  • 【BZOJ2049】[SDOI2008] Cave 洞穴勘测(LCT维护连通性)

    点此看题面

    大致题意:(n)个洞穴,(3)种操作:连一条边,删一条边,询问两点是否联通。

    (LCT)维护连通性

    这道题应该是(LCT)动态维护连通性的一道模板题。

    考虑将(x)(y)连边时,我们就在(LCT)(Link(x,y))

    同理,(x)(y)断边时,就(Cut(x,y))

    询问连通性时,只要判断(FindRoot(x))(FindRoot(y))是否相等即可。

    代码

    #include<bits/stdc++.h> 
    #define N 10000 
    #define swap(x,y) (x^=y^=x^=y) 
    using namespace std; 
    int n; 
    class Class_FIO 
    { 
        private: 
            #define Fsize 100000 
            #define tc() (A==B&&(B=(A=Fin)+fread(Fin,1,Fsize,stdin),A==B)?EOF:*A++) 
            char ch,*A,*B,Fin[Fsize]; 
        public: 
            Class_FIO() {A=B=Fin;} 
            inline void read(int &x) {x=0;while(!isdigit(ch=tc()));while(x=(x<<3)+(x<<1)+(ch&15),isdigit(ch=tc()));} 
            inline void reads(string &x) {x="";while(isspace(ch=tc()));while(x+=ch,!isspace(ch=tc()));} 
    }F; 
    class Class_LCT//LCT模板
    { 
        private: 
            #define LCT_SIZE N 
            #define Rever(x) (swap(node[x].Son[0],node[x].Son[1]),node[x].Rev^=1) 
            #define PushDown(x) (node[x].Rev&&(Rever(node[x].Son[0]),Rever(node[x].Son[1]),node[x].Rev=0)) 
            #define Which(x) (node[node[x].Father].Son[1]==x) 
            #define Connect(x,y,d) (node[node[x].Father=y].Son[d]=x) 
            #define IsRoot(x) (node[node[x].Father].Son[0]^x&&node[node[x].Father].Son[1]^x) 
            #define MakeRoot(x) (Access(x),Splay(x),Rever(x)) 
            int Stack[LCT_SIZE+5]; 
            struct Tree 
            { 
                int Father,Rev,Son[2]; 
            }node[LCT_SIZE+5]; 
            inline void Rotate(int x) 
            { 
                register int fa=node[x].Father,pa=node[fa].Father,d=Which(x); 
                !IsRoot(fa)&&(node[pa].Son[Which(fa)]=x),node[x].Father=pa,Connect(node[x].Son[d^1],fa,d),Connect(fa,x,d^1); 
            } 
            inline void Splay(int x) 
            { 
                register int fa=x,Top=0; 
                while(Stack[++Top]=fa,!IsRoot(fa)) fa=node[fa].Father; 
                while(Top) PushDown(Stack[Top]),--Top; 
                while(!IsRoot(x)) fa=node[x].Father,!IsRoot(fa)&&(Rotate(Which(x)^Which(fa)?x:fa),0),Rotate(x); 
            } 
            inline void Access(int x) {for(register int son=0;x;x=node[son=x].Father) Splay(x),node[x].Son[1]=son;} 
            inline int FindRoot(int x) {Access(x),Splay(x);while(node[x].Son[0]) PushDown(x),x=node[x].Son[0];return Splay(x),x;} 
        public: 
            inline void Link(int x,int y) {MakeRoot(x),FindRoot(y)^x&&(node[x].Father=y);} 
            inline void Cut(int x,int y) {MakeRoot(x),!(FindRoot(y)^x)&&!(node[y].Father^x)&&!node[y].Son[0]&&(node[y].Father=node[x].Son[1]=0);} 
            inline bool IsConnected(int x,int y) {return !(FindRoot(x)^FindRoot(y));}//判断连通性
    }LCT; 
    int main() 
    { 
        register int query_tot,x,y;register string op; 
        for(F.read(n),F.read(query_tot);query_tot;--query_tot)  
        { 
            F.reads(op),F.read(x),F.read(y); 
            switch(op[0]) 
            { 
                case 'C':LCT.Link(x,y);break;//连边
                case 'D':LCT.Cut(x,y);break;//删边
                case 'Q':puts(LCT.IsConnected(x,y)?"Yes":"No");break;//询问
            } 
        } 
        return 0; 
    }
    
  • 相关阅读:
    解决JDBC连接MySQL 8时得异常:java.sql.SQLException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone.
    洛谷P2604(最大流+最小费用最大流)
    poj2411(状压dp)
    二轮前水题计划
    最近挖的坑
    关于我
    future
    mysql学习笔记
    vue踩坑记
    XSS漏洞学习笔记
  • 原文地址:https://www.cnblogs.com/chenxiaoran666/p/BZOJ2049.html
Copyright © 2011-2022 走看看