bzoj2049 [sdoi2008]cave Cave Survey Link Cut tree Introduction

Source: Internet
Author: User

Link Cut Tree Introductory question

First of all, I will only write the bottom-up of the array version (all said not to write pointers, do not write from the top down qaq ...)

Suddenly found link cut tree not difficult to write ...

Say the functions of each function:

BOOL IsRoot (int x): Determine if x is the root of the heavy chain (splay) being located

void down (int x): Delegate various markings

void rotate (int x): position x in the heavy chain (splay) where x is rotated to fa[x]

void splay (int x): Rotates x to the root in x sitting in heavy chain (splay)

void access (int x): Turns the root of the tree from X to x into a heavy chain, the root is the chain top, and X is the chain bottom

void reverse (int x): turns x into the root of the tree (access then hits the reverse tag)

void link (int x,int y) connects X, y two points

void cut (int x,int y) disconnects X, y two points

int find (int x) finds the root of the tree where x is located

1#include <algorithm>2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <string>7#include <cmath>8#include <ctime>9#include <queue>Ten#include <stack> One#include <map> A#include <Set> - #defineRre (i,r,l) for (int i= (r); i>= (l); i--) - #defineRe (I,L,R) for (int i= (l); i<= (R); i++) the #defineClear (A, B) memset (A,b,sizeof (a)) - #defineInOut (x) printf ("%d", (x)) - #defineDouin (x) scanf ("%lf", &x) - #defineStrin (x) scanf ("%s", (x)) + #defineLLin (x) scanf ("%lld", &x) - #defineOP operator + #defineCSC Main Atypedef unsignedLong LongULL; attypedefConst intCInt; -typedefLong LongLL; - using namespacestd; - voidInin (int&ret) - { -ret=0;intf=0;CharCh=GetChar (); in      while(ch<'0'|| Ch>'9'){if(ch=='-') f=1; ch=GetChar ();} -      while(ch>='0'&&ch<='9') ret*=Ten, ret+=ch-'0', ch=GetChar (); toret=f?-Ret:ret; + } - intfa[10010],ch[10010][2],rev[10010]; the BOOLIsRootintx) {returnch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;} * voidDownintx) $ {Panax Notoginseng     if(!rev[x])return ; -Swap (ch[x][0],ch[x][1]); therev[ch[x][0]]^=1; +rev[ch[x][1]]^=1; Arev[x]=0; the } + voidRotateintx) - { $     inty=fa[x],z=Fa[y]; $     if(!isroot (y)) -         if(ch[z][0]==y) ch[z][0]=x; -         Elsech[z][1]=x;Else ; thefa[x]=z,fa[y]=x; -     intd=ch[y][1]==x;Wuyifa[ch[x][d^1]]=y; thech[y][d]=ch[x][d^1]; -ch[x][d^1]=y; Wu } - intsta[10010],top; About voidSplay (intx) $ { -top=0; sta[++top]=x; -      for(inti=x;! IsRoot (i); I=fa[i]) sta[++top]=Fa[i]; -      while(top) Down (sta[top--]); A      while(!isroot (x)) +     { the         inty=fa[x],z=Fa[y]; -         if(!isroot (y)) $             if((ch[y][1]==x) ^ (ch[z][1]==y)) Rotate (x); the             ElseRotate (y);Else  the rotate (x); the     } the } - voidAccessintx) in { the     inttemp=0; the      while(x) About     { the splay (x); thech[x][1]=temp; thetemp=x,x=Fa[x]; +     } - } the voidReverseintx)Bayi { theAccess (x); splay (x), rev[x]^=1; the } - voidLinkintXinty) - { thereverse (x); fa[x]=y,splay (x); the } the voidCutintXinty) the { -reverse (x); Access (y); splay (y); ch[y][0]=fa[x]=0; the } the intFindintx) the {94 access (x), splay (x); the     inty=x; the      while(ch[y][0]) y=ch[y][0]; the     returny;98 } About intn,m; - intMain ()101 {102     Chars[ One];103 inin (n), Inin (m);104Re (I,1, M) the     {106 Strin (s);107         intX,y;inin (x), Inin (y);108         if(s[0]=='C') Link (x, y);109         Else if(s[0]=='D') Cut (x, y); the         Else 111         { the             if(Find (x) ==find (y)) printf ("yes\n");113             Elseprintf"no\n"); the         } the     } the      return 0;117}

bzoj2049 [sdoi2008]cave Cave Survey Link Cut tree Introduction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.