SDOI2008洞穴勘測--LCT入門

來源:互聯網
上載者:User

LCT真是太抽象了,sak(%%%)給我講了一下,大概懂了但是還有很多存在漏洞的地方。顯然,先發篇部落格再慢慢看吧。。。。。。。。

#include<iostream>#include<cstdio>using namespace std;const int maxn=10005;struct Link_Cut_Tree{bool rev[maxn];int fa[maxn],ch[maxn][2];void push_down(int p){if(rev[p]){rev[ch[p][0]]^=1;rev[ch[p][1]]^=1;swap(ch[p][0],ch[p][1]);rev[p]=0;}}void push(int u){if(!is_root(u))push(fa[u]);push_down(u);}bool is_root(int u){return (u!=ch[fa[u]][0])&&(u!=ch[fa[u]][1]);}void rotate(int u,int p,bool k){if(!is_root(p))ch[fa[p]][p==ch[fa[p]][1]]=u;fa[ch[u][k^1]]=p,fa[u]=fa[p],fa[p]=u;ch[p][k]=ch[u][k^1],ch[u][k^1]=p;}void splay(int u){push(u);while(!is_root(u))rotate(u,fa[u],u==ch[fa[u]][1]);}void access(int u){int v=0;while(u){splay(u);ch[u][1]=v;u=fa[v=u];}}void set_root(int u){access(u),splay(u);rev[u]^=1;}void link(int u,int v){set_root(u);fa[u]=v;}void cut(int u,int v){set_root(u);access(v),splay(v);fa[u]=ch[v][0]=0;}int find(int u){while(fa[u])u=fa[u];return u;}bool query(int u,int v){return find(u)==find(v);}}T;int main(){char ch[10];int n,m,u,v;scanf("%d%d",&n,&m);while(m--){scanf("%s%d%d",ch,&u,&v);if(ch[0]=='C')T.link(u,v);else if(ch[0]=='D')T.cut(u,v);else printf("%s\n",T.query(u,v)?"Yes":"No");}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.