博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
3673: 可持久化并查集 by zky
阅读量:7130 次
发布时间:2019-06-28

本文共 1513 字,大约阅读时间需要 5 分钟。

3673: 可持久化并查集 by zky

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

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
#include
using namespace std;using namespace __gnu_cxx;const int N=2e4+5;rope
*h[N];int n,m;inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){
if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';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+1,1); return fax;}inline void merge(){ fx=find(a);fy=find(b); if(fx!=fy){ h[id]->insert(fx,fy); h[id]->erase(fx+1,1); }}inline bool isone(){ fx=find(a);fy=find(b); return fx==fy;}int main(){ n=10; n=read();m=read(); h[0]=new rope
(); for(int i=0;i<=n;i++) h[0]->push_back(i); for(int i=1,opt;i<=m;i++){ opt=read();a=read(); if(opt&1) b=read(); h[i]=new rope
(*h[opt&1?i-1:a]); id=i; if(opt==1){ merge(); } if(opt==3){ printf("%d\n",isone()); } } return 0;}

 

转载于:https://www.cnblogs.com/shenben/p/6472778.html

你可能感兴趣的文章
人脑功能连接组:计算方法学、重测信度及发展轨线(左西年,2011年)
查看>>
Day1_PHP快速入门
查看>>
oracle学习10
查看>>
vue判断图片为空或者图片加载不成功时显示默认图片
查看>>
Android 解决ViewPager双层嵌套的滑动问题
查看>>
团队加分作业
查看>>
applicationContext.xml 中加载db.properties文件,并配置连接池,以及JDBCTemplate使用
查看>>
关于Android工程中的主要文件夹存放的文件种类
查看>>
Quartz与Spring的整合使用
查看>>
C# 0-1背包问题
查看>>
一维高斯分布与多维高斯分布
查看>>
TNS-00512: Address already in use-TNS-12542: TNS:address already in use
查看>>
法伤符i
查看>>
部署NodeJS上线步骤
查看>>
tp框架表单验证 及ajax
查看>>
[转] ADO.NET调用存储过程带输出参数或返回值
查看>>
js的重载
查看>>
oracle 多表查询
查看>>
数据结构:二级指针与Stack的数组实现
查看>>
JVM内存回收对象及引用分析
查看>>