zoukankan      html  css  js  c++  java
  • 【bzoj2049】[Sdoi2008]Cave 洞穴勘测

    题目描述:
    辉辉热衷于洞穴勘测。某天,他按照地图来到了一片被标记为JSZX的洞穴群地区。经过初步勘测,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成,并且每条通道连接了恰好两个洞穴。假如两个洞穴可以通过一条或者多条通道按一定顺序连接起来,那么这两个洞穴就是连通的,按顺序连接在一起的这些通道则被称之为这两个洞穴之间的一条路径。洞穴都十分坚固无法破坏,然而通道不太稳定,时常因为外界影响而发生改变,比如,根据有关仪器的监测结果,123号洞穴和127号洞穴之间有时会出现一条通道,有时这条通道又会因为某种稀奇古怪的原因被毁。辉辉有一台监测仪器可以实时将通道的每一次改变状况在辉辉手边的终端机上显示:如果监测到洞穴u和洞穴v之间出现了一条通道,终端机上会显示一条指令 Connect u v 如果监测到洞穴u和洞穴v之间的通道被毁,终端机上会显示一条指令 Destroy u v 经过长期的艰苦卓绝的手工推算,辉辉发现一个奇怪的现象:无论通道怎么改变,任意时刻任意两个洞穴之间至多只有一条路径。因而,辉辉坚信这是由于某种本质规律的支配导致的。因而,辉辉更加夜以继日地坚守在终端机之前,试图通过通道的改变情况来研究这条本质规律。然而,终于有一天,辉辉在堆积成山的演算纸中崩溃了……他把终端机往地面一砸(终端机也足够坚固无法破坏),转而求助于你,说道:“你老兄把这程序写写吧”。辉辉希望能随时通过终端机发出指令 Query u v,向监测仪询问此时洞穴u和洞穴v是否连通。现在你要为他编写程序回答每一次询问。已知在第一条指令显示之前,JSZX洞穴群中没有任何通道存在。
    输入:
    第一行为两个正整数n和m,分别表示洞穴的个数和终端机上出现过的指令的个数。以下m行,依次表示终端机上出现的各条指令。每行开头是一个表示指令种类的字符串s(”Connect”、”Destroy”或者”Query”,区分大小写),之后有两个整数u和v (1≤u, v≤n且u≠v) 分别表示两个洞穴的编号。
    输出:
    对每个Query指令,输出洞穴u和洞穴v是否互相连通:是输出”Yes”,否则输出”No”。(不含双引号)
    样例输入:

    样例输入1 cave.in
    200 5
    Query 123 127
    Connect 123 127
    Query 123 127
    Destroy 127 123
    Query 123 127

    样例输入2 cave.in
    3 5
    Connect 1 2
    Connect 3 1
    Query 2 3
    Destroy 1 3
    Query 2 3

    样例输出:

    样例输出1 cave.out
    No
    Yes
    No

    样例输出2 cave.out
    Yes
    No

    题解:
    lct模板题。话说这道题我因为半期考试等各种原因调了两个礼拜(因为被强制拉去读文化课我也好几个礼拜没写blog了)。。。link和cut操作是大模板,然后查询连通性就往上找一下父亲就可以了。查询操作大概长这样子:

    inline bool find(R Node *that)
        {
            access();
            splay();
            while (that -> fa != null)
                that = that -> fa;
            return that == this;
        }

    完整代码:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    
    #ifdef WIN32
        #define LL "%I64d"
    #else
        #define LL "%lld"
    #endif
    
    #ifdef CT
        #define debug(...) printf(__VA_ARGS__)
        #define setfile() 
    #else
        #define debug(...)
        #define filename ""
        #define setfile() freopen(filename".in", "r", stdin); freopen(filename".out", "w", stdout);
    #endif
    
    #define R register
    #define getc() (S == T && (T = (S = B) + fread(B, 1, 1 << 15, stdin), S == T) ? EOF : *S++)
    #define dmax(_a, _b) ((_a) > (_b) ? (_a) : (_b))
    #define dmin(_a, _b) ((_a) < (_b) ? (_a) : (_b))
    #define cmax(_a, _b) (_a < (_b) ? _a = (_b) : 0)
    #define cmin(_a, _b) (_a > (_b) ? _a = (_b) : 0)
    char B[1 << 15], *S = B, *T = B;
    inline int FastIn()
    {
        R char ch; R int cnt = 0; R bool minus = 0;
        while (ch = getc(), (ch < '0' || ch > '9') && ch != '-') ;
        ch == '-' ? minus = 1 : cnt = ch - '0';
        while (ch = getc(), ch >= '0' && ch <= '9') cnt = cnt * 10 + ch - '0';
        return minus ? -cnt : cnt;
    }
    #define maxn 200010
    int n, m;
    struct Node *null;
    struct Node
    {
        bool rev;
        Node *ch[2], *fa;
        inline bool type()
        {
            return fa -> ch[1] == this;
        }
        inline bool check()
        {
            return this == fa -> ch[type()];
        }
        inline void set_rev()
        {
            std::swap(ch[0], ch[1]);
            rev ^= 1;
        }
        inline void pushdown()
        {
            if (rev)
            {
                ch[0] -> set_rev();
                ch[1] -> set_rev();
                rev = 0;
            }
        }
        void pushdownall()
        {
            if (check())
                fa -> pushdownall();
            pushdown();
        }
        inline void rotate()
        {
            R Node *f = fa;
            R bool d = type();
            (fa = f -> fa), f -> check() ? fa -> ch[f -> type()] = this : 0;
            (f -> ch[d] = ch[!d]) != null ? ch[!d] -> fa = f : 0;
            (ch[!d] = f) -> fa = this;
        }
        inline void splay(R bool need = 1)
        {
            if (need) pushdownall();
            for (; check(); rotate())
                if (fa -> check())
                    (type() != fa -> type() ? this : fa) -> rotate();
        }
        inline Node *access()
        {
            R Node *i = this, *j = null;
            for (; i != null; i = (j = i) -> fa)
            {
                i -> splay();
                i -> ch[1] = j;
            }
            return j;
        }
        inline void make_root()
        {
            access();
            splay(0);
            set_rev();
        }
        inline void link(R Node *that)
        {
            make_root();
            fa = that;
        }
        inline void cut(R Node *that)
        {
            make_root();
            that -> access();
            splay(0);
            that -> fa = ch[1] = null;
        }
        inline bool find(R Node *that)
        {
            access();
            splay();
            while (that -> fa != null)
                that = that -> fa;
            return that == this;
        }
    }mem[maxn];
    int main()
    {
    //  setfile();
        n = FastIn(), m = FastIn();
        null = mem;
        null -> fa = null -> ch[0] = null -> ch[1] = null;
        for (R int i = 1; i <= n; ++i) mem[i] = (Node) {0, {null, null}, null};
        for (R int i = 1; i <= m; ++i)
        {
            R char opt;
            while (opt = getc(), opt < 'A' || opt > 'Z');
            R int a = FastIn(), b = FastIn();
            if (opt == 'C')
            {
                (mem + a) -> link(mem + b);
            }
            else if (opt == 'D')
            {
                (mem + a) -> cut(mem + b);
            }
            else
            {
                puts((mem + a) -> find(mem + b) ? "Yes" : "No");
            }
        }
        return 0;
    }
    /*
    200 5
    Query   123 127
    Connect 123 127
    Query   123 127
    Destroy 127 123
    Query   123 127
    3 5
    Connect 1 2
    Connect 3 1
    Query   2 3
    Destroy 1 3
    Query   2 3
    */
  • 相关阅读:
    android29
    android28
    android27
    android26
    Dynamics CRM2011 MspInstallAction failed when installing an Update Rollup
    Dynamics CRM Import Solution Attribute Display Name description is null or empty
    The service cannot be activated because it does not support ASP.NET compatibility
    IIS部署WCF报 无法读取配置节“protocolMapping”,因为它缺少节声明
    Unable to access the IIS metabase.You do not have sufficient privilege
    LM算法与非线性最小二乘问题
  • 原文地址:https://www.cnblogs.com/cocottt/p/5550967.html
Copyright © 2011-2022 走看看