P3402 可持久化并查集

题目链接:点击这里

题目大意:
给定 n n n 个集合,第 i i i 个集合内初始状态下只有一个数,为 i i i

m m m 次操作。操作分为 3 3 3 种:

1   a   b 1\ a\ b 1 a b 合并 a , b a,b a,b 所在集合;

2   k 2\ k 2 k 回到第 k k k 次操作(执行三种操作中的任意一种都记为一次操作)之后的状态;

3   a   b 3\ a\ b 3 a b 询问 a , b a,b a,b 是否属于同一集合,如果是则输出 1 1 1 ,否则输出 0 0 0
对每个操作 3 3 3,输出一行一个整数表示答案

题目分析:
使用主席树构建可持久化数组来充当原来并查集的数组下标,对于查找下标直接对应可持久化数组的查找操作
可持久化并查集需要用按秩合并的思想来进行合并操作来保证时间复杂度,不用路径压缩是因为使用路径压缩可能使得更新是产生很多的冗余空间容易 M L E MLE MLE

具体细节见代码:

// Problem: P3402 可持久化并查集
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3402
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

//#pragma GCC optimize(2)
//#pragma GCC optimize("Ofast","inline","-ffast-math")
//#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<unordered_map>
#define ll long long
#define inf 0x3f3f3f3f
#define Inf 0x3f3f3f3f3f3f3f3f
//#define int  ll
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
using namespace std;
int read()
{
	int res = 0,flag = 1;
	char ch = getchar();
	while(ch<'0' || ch>'9')
	{
		if(ch == '-') flag = -1;
		ch = getchar();
	}
	while(ch>='0' && ch<='9')
	{
		res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
		ch = getchar();
	}
	return res*flag;
}
const int maxn = 1e6+5;
const int mod = 1e9+7;
const double pi = acos(-1);
const double eps = 1e-8;
struct node{
	int l,r;
}nod[maxn*32];
int n,m,cnt,root[maxn],fa[maxn*32],dep[maxn*32];
void build(int &u,int l,int r)
{
	nod[u = ++cnt];
	if(l == r)
	{
		fa[u] = l;
		return ;
	}
	int mid = l+r>>1;
	build(nod[u].l,l,mid);
	build(nod[u].r,mid+1,r);
}
int query(int u,int l,int r,int pos)
{
	if(l == r) return u;
	int mid = l+r>>1;
	if(pos <= mid) return query(nod[u].l,l,mid,pos);
	else return query(nod[u].r,mid+1,r,pos);
}
int find(int root,int x)
{
	int pos = query(root,1,n,x);
	if(fa[pos] == x) return pos;
	return find(root,fa[pos]);
}
void update(int &u,int pre,int l,int r,int pos,int fax) // 修改pos的父亲为fax
{
	nod[u = ++cnt];
	if(l == r)
	{
		fa[u] = fax;
		dep[u] = dep[pre];
		return ;
	}
	int mid = l+r>>1;
	nod[u].l = nod[pre].l;
	nod[u].r = nod[pre].r;
	if(pos <= mid) update(nod[u].l,nod[pre].l,l,mid,pos,fax);
	else update(nod[u].r,nod[pre].r,mid+1,r,pos,fax);
}
void add(int u,int l,int r,int pos) // 把某一个并查集的根深度加一
{
	if(l == r)
	{
		dep[u]++;
		return ;
	}
	int mid = l+r>>1;
	if(pos <= mid) add(nod[u].l,l,mid,pos);
	else add(nod[u].r,mid+1,r,pos);
}
int main()
{
	n = read(),m = read();
	build(root[0],1,n);
	for(int i = 1;i <= m;i++)
	{
		int opt = read();
		if(opt == 1)
		{
			int x = read(),y = read();
			root[i] = root[i-1];
			int px = find(root[i],x),py = find(root[i],y);
			if(fa[px] == fa[py]) continue;
			if(dep[px] > dep[py]) swap(px,py);
			update(root[i],root[i-1],1,n,fa[px],fa[py]);
			if(dep[px] == dep[py]) add(root[i],1,n,fa[py]); // 注意是fa[py],因为此时的新根是fa[py],深度比原来增加了1
		}
		else if(opt == 2)
		{
			int id = read();
			root[i] = root[id];
		}
		else if(opt == 3)
		{
			int x = read(),y = read();
			root[i] = root[i-1];
			int fax = find(root[i],x),fay = find(root[i],y);
			puts(fax==fay ? "1" : "0");
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值