BZOJ 2049 洞穴勘測

來源:互聯網
上載者:User

標籤:

又一道LCT模板題。

如何找是不是在同一棵樹上?只要找深度最小的點是不是相同的點即可。

也就是splay最左邊的那個點。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define maxv 10050#define maxe 20050using namespace std;int n,m,fath[maxv],size[maxv],tree[maxv][3],x,y,rev[maxv];int stack[maxv],top=0;char s[20];bool isroot(int x){    return tree[fath[x]][1]!=x&&tree[fath[x]][2]!=x;}void pushdown(int x){    if (rev[x])    {        int ls=tree[x][1],rs=tree[x][2];        rev[ls]^=1;rev[rs]^=1;rev[x]=0;        swap(tree[x][1],tree[x][2]);    }}void pushup(int x){    int ls=tree[x][1],rs=tree[x][2];    size[x]=size[ls]+size[rs]+1;}void rotate(int x){    int y=fath[x],z=fath[y],l,r;    if (tree[y][1]==x) l=1;else l=2;    r=3-l;    if (!isroot(y))    {        if (tree[z][1]==y) tree[z][1]=x;        else tree[z][2]=x;    }    fath[x]=z;fath[y]=x;fath[tree[x][r]]=y;    tree[y][l]=tree[x][r];tree[x][r]=y;    pushup(y);pushup(x);}void splay(int x){    top=0;    stack[++top]=x;    for (int i=x;!isroot(i);i=fath[i])        stack[++top]=fath[i];    for (int i=top;i>=1;i--)        pushdown(stack[i]);    while (!isroot(x))    {        int y=fath[x],z=fath[y];        if (!isroot(y))        {            if ((tree[y][1]==x)^(tree[z][1]==y)) rotate(x);            else rotate(y);        }        rotate(x);    }}void access(int x){    int regis=0;    while (x)    {        splay(x);        tree[x][2]=regis;        regis=x;x=fath[x];    }}void makeroot(int x){    access(x);    splay(x);    rev[x]^=1;}void link(){    scanf("%d%d",&x,&y);    makeroot(x);    fath[x]=y;    splay(x);}void cut(){    scanf("%d%d",&x,&y);    makeroot(x);access(y);    splay(y);tree[y][1]=0;fath[x]=0;}int find(int x){    access(x);splay(x);    int y=x;    while (tree[y][1]) y=tree[y][1];    return y;}void query(){    scanf("%d%d",&x,&y);    int u=find(x),v=find(y);    if (u==v)        printf("Yes\n");    else printf("No\n");}int main(){    scanf("%d%d",&n,&m);    for (int i=1;i<=n;i++) size[i]=1;    for (int i=1;i<=m;i++)    {        scanf("%s",s);        if (s[0]==‘C‘) link();        else if (s[0]==‘D‘) cut();        else if (s[0]==‘Q‘) query();    }    return 0;}

 

BZOJ 2049 洞穴勘測

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.