P4219-[BJOI2014]大融合【LCT】
生活随笔
收集整理的這篇文章主要介紹了
P4219-[BJOI2014]大融合【LCT】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
正題
題目鏈接:https://www.luogu.com.cn/problem/P4219
題目大意
nnn個點,每次有操作
解題思路
考慮如何用LCTLCTLCT維護虛子樹信息,只要在斷邊的時候把虛子樹的信息維護進該節點即可。
codecodecode
#include<cstdio> #include<cstring> #include<algorithm> #include<stack> using namespace std; const int N=1e5+10; int n,q,siz[N],size[N]; struct Link_Cut_Tree{int fa[N],t[N][2];bool r[N];stack<int> s;bool Nroot(int x){return fa[x]&&(t[fa[x]][1]==x||t[fa[x]][0]==x);}bool Direct(int x){return t[fa[x]][1]==x;}void PushUp(int x){size[x]=size[t[x][0]]+size[t[x][1]]+siz[x]+1;return;}void Rev(int x){r[x]^=1;swap(t[x][0],t[x][1]);return;}void PushDown(int x){if(!r[x])return;Rev(t[x][0]);Rev(t[x][1]);r[x]=0;return;}void Rotate(int x){int y=fa[x],z=fa[y];int xs=Direct(x),ys=Direct(y);int w=t[x][xs^1];t[x][xs^1]=y;t[y][xs]=w;if(Nroot(y))t[z][ys]=x;if(w)fa[w]=y;fa[x]=z;fa[y]=x;PushUp(y);PushUp(x);return;}void Splay(int x){s.push(x);while(Nroot(s.top()))s.push(fa[s.top()]);while(!s.empty())PushDown(s.top()),s.pop();while(Nroot(x)){int y=fa[x];if(!Nroot(y))Rotate(x);else if(Direct(x)==Direct(y))Rotate(y),Rotate(x);else Rotate(x),Rotate(x);}return;}void Access(int x){for(int y=0;x;x=fa[y=x]){Splay(x);siz[x]-=size[y]-size[t[x][1]];t[x][1]=y;PushUp(x);}return;}void MakeRoot(int x){Access(x);Splay(x);Rev(x);return;}void Link(int x,int y){MakeRoot(x);MakeRoot(y);fa[x]=y;siz[y]+=size[x];PushUp(y);return;}void Split(int x,int y){MakeRoot(x);Access(y);Splay(y);return;} }LCT; int main() {scanf("%d%d",&n,&q);for(int i=1;i<=n;i++)size[i]=1;while(q--){char op[5];int x,y;scanf("%s %d%d",op,&x,&y);if(op[0]=='Q'){LCT.Split(x,y);printf("%lld\n",1ll*(siz[y]+1)*(siz[x]+1));}else LCT.Link(x,y);}return 0; }總結
以上是生活随笔為你收集整理的P4219-[BJOI2014]大融合【LCT】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows 2008 iis怎么搭建
- 下一篇: dede备份的数据库txt怎么用sql打