天天看點

【BZOJ2049】【SDOI2008】洞穴勘測(LCT)

Description

click me

Solution

直接LCT維護即可。。

Code

/*****************
Au: Hany01
Date: Dec 31th, 2017
Prob: bzoj2049 & sdoi2008 cave
Email: [email protected]
*****************/


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define rep(i , j) for (int i = 0 , i##_end_ = j; i < i##_end_ ; ++ i)
#define For(i , j , k) for (int i = (j) , i##_end_ = (k) ; i <= i##_end_ ; ++ i)
#define Fordown(i , j , k) for (int i = (j) , i##_end_ = (k) ; i >= i##_end_ ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#ifdef hany01
#define debug(...) fprintf(stderr , __VA_ARGS__)
#else
#define debug(...)
#endif

inline void file()
{
#ifdef hany01 
    freopen("bzoj2049.in" , "r" , stdin);
    freopen("bzoj2049.out" , "w" , stdout);
#endif
}

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b,  : ; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b,  : ; }

char c_; int _ , __;
inline int read()
{
    for (_ =  , __ =  , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-')  __ = -;
    for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << ) + (_ << ) + (c_ ^ );
    return _ * __;
}

const int maxn = ;

#define dir(x) (ch[fa[x]][0] != (x))
#define isrt(x) (ch[fa[x]][0] != x && ch[fa[x]][1] != x)

struct Link_Cut_Trees
{

    int fa[maxn], ch[maxn][], rev[maxn];

    inline void pushdown(int o)
    {
        if (rev[o]) {
            rev[ch[o][]] ^= ; rev[ch[o][]] ^= ;
            swap(ch[o][], ch[o][]); rev[o] = ;
        }
    }

    inline void rotate(int o)
    {
        register int f = fa[o], gf = fa[f], d = dir(o);
        fa[ch[f][d] = ch[o][d ^ ]] = f;
        fa[o] = gf;
        if (!isrt(f)) ch[gf][dir(f)] = o;
        ch[fa[f] = o][d ^ ] = f; 
    }

    int top, stk[maxn];
    inline void splay(int o)
    {
        stk[top = ] = o;
        for (register int t = o; !isrt(t); t = fa[t]) stk[++ top] = fa[t];//fa[t]不能寫成了t
        while (top) pushdown(stk[top --]);
        for ( ; !isrt(o); rotate(o)) if (!isrt(fa[o])) rotate(dir(o) == dir(fa[o]) ? fa[o] : o);
    }

    inline void access(int o)
    {
        for (register int t = ; o; t = o, o = fa[o]) splay(o), ch[o][] = t;
    }

    inline void makeroot(int o) { access(o), splay(o), rev[o] ^= ; }

    inline int findroot(int o)
    {
        access(o), splay(o);
        while (ch[o][]) o = ch[o][];
        return o;
    }

    inline void link(int x, int y)
    {
        if (findroot(x) == findroot(y)) return ;
        makeroot(x), fa[x] = y;
    }

    inline void cut(int x, int y)
    {
        makeroot(x), access(y), splay(y);
        if (ch[y][] == x) ch[y][] = , fa[x] = ; 
    }

    inline void isconnected(int x, int y)
    {
        if (findroot(x) == findroot(y)) puts("Yes"); else puts("No");
    }

}lct;

int main()
{
    file();
    register int n = read(), m = read(), x, y;
    register char op[];
    while (m --) {
        scanf("%s", op); x = read(); y = read();
        if (*op == 'C') lct.link(x, y);
        else if (*op == 'D') lct.cut(x, y);
        else lct.isconnected(x, y);
    }
    return ;
}
//漁舟逐水愛山春,兩岸桃花夾古津。
//    -- 王維《桃源行》
           

繼續閱讀