90pts LCT ,求大佬指点这份LCT 哪里还可以优化~
查看原帖
90pts LCT ,求大佬指点这份LCT 哪里还可以优化~
393674
jixiang楼主2022/3/15 18:06
#include<bits/stdc++.h>
using namespace std;
const int N =  3e5+9;
struct node
{
    int p,s[2];
    int rev;
    int v,sum,sz;
}tr[N];

int fa[N];

struct fight
{
    int kkk,zzz;
}f[N];

int stk[N];

bool isroot(int x)
{
    int fa= tr[x].p;
    return tr[fa].s[1]!=x && tr[fa].s[0]!=x;
}

void push_rev(int x)
{
    swap(tr[x].s[1],tr[x].s[0]);
    tr[x].rev^=1;
}

void up(int x)
{
    tr[x].sum=tr[tr[x].s[1]].sum^tr[x].v^tr[tr[x].s[0]].sum;
}

void down(int x)
{
    if(tr[x].rev)
    {
        push_rev(tr[x].s[1]);
        push_rev(tr[x].s[0]);
        tr[x].rev = 0;
    }
}


void spin(int x)
{
    int y= tr[x].p;
    int z= tr[y].p;
    int k= tr[y].s[1]==x;
    if(!isroot(y))tr[z].s[tr[z].s[1]==y]=x;
    tr[x].p=z;
    tr[y].s[k]=tr[x].s[k^1]; tr[tr[x].s[k^1]].p=y;
    tr[x].s[k^1]=y; tr[y].p=x;
    up(y); up(x);
}

void splay (int x)
{
    int top = 0;
    int r  = x;
    stk[++top]=x;
    while (!isroot(r))stk[++top]=r=tr[r].p;
    while (top) down(stk[top--]);

    while (!isroot(x))
    {
        int y= tr[x].p;
        int z= tr[y].p;

        if(!isroot(y))
        {
            if((tr[y].s[1]==x) ^ (tr[z].s[1]==y))spin(x);
            else spin(y);
        }
        spin(x);
    }
}

void access(int x)
{
    int z =x;
    for (int y= 0; x; y=x,x=tr[x].p)
    {
        splay(x);
        tr[x].s[1]=y;
        up(x);  ///改接树上结构需要up
    }
    splay(z); ///access自带把x转上辅助树的树根
}


int find_root(int x)/// 辅助树的root
{
    access(x);
    while (tr[x].s[0])
    {
        down(x); ///忘记2
        x=tr[x].s[0];
    }
    splay(x);
    return x;
}

void make_root(int x)/// 把x做成原树的根节点,需要反转整个中序遍历
{
    access(x);
    push_rev(x);///q1
}

void sign(int x,int y) ///把x到y的路径标记为实边
{
    make_root(x);
    access(y);
}

void link(int x,int y)
{
    make_root(x);
    if(find_root(y)!=x )tr[x].p=y;
}

void cut(int x,int y)
{
    make_root(x);
    if(find_root(y)==x && tr[y].p==x && !tr[y].s[0])
    {
        tr[x].s[1]=tr[y].p=0;
        up(x);
    }
}

template <typename T> void inline read(T &x)
{
    x= 0 ; bool f= 0; char ch  = getchar();
    while (ch < '0' || ch > '9') {if(ch == '-')f= 1; ch = getchar();}
    while (ch <='9' && ch >= '0') {x= (x<<1)+ (x<<3) + ch - '0'; ch = getchar();}
    if(f)x*=-1;
}

int main()
{
    int x,y;
    int n,m;
    read(n);
    read(m);
    int idx = 0;
    for (int i= 1;i<= n;i++)fa[i]=i;
    for (int i= 1;i<n;i++)
    {
        read(x); read(y);
        link (x,y);
    }

    while (m--)
    {
        char op;
        cin>>op;
        if(op=='Q')
        {
            read(x); read(y);
            if(find_root (x)!=find_root(y)) puts("No");
            else puts("Yes");
        }
        else if(op=='C')
        {
            read(x); read(y);
            cut (x,y);
            f[++idx]={x,y};
        }
        else
        {
            read(x);
            link(f[x].kkk,f[x].zzz);
        }
    }

    return 0;
}

2022/3/15 18:06
加载中...