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

90 篇文章 0 订阅
11 篇文章 0 订阅

Description

click me

Solution

直接LCT维护即可。。

Code

/*****************
Au: Hany01
Date: Dec 31th, 2017
Prob: bzoj2049 & sdoi2008 cave
Email: hany01@foxmail.com
*****************/


#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, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

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

const int maxn = 300005;

#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][2], rev[maxn];

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

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

    int top, stk[maxn];
    inline void splay(int o)
    {
        stk[top = 1] = 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 = 0; o; t = o, o = fa[o]) splay(o), ch[o][1] = t;
    }

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

    inline int findroot(int o)
    {
        access(o), splay(o);
        while (ch[o][0]) o = ch[o][0];
        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][0] == x) ch[y][0] = 0, fa[x] = 0; 
    }

    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[10];
    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 0;
}
//渔舟逐水爱山春,两岸桃花夹古津。
//    -- 王维《桃源行》
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值