LCT——洛谷P2147 [SDOI2008]Cave 洞穴勘测

来源:互联网 发布:杭州软件开发招聘 编辑:程序博客网 时间:2024/05/20 22:38

https://www.luogu.org/problem/show?pid=2147
lct
我想说一下splay的部分;
首先;
我们一开始读入n
表示有n棵树的森林;
我们每次合并x,y
是吧x,y两颗树合并起来;
但是每棵树有很多splay;
所以这些splay必须要互补影响;
splay部分

int get(int x){    if(ch[fa[x]][1]==x)return 1;    if(ch[fa[x]][0]==x)return 0;    return -1;}void rotate(int x){    int f=fa[x],ff=fa[f],l=get(x),r=l^1;    if(get(f)!=-1)ch[ff][ch[ff][1]==f]=x;    fa[ch[x][r]]=f; fa[f]=x; fa[x]=ff;     ch[f][l]=ch[x][r]; ch[x][r]=f; }void push(int x){    if(!rev[x])return;    rev[x]=0;    rev[ch[x][0]]^=1;    rev[ch[x][1]]^=1;    swap(ch[x][0],ch[x][1]);}void cle(int x){if(get(x)!=-1)cle(fa[x]);push(x);}void splay(int x){    cle(x);    for(int f=fa[x];get(x)!=-1;rotate(x),f=fa[x])        if(get(f)!=-1)rotate(get(x)==get(f)?f:x);}

首先判断标准从f是不是0变到了get(f)是不是-1;
因为实边的存在,一个splay的根节点可能fa不是0;
所以我们要判断他父亲节点有木有他这个儿子;
然后是旋转;
切记旋转一定要按顺序;
比如if(get(f)!=-1)ch[ff][ch[ff][1]==f]=x一 定要放最前
因为get(f)里面调用了fa[f],这个在后面是更新了的;
然后splay就是没了;
代码自己看吧

#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int N=1e4+5;int ch[N][2],fa[N],rev[N];int n,m,x,y;char c[20];int get(int x){    if(ch[fa[x]][1]==x)return 1;    if(ch[fa[x]][0]==x)return 0;    return -1;}void rotate(int x){    int f=fa[x],ff=fa[f],l=get(x),r=l^1;    if(get(f)!=-1)ch[ff][ch[ff][1]==f]=x;    fa[ch[x][r]]=f; fa[f]=x; fa[x]=ff;     ch[f][l]=ch[x][r]; ch[x][r]=f; }void push(int x){    if(!rev[x])return;    rev[x]=0;    rev[ch[x][0]]^=1;    rev[ch[x][1]]^=1;    swap(ch[x][0],ch[x][1]);}void cle(int x){if(get(x)!=-1)cle(fa[x]);push(x);}void splay(int x){    cle(x);    for(int f=fa[x];get(x)!=-1;rotate(x),f=fa[x])        if(get(f)!=-1)rotate(get(x)==get(f)?f:x);}void access(int x){for(int t=0;x;x=fa[t=x])splay(x),ch[x][1]=t;}void makeroot(int x){access(x);splay(x);rev[x]=1;}void link(int x,int y){makeroot(x);fa[x]=y;}void cut(int x,int y){makeroot(x);access(y);splay(y);fa[x]=ch[y][0]=0;}int find(int x){for(access(x),splay(x);ch[x][0];x=ch[x][0]);return x;}int main(){    scanf("%d%d",&n,&m);    while(m--){        scanf("%s%d%d",c,&x,&y);        if(c[0]=='C')link(x,y);        if(c[0]=='D')cut(x,y);        if(c[0]=='Q')            if(find(x)==find(y))printf("Yes\n");else printf("No\n");        }}
原创粉丝点击