Bzoj3510:首都

Sol

LCT L C T 动态维护树重心

方法一

因为只有加边,所以可以暴力启发式合并,维护重心
维护子树信息,子树大小不超过一半
复杂度两只 log l o g

方法二

扣出两个重心的链,链上二分找
每次 Splay S p l a y 重心,应该是一只 log l o g 的吧。。。

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e5 + 5);

IL int Input(){
    RG int x = 0, z = 1; RG char c = getchar();
    for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
    for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x * z;
}

int n, m, Xor, fa[_], ch[2][_], rev[_], S[_], sum[_], val[_], rt[_];
char op;

IL int Son(RG int x){
    return ch[1][fa[x]] == x;
}

IL int Isroot(RG int x){
    return ch[0][fa[x]] != x && ch[1][fa[x]] != x;
}

IL void Reverse(RG int x){
    if(!x) return;
    rev[x] ^= 1, swap(ch[0][x], ch[1][x]);
}

IL void Pushdown(RG int x){
    if(!rev[x]) return;
    Reverse(ch[0][x]), Reverse(ch[1][x]), rev[x] ^= 1;
}

IL void Update(RG int x){
    sum[x] = sum[ch[0][x]] + sum[ch[1][x]] + val[x] + 1;
}

IL void Rotate(RG int x){
    RG int y = fa[x], z = fa[y], c = Son(x);
    if(!Isroot(y)) ch[Son(y)][z] = x; fa[x] = z;
    ch[c][y] = ch[!c][x], fa[ch[c][y]] = y;
    ch[!c][x] = y, fa[y] = x, Update(y);
}

IL void Splay(RG int x){
    S[S[0] = 1] = x;
    for(RG int y = x; !Isroot(y); y = fa[y]) S[++S[0]] = fa[y];
    while(S[0]) Pushdown(S[S[0]--]);
    for(RG int y = fa[x]; !Isroot(x); Rotate(x), y = fa[x])
        if(!Isroot(y)) Son(x) ^ Son(y) ? Rotate(x) : Rotate(y);
    Update(x);
}

IL void Access(RG int x){
    for(RG int y = 0; x; y = x, x = fa[x]){
        Splay(x), val[x] += sum[ch[1][x]] - sum[y];
        ch[1][x] = y, Update(x);
    }
}

IL void Makeroot(RG int x){
    Access(x), Splay(x), Reverse(x);
}

IL void Split(RG int x, RG int y){
    Makeroot(x), Access(y), Splay(y);
}

IL void Adjust(RG int x, RG int y){
    Split(x, y);
    RG int flg = 0, t = y, g = 0, size = sum[y] >> 1, sl = 0, sr = 0;
    while(t){
        Pushdown(t);
        RG int ls = ch[0][t], rs = ch[1][t], ssl = sum[ls] + sl, ssr = sum[rs] + sr;
        if(ssl <= size && ssr <= size){
            if(!flg || t < g) g = t, flg = 1; 
            if(ssl == ssr) break;
        }
        if(ssr > ssl) sl += sum[ls] + val[t] + 1, t = rs;
        else sr += sum[rs] + val[t] + 1, t = ls;
    }
    Splay(g);
    Xor ^= x ^ y ^ g, rt[x] = rt[y] = rt[g] = g;
}

IL int Findrt(RG int x){
    return x == rt[x] ? x : rt[x] = Findrt(rt[x]);
}

IL void Link(RG int x, RG int y){
    Makeroot(x), Makeroot(y);
    fa[x] = y, val[y] += sum[x], Update(y);
    Adjust(Findrt(x), Findrt(y));
}

int main(RG int argc, RG char *argv[]){
    n = Input(), m = Input();
    for(RG int i = 1; i <= n; ++i) rt[i] = i, Xor ^= i, sum[i] = 1;
    for(RG int i = 1, x, y; i <= m; ++i){
        scanf(" %c", &op);
        if(op == 'A') x = Input(), y = Input(), Link(x, y);
        else if(op == 'Q') x = Input(), printf("%d\n", Findrt(x));
        else scanf(" %*s"), printf("%d\n", Xor);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值