【模板】LCT

数组版

struct node {
    int son[2], fa;
    bool tag;
    node() {
        son[0] = son[1] = fa = 0;
        tag = 0;
    }
}t[100001];
bool isroot(int p) {
    return !(t[t[p].fa].son[0] == p || t[t[p].fa].son[1] == p);
}
void push_up(int p) {
}
void push_down(int p) {
    if(t[p].tag) {
        t[p].tag = 0;
        t[t[p].son[0]].tag ^= 1;
        t[t[p].son[1]].tag ^= 1;
        swap(t[p].son[0], t[p].son[1]);
    }
}
void down(int p) {
    if(! isroot(p))down(t[p].fa);
    push_down(p);
}
void rotate(int p) {
    int f = t[p].fa;
    if(! isroot(f))t[t[f].fa].son[t[t[f].fa].son[1] == f] = p;
    t[p].fa = t[f].fa;
    bool c = t[f].son[1] == p;
    t[f].son[c] = t[p].son[! c];
    if(t[f].son[c])t[t[f].son[c]].fa = f;
    t[f].fa = p;
    t[p].son[! c] = f;
    push_up(f), push_up(p);
}
void splay(int p) {
    down(p);
    int f;
    while(! isroot(p)) {
        f = t[p].fa;
        if(! isroot(f)) {
            if((p == t[f].son[1]) ^ (f == t[t[f].fa].son[1]))rotate(p);
            else rotate(f);
        }
        rotate(p);
    }
}
int find_root(int p) {
    while(t[p].fa)p = t[p].fa;
    return p;
}
void access(int p) {
    for(int x = 0; p; x = p, p = t[p].fa)splay(p), t[p].son[1] = x, push_up(p);
}
void make_root(int p) {
    access(p), splay(p), t[p].tag ^= 1;
}
void split(int x, int y) {
    make_root(x), access(y), splay(y);
}
void link(int x, int y) {
    make_root(x), t[x].fa = y;
}
void cut(int x, int y) {
    split(x, y), t[y].son[x == t[y].son[1]] = t[x].fa = 0;
}

转载于:https://www.cnblogs.com/akakw1/p/10038837.html

你可能感兴趣的:(【模板】LCT)