前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >3673: 可持久化并查集 by zky

3673: 可持久化并查集 by zky

作者头像
attack
发布2018-04-12 11:24:47
4850
发布2018-04-12 11:24:47
举报

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

出题人大SB

没错我就是来骗访问量的,

这道题和上一道题的唯一区别就是

不用xor!

代码语言:javascript
复制
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<ext/rope>
using namespace std;
using namespace __gnu_cxx;
const int MAXN=2000050;
const int maxn=0x7fffffff;
void read(int &n)
{
    char c='+';int x=0;bool flag=0;
    while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}
    while(c>='0'&&c<='9'){x=x*10+(c-48);c=getchar();}
    flag==1?n=-x:n=x;
}

rope<int> *rp[MAXN];
int a[MAXN];
int n,m,how,x,y,lastans;
int find(int i,int x)
{
	if(rp[i]->at(x)==x) 	return x;
	int f=find(i,rp[i]->at(x));
	if(f==rp[i]->at(x))		return f;
	rp[i]->replace(x,f);
	return f;
}
void merge(int i,int x,int y)
{
	x=find(i,x),y=find(i,y);
	if(x!=y)	rp[i]->replace(y,x);
}
int main()
{
    int n,m;
    read(n);read(m);
    for(int i=1;i<=n;i++)	a[i]=i;
	rp[0]=new rope<int> (a,a+n+1);
	for(int i=1;i<=m;i++)
	{
		rp[i]=new rope<int> (*rp[i-1]);
		int how;read(how);
		if(how==1)
		{
			read(x);read(y);
			merge(i,x^lastans,y^lastans);
		}
		else if(how==2)
		{
			read(x);
			rp[i]=rp[x^lastans];
		}
		else if(how==3)
		{
			read(x);read(y);
			printf("%d\n",(find(i,x^lastans)==find(i,y^lastans)));
		}
	}
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-08-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Description
  • Input
  • Output
  • Sample Input
  • Sample Output
  • HINT
  • Source
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档