bzoj2049: [Sdoi2008]Cave 洞穴勘测 lct裸题

题意:三种操作一种摧毁一条边,一种链接一条边,一种查询两个点是否联通
题解:lct的link和cut即可

/**************************************************************
    Problem: 2049
    User: walfy
    Language: C++
    Result: Accepted
    Time:1896 ms
    Memory:1508 kb
****************************************************************/
 
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0)
 
using namespace std;
 
const double eps=1e-6;
const int N=10000+1,maxn=5000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f;
 
struct LCT{
    int fa[N],ch[N][2],rev[N],q[N];
    bool isroot(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}
    void pushup(int x){}
    void pushdown(int x)
    {
        if(rev[x])
        {
            rev[x]=0;swap(ch[x][0],ch[x][1]);
            rev[ch[x][0]]^=1,rev[ch[x][1]]^=1;
        }
    }
    void Rotate(int x)
    {
        int y=fa[x],z=fa[y],l,r;
        if(ch[y][0]==x)l=0,r=l^1;
        else l=1,r=l^1;
        if(!isroot(y))
        {
            if(ch[z][0]==y)ch[z][0]=x;
            else ch[z][1]=x;
        }
        fa[x]=z;fa[y]=x;fa[ch[x][r]]=y;
        ch[y][l]=ch[x][r];ch[x][r]=y;
        pushup(y);pushup(x);
    }
    void splay(int x)
    {
        int top=1;q[top]=x;
        for(int i=x;!isroot(i);i=fa[i])q[++top]=fa[i];
        for(int i=top;i;i--)pushdown(q[i]);
        while(!isroot(x))
        {
            int y=fa[x],z=fa[y];
            if(!isroot(y))
            {
                if((ch[y][0]==x)^(ch[z][0]==y))Rotate(x);
                else Rotate(y);
            }
            Rotate(x);
        }
    }
    void access(int x){for(int y=0;x;y=x,x=fa[x])splay(x),ch[x][1]=y,pushup(x);}
    void makeroot(int x){access(x),splay(x),rev[x]^=1;}
    int findroot(int x){access(x),splay(x);while(ch[x][0])x=ch[x][0];return x;}
    void split(int x,int y){makeroot(x),access(y),splay(y);}
    void cut(int x,int y){split(x,y);if(ch[y][0]==x)ch[y][0]=0,fa[x]=0;}
    void link(int x,int y){makeroot(x),fa[x]=y;}
}lct;
char s[N];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    while(m--)
    {
        char op[10];
        int x,y;
        scanf("%s%d%d",op,&x,&y);
        if(op[0]=='Q')
        {
            int fx=lct.findroot(x),fy=lct.findroot(y);
            if(fx!=fy)puts("No");
            else puts("Yes");
        }
        else if(op[0]=='C')
        {
            int fx=lct.findroot(x),fy=lct.findroot(y);
            if(fx!=fy)lct.link(x,y);
        }
        else if(op[0]=='D')
        {
            int fx=lct.findroot(x),fy=lct.findroot(y);
            if(fx==fy)lct.cut(x,y);
        }
    }
    return 0;
}
/********************
200 5
Query   123 127
Connect 123 127
Query   123 127
Destroy 127 123
Query   123 127
********************/
posted @ 2018-05-29 23:16  walfy  阅读(154)  评论(0编辑  收藏  举报