[bzoj3673][可持久化並查集 by zky] (rope(可持久化數組)+並查集=可持久化並查集)

來源:互聯網
上載者:User

標籤:span   狀態   實現   pre   out   int   div   曆史   cas   

Description

n個集合 m個操作
操作:
1 a b 合并a,b所在集合
2 k 回到第k次操作之後的狀態(查詢算作操作)
3 a b 詢問a,b是否屬於同一集合,是則輸出1否則輸出0

0<n,m<=2*10^4

InputOutputSample Input
5 61 1 23 1 22 03 1 22 13 1 2
Sample Output
101

Solution

用rope實現可持久化數組,用rope的記錄功能實現可持久化並查集,通過時間168ms

#include<cstdio>#include<ext/rope>#include<iostream>using namespace std;using namespace __gnu_cxx;inline int Rin(){  int x=0,c=getchar(),f=1;  for(;c<48||c>57;c=getchar())    if(!(c^45))f=-1;  for(;c>47&&c<58;c=getchar())    x=(x<<1)+(x<<3)+c-48;  return x*f;}const int M=20010;rope<int>*his[M];int n,m,a[M];inline int find(int i,int x){  int d=x,p;  while(his[i]->at(x)^x)x=his[i]->at(x);  while(d^x)p=his[i]->at(d),his[i]->replace(d,x),d=p;  return x;}inline void merge(int i,int x,int y){  x=find(i,x),y=find(i,y);  if(x^y)    his[i]->replace(y,x);}int main(){  n=Rin(),m=Rin();  for(int i=1;i<=n;i++)a[i]=i;  his[0]=new rope<int>(a,a+1+n);  for(int i=1,x,y,sign;i<=m;i++){    his[i]=new rope<int>(*his[i-1]);    scanf("%d",&sign);    switch(sign){    case 1:      x=Rin(),y=Rin();      merge(i,x,y);      break;    case 2:      x=Rin();      his[i]=his[x];      break;    case 3:      x=Rin(),y=Rin();      printf("%d\n",(!(find(i,x)^find(i,y))));      break;    }  }  return 0;}

 

[bzoj3673][可持久化並查集 by zky] (rope(可持久化數組)+並查集=可持久化並查集)

聯繫我們

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