3673: 可持久化并查集 by zky

Time Limit: 5 Sec  Memory Limit: 128 MB
Submit: 2170  Solved: 978
[Submit][Status][Discuss]

Description

n个集合 m个操作
操作:
1 a b 合并a,b所在集合
2 k 回到第k次操作之后的状态(查询算作操作)
3 a b 询问a,b是否属于同一集合,是则输出1否则输出0

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

Input

 

Output

 

Sample Input

5 6
1 1 2
3 1 2
2 0
3 1 2
2 1
3 1 2

Sample Output

1
0
1

HINT

 

Source

//(暴力)用rope实现可持久化数组
#include<cstdio>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
const int N=2e4+;
rope<int> *h[N];
int n,m;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int id,a,b,fx,fy;
int find(int x){
int fax=h[id]->at(x);
if(fax==x) return x;
fax=find(fax);
h[id]->insert(x,fax);
h[id]->erase(x+,);
return fax;
}
inline void merge(){
fx=find(a);fy=find(b);
if(fx!=fy){
h[id]->insert(fx,fy);
h[id]->erase(fx+,);
}
}
inline bool isone(){
fx=find(a);fy=find(b);
return fx==fy;
}
int main(){
n=;
n=read();m=read();
h[]=new rope<int> ();
for(int i=;i<=n;i++) h[]->push_back(i);
for(int i=,opt;i<=m;i++){
opt=read();a=read();
if(opt&) b=read();
h[i]=new rope<int> (*h[opt&?i-:a]);
id=i;
if(opt==){
merge();
}
if(opt==){
printf("%d\n",isone());
}
}
return ;
}

最新文章

  1. AndroidStudio非法字符: &#39;\ufeff&#39;解决
  2. ahjesus HttpQuery
  3. 【高级功能】使用canvas元素(第一部分)
  4. 用栈解决Largest Rectangle问题
  5. Python 中文Key 报错问题
  6. ss 公共代理的必要设置(转)
  7. Gmail新版截图曝光 你还能认得出来吗?
  8. iOS9的适配
  9. nyoj 10 skiing(记忆化搜索)
  10. Codeforces Beta Round #51 A. Flea travel 水题
  11. linux 下启动程序的时候会显示坏的解释器,或者没有那个文件
  12. python+selenium遇到鼠标悬停不成功可以使用js进行操作
  13. tomcat服务器一闪而过解决方法
  14. EasyUI datagrid 使用小结
  15. JS中[object object]怎么取值
  16. LeetCode 101. Symmetric Tree 判断对称树 C++
  17. oracle(环境搭建二)
  18. 006 numpy常用函数
  19. linux中chkconfig 启动程序顺序介绍
  20. eclipse导入svn中的web工程,部署到tomcat时候,只有WEB-INF目录问题

热门文章

  1. 一起talk C栗子吧(第二十二回:C语言实例--队列一)
  2. 怎样用Google APIs和Google的应用系统进行集成(4)----获得Access Token以通过一些Google APIs的OAuth2认证
  3. vue sync用法
  4. centos下hadoop的安装
  5. 【Python3 爬虫】09_正则表达式(re.math()、re.search()、re.sub()、全局匹配函数)
  6. MapReduce源代码分析之JobSubmitter(一)
  7. python机器学习-逻辑回归
  8. centos mysql iptables配置
  9. asp.net+mvc+easyui+sqlite 简单用户系统学习之旅(一)—— 手把手教你创建第一个三层架构+mvc的asp.net项目
  10. 【Scala】使用Option、Some、None,避免使用null