模板LCT+理解

來源:互聯網
上載者:User

原來一直不知道LCT是幹什麼的。。。只知道是動態樹剖。。。(現在看好像也不是那麼像了。。。)

核心:用一叢平衡樹維護一棵無根樹。。。

因為SPLAY與LCT相性最好。。。(個人感覺。。。畢竟還沒看到拿TREAP寫的。。。(PS:我孤陋寡聞。。。));

所以用SPLAY維護。。。

核心++:把一棵樹剖成許多鏈。。。一條長度為log(n)的鏈是一棵SPLAY(關鍵字:dep)

所以SPLAY最左端就是root。。。


#include<cstdio>#include<iostream>#define MAXN 10005 using namespace std;int ch[MAXN][2],fa[MAXN];int lazy[MAXN],num[MAXN];inline bool root(int x){return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;}void rotate(int x){  int y=fa[x],z=fa[y];  int d=ch[y][0]==x ? 0:1;  if(!root(y))  {    if(ch[z][0]==y)ch[z][0]=x;    else ch[z][1]=x;          }  fa[y]=x,fa[x]=z,fa[ch[x][d^1]]=y;  ch[y][d]=ch[x][d^1],ch[x][d^1]=y;}inline void down(int x){  if(!lazy[x])return ;  lazy[x]^=1,lazy[ch[x][0]]^=1,lazy[ch[x][1]]^=1;  swap(ch[x][0],ch[x][1]);       }void up(int x)                                         ///注意先把標記傳完。。。否則會T。。。不知原因。。。{  if(!root(x))up(fa[x]);  down(x);     }void splay(int x){  up(x);  int y,z;  while(!root(x))  {    y=fa[x],z=fa[y];    down(y);down(x);    if(!root(y))    {      if((ch[z][0]==y)^(ch[y][0]==x))rotate(x);      else rotate(y);                }                   rotate(x);  }  down(x);}int pos[MAXN],poi=0;void access(int x){  int t=0;  while(x)  {    splay(x);    ch[x][1]=t;    t=x,x=fa[x];          }}void rev(int x){  access(x),splay(x);  lazy[x]^=1;  }void link(int x,int y){rev(x);fa[x]=y;}void cut(int x,int y){rev(x),access(y),splay(y),ch[y][0]=fa[x]=0;}int find(int x){  while(fa[x])x=fa[x];  return x;}int main(){  int n,m;    scanf("%d%d",&n,&m);  char opr[10];int t1,t2;  for(int i=1;i<=m;i++)  {    scanf("%s",opr);    scanf("%d%d",&t1,&t2);    if(opr[0]=='C')link(t1,t2);    else if(opr[0]=='D')cut(t1,t2);    else     {      t1=find(t1),t2=find(t2);      if(t1==t2)printf("Yes\n");      else printf("No\n");         }  }  return 0;    }


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.