日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

P4219-[BJOI2014]大融合【LCT】

發布時間:2023/12/3 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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】的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。