码迷,mamicode.com
首页 > 其他好文 > 详细

UOJ207:共价大爷游长沙

时间:2018-02-07 19:37:31      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:access   代码   space   mat   register   date   eve   post   gis   

题面

UOJ

Sol

神题
给每个点对随机一个权值,把这两个点的权值异或上这个随机的值
\(LCT\)维护子树信息,若子树异或和为所有点对的异或和那么就是答案

大常数代码

# 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, ch[_][2], fa[_], size[_], sum[_], val[_], rev[_], S[_], top, Sum;
struct Edge{
    int u, v, w;
} P[_ << 2];

# define ls ch[x][0]
# define rs ch[x][1]

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

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

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

IL void Pushdown(RG int x){
    if(!rev[x]) return;
    Reverse(ls); Reverse(rs); rev[x] = 0;   
}

IL void Update(RG int x){
    sum[x] = val[x] ^ size[x] ^ sum[ls] ^ sum[rs];
}

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

IL void Splay(RG int x){
    S[top = 1] = x;
    for(RG int y = x; !Isroot(y); y = fa[y]) S[++top] = fa[y];
    while(top) Pushdown(S[top--]);
    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);
        size[x] ^= sum[rs] ^ sum[y];
        rs = y; Update(x);
    }
}

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

IL void Link(RG int x, RG int y){
    Makeroot(x); Makeroot(y); fa[x] = y;
    size[y] ^= sum[x]; Update(y);
}

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

IL void Cut(RG int x, RG int y){
    Split(x, y); fa[x] = ch[y][0] = 0;
}

# define yyb 141905
# define lrh 141936
# define ni_dong_de +

int main(RG int argc, RG char* argv[]){
    srand(yyb ni_dong_de lrh);
    Input(); n = Input(); m = Input();
    for(RG int i = 1, x, y; i < n; ++i)
        x = Input(), y = Input(), Link(x, y);
    for(RG int i = 1, tot = 0; i <= m; ++i){
        RG int opt = Input(), x = Input(), y, u, v;
        if(opt == 1){
            y = Input(), u = Input(), v = Input();
            Cut(x, y), Link(u, v);
        }
        else if(opt == 2){
            P[++tot].u = x, P[tot].v = y = Input();
            P[tot].w = rand() % 19260817;
            Makeroot(x); val[x] ^= P[tot].w; Update(x);
            Makeroot(y); val[y] ^= P[tot].w; Update(y);
            Sum ^= P[tot].w;
        }
        else if(opt == 3){
            Makeroot(P[x].u); val[P[x].u] ^= P[x].w; Update(P[x].u);
            Makeroot(P[x].v); val[P[x].v] ^= P[x].w; Update(P[x].v);
            Sum ^= P[x].w;
        }
        else{
            y = Input(); Split(x, y);
            if(sum[x] ^ Sum) puts("NO");
            else puts("YES");
        }
    }
    return 0;
}

UOJ207:共价大爷游长沙

标签:access   代码   space   mat   register   date   eve   post   gis   

原文地址:https://www.cnblogs.com/cjoieryl/p/8427481.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!