BZOJ 3673/3674 可持久化并查集 by zky [rope可持久化平衡树]

题意:

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

rope按秩合并:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<ext/rope>
#define pii pair<int,int>
#define N 200005
using namespace std;
using namespace __gnu_cxx;
rope<pii> *his[N];
pii a[N];
pii find(int x,int root)
{
	pii fa=his[root]->at(x);
	if(fa.first!=x)return find(fa.first,root);
	return fa;
}
int main()
{
	int n,m,last=0;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		a[i].first=i,a[i].second=0;
	his[0]=new rope<pii>(a,a+n+1);
	for(int i=1;i<=m;i++)
	{
		int op;
		scanf("%d",&op);
		if(op==1)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			a^=last,b^=last;
			pii u=find(a,i-1);
			pii v=find(b,i-1);
			his[i]=new rope<pii>(*his[i-1]);
			if(u.first==v.first)continue;
			if(u.second<v.second)swap(u,v);
			if(u.second==v.second)his[i]->replace(u.first,make_pair(u.first,u.second+1));
			his[i]->replace(v.first,make_pair(u.first,v.second));
		}
		if(op==2)
		{
			int k;
			scanf("%d",&k);
			k^=last;
			his[i]=new rope<pii>(*his[k]);
		}
		if(op==3)
		{
			int a,b;
			scanf("%d%d",&a,&b);
			a^=last,b^=last;
			pii u=find(a,i-1);
			pii v=find(b,i-1);
			his[i]=new rope<pii>(*his[i-1]);
			printf("%d\n",u.first==v.first);
			last=u.first==v.first;
		}
	}
}

rope路径压缩:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<ext/rope>
#define pii pair<int,int>
#define N 200005
using namespace std;
using namespace __gnu_cxx;
rope<int>*his[N];
int find(int x,int root)
{
	if(x==his[root]->at(x))return x;
	int t=find(his[root]->at(x),root);
	if(t!=his[root]->at(x))his[root]->replace(x,t);//不判的话会mle 
	return his[root]->at(x);
}
int main()
{
	int n,m,last=0;
	scanf("%d%d",&n,&m);
	his[0]=new rope<int>;
	his[0]->push_back(0);
	for(int i=1;i<=n;i++)his[0]->push_back(i);
	for(int i=1;i<=m;i++)
	{
		int op,a,b,u,v,k;
		scanf("%d",&op);
		if(op==1)
		{
			scanf("%d%d",&a,&b);
			a^=last,b^=last;
			u=find(a,i-1);
			v=find(b,i-1);
			his[i]=new rope<int>(*his[i-1]);
			if(u==v)continue;
			his[i]->replace(v,u);
		}
		if(op==2)
		{
			scanf("%d",&k);
			k^=last;
			his[i]=new rope<int>(*his[k]);
		}
		if(op==3)
		{
			scanf("%d%d",&a,&b);
			a^=last,b^=last;
			u=find(a,i-1);
			v=find(b,i-1);
			his[i]=new rope<int>(*his[i-1]);
			printf("%d\n",u==v);
			last=u==v;
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值