BZOJ2049 LCT

换了一种新写法,感觉比之前好
主要注意define Rotate()别写错

#include
#include
#include
#include
#include
#include
#define For(i,j,k) for(register int i=(j);i<=(int)k;i++)
#define Forr(i,j,k) for(register int i=(j);i>=(int)k;i++)
#define F(i) (T[i].fa)
#define L(i) (T[i].s[0])
#define R(i) (T[i].s[1])
#define Loc(i) (i==R(F(i)))
#define Set(a,b) memset((a),(b),sizeof(a))
using namespace std;
const int N=10010;
inline void read(int &x){
    x=0;char c=getchar();int f=(c=='-');
    while(c<'0'||c>'9')c=getchar(),f|=(c=='-');
    while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();if(f)x=-x;
}
struct node{
    int s[2],rev,fa;
};
struct LCT{
    node T[N];
    inline void clear(){Set(T,0);}
    inline void pushdown(int x){
        if(T[x].rev){
            T[x].rev^=1,T[L(x)].rev^=1,T[R(x)].rev^=1;
            swap(L(x),R(x));
        }
    }
    inline bool isrt(int x){
        return L(F(x))!=x&&R(F(x))!=x;
    }
    void Pushdown(int x){
        if(!isrt(x))Pushdown(F(x));
        pushdown(x);
    }
    inline void Sets(int a,int b,int c){
        T[a].s[c]=b,F(b)=a;
    }
    void Rotate(int x){
        int y=F(x),z=F(y),l=Loc(x),r=l^1;
        if(!isrt(y))T[z].s[Loc(y)]=x;F(x)=z;
        Sets(y,T[x].s[r],l);
        Sets(x,y,r);
    }
    inline void splay(int x){
        Pushdown(x);
        while(!isrt(x)){
            int y=F(x),z=F(y);
            if(!isrt(y))Rotate(x);
            Rotate(x);
        }
    }
    inline void access(int x){
        int y=0;
        while(x){
            splay(x);
            R(x)=y;
            y=x,x=F(x);
        }
    }
    inline int findrt(int x){
        access(x);splay(x);
        while(L(x))x=L(x);
        return x;
    }
    inline void reverse(int x){
        access(x);
        splay(x);
        T[x].rev^=1;
    }
    inline void link(int x,int y){
        reverse(x),F(x)=y,splay(x);
    }
    inline void cut(int x,int y){
        reverse(x),access(y),splay(y),F(x)=L(y)=0;
    }
}t;
char s[10];
int main(){
    int n,m,x,y;
    t.clear();
    read(n),read(m);
    For(i,1,m){
        scanf("%s",s);read(x),read(y);
        if(s[0]=='C')t.link(x,y);
        else if (s[0]=='D')t.cut(x,y);
        else {
            if(t.findrt(x)==t.findrt(y))puts("Yes");
            else puts("No");
        }
    }
    return 0;
}

你可能感兴趣的:(bzoj,动态树)