带撤销并查集支持从某个元素从原来的集合中撤出来,然后加入到一个另外一个集合中,或者删除该元素

用一个映射来表示元素和并查集中序号的关系,代码中用\(to[x]\) 表示x号元素在并查集中的 id

删除 x 号元素时,需要将 \(to[x]\) 的集合大小减去1,然后令 \(to[x]=-1\) 标记 x 删除即可

如果要重新加入一个元素,那么给x分配一个新的 id,\(to[x] = newid\)

例题1:https://www.cometoj.com/contest/33/problem/E?problem_id=1459

#include <bits/stdc++.h>
using namespace std;
const int N = 4000010;
int fa[N],to[N],sz[N],cnt,a[N],b[N],tot;
int n,m,k,x,y;
int ok[N];
int find(int x){
return x == fa[x]?x:fa[x] = find(fa[x]);
}
void merge(int x,int y){
int a = find(to[x]);
int b = find(to[y]);
if(a==b) return;
fa[b] = a;
sz[a] += sz[b]; sz[b] = 0;
}
// 将x从原集合删除,加入到 y 所属的集合
void update(int x,int y){
sz[find(to[x])]--;
to[x] = ++cnt;//给x分配新的 id
sz[cnt] = 1;
fa[cnt] = cnt;
merge(x,y);
}
int main(){
scanf("%d%d",&n,&m);cnt = n;
for(int i=1;i<=n;i++)fa[i] = i,to[i] = i,sz[i] = 1;
for(int i=1;i<=m;i++){
scanf("%d%d",&k,&x);if(k!=3)scanf("%d",&y);
if(k == 5){
a[++tot] = x;
b[tot] = y;continue;
}
if(k == 1)merge(x,y);
if(k == 2)update(x,y);
if(k == 4){
if(find(to[x]) == find(to[y]))printf("Yes\n");
else printf("No\n");
} if(k == 3)printf("%d\n",sz[find(to[x])]-1);
} for(int i=1;i<=tot;i++){
if(find(to[a[i]]) == find(to[b[i]])) ok[find(to[a[i]])] = 1;
}
int res = -1;
for(int i=1;i<=n;i++)
if(!ok[find(to[i])])res = max(res,sz[find(to[i])]);
cout<<res<<endl;
return 0;
}

例题2:https://nanti.jisuanke.com/t/42576

[2019南昌区域赛A]

不要求在线的可持久化操作,可以离线处理询问,按照正常的时间顺序维护并查集,由于需要回溯,所以不能使用路径压缩

复杂度 \(O(m\log n)\)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
#define dbg(x...) do { cout << "\033[32;1m" << #x <<" -> "; err(x); } while (0)
void err() { cout << "\033[39;0m" << endl; }
template<class T, class... Ts> void err(const T& arg,const Ts&... args) { cout << arg << " "; err(args...); }
const int N = 1000000 + 50;
struct Node{
int op, x, y;
}o[N];
vector<int> g[N];
int n, m;
int fa[N*2], dep[N*2], sz[N*2], to[N], tot;
int res[N];
int find(int x){
if(x == fa[x]) return x;
return find(fa[x]);
}
/*
1 k a b merge(a, b)
2 k a del(a)
3 k a b merge(a,b)
4 k a b find(a) == find(b)
5 k a sz[a]
*/
void dfs(int u){
for(auto id : g[u]){
int op = o[id].op, x = o[id].x, y = o[id].y;
if(op == 1){
if(to[x] == -1 || to[y] == -1){
dfs(id);
continue;
}
int rx = find(to[x]), ry = find(to[y]);
if(rx == ry){ dfs(id); continue;}
else if(dep[rx] == dep[ry]){
fa[ry] = rx; sz[rx] += sz[ry]; dep[rx] ++;
dfs(id);
fa[ry] = ry; sz[rx] -= sz[ry]; dep[rx] --;
} else {
if(dep[rx] < dep[ry]) swap(rx, ry);
fa[ry] = rx; sz[rx] += sz[ry];
dfs(id);
fa[ry] = ry; sz[rx] -= sz[ry];
}
} else if(op == 2){
if(to[x] == -1) { dfs(id); continue; }
int rx = find(to[x]);
int t = to[x];
to[x] = -1;
sz[rx] --;
dfs(id);
to[x] = t;
sz[rx] ++;
} else if(op == 3){
if(to[x] == - 1 || to[y] == -1) {dfs(id); continue; }
int t = to[x];
int rx = find(to[x]), ry = find(to[y]);
sz[rx]--;
to[x] = ++tot;
sz[to[x]] = 1;
fa[to[x]] = ry;
sz[ry] ++;
dfs(id);
sz[ry] --;
sz[rx] ++;
to[x] = t;
} else {
if(op == 4){
if(to[x] == -1 || to[y] == -1){
res[id] = 0;
} else {
res[id] = find(to[x]) == find(to[y]);
}
} else if(op == 5){
if(to[x] == -1) res[id] = 0;
else {
res[id] = sz[find(to[x])];
}
}
dfs(id);
}
}
}
int main(){
scanf("%d%d", &n, &m);
for(int i=1;i<=m;i++){
int op, k, x, y = 0;scanf("%d%d%d", &op, &k, &x);
g[k].push_back(i);
if(op != 2 && op != 5) scanf("%d", &y);
o[i] = {op, x, y};
}
for(int i=1;i<=n;i++) to[i] = i, fa[i] = i, sz[i] = 1;
tot = n;
dfs(0);
for(int i=1;i<=m;i++){
if(o[i].op == 4) puts(res[i] ? "Yes" : "No");
else if(o[i].op == 5){
printf("%d\n", res[i]);
}
}
return 0;
}

可持久化并查集支持查询任一历史版本的信息。并查集信息用数组 \(fa\) 表示,合并集合时,基本操作有两种,一个是路径压缩(可能会修改很多个fa),一个是按秩合并(启发式合并思路,小的集合向大的合并,只会修改一次fa),由于要保存历史信息,按照可持久化的一贯思路,每一次操作都会新开 logn 的空间,所以这里要用按秩合并。

注意按秩合并的find函数写法也有所不同,不能修改fa

此时问题就是维护每个版本的fa数组

模版题:https://www.luogu.com.cn/problem/P3402

const int N = 200000 + 5;
int n, m;
struct TreeNode{
int l, r;
}t[N*30];
int root[N], tot;
int fa[N*30], dep[N*30];
void build(int &p, int l, int r){
p = ++tot;
if(l == r){fa[p] = l;return;}
int mid = l + r >> 1;
build(t[p].l, l, mid);
build(t[p].r, mid+1, r);
}
// 修改p版本的pos位置的fa值
void change(int last, int &p, int l, int r, int pos, int val){
p = ++tot;
t[p] = t[last];
if(l == r){
fa[p] = val;
dep[p] = dep[last];
return;
}
int mid = l + r >> 1;
if(pos <= mid)
change(t[last].l, t[p].l, l, mid, pos, val);
else
change(t[last].r, t[p].r, mid+1, r, pos, val);
}
int getIndex(int p, int l, int r,int pos){
if(l == r) return p;
int mid = l + r >> 1;
if(pos <= mid) return getIndex(t[p].l, l, mid, pos);
else return getIndex(t[p].r, mid+1, r, pos);
}
int find(int p, int x){
int index = getIndex(p, 1, n, x);
if(fa[index] == x) return index; // 返回祖先节点下标
return find(p, fa[index]);
}
void merge(int last, int &p, int x, int y){
int fx = find(p, x), fy = find(p, y);
if(fa[fx] == fa[fy]) return;
// x-> y合并,需要满足 dep[x]<dep[y]
if(dep[fx] > dep[fy]) swap(fx, fy);
change(last, p, 1, n, fa[fx], fa[fy]);
if(dep[fx] == dep[fy]){
dep[getIndex(p, 1, n, fa[fy])]++;
}
}
int main(){
scanf("%d%d", &n, &m);
build(root[0], 1, n);
for(int i=1;i<=m;i++){
root[i] = root[i-1];
int op, x, y;scanf("%d%d", &op, &x);
if(op == 1){
scanf("%d", &y);
merge(root[i], root[i], x, y);
}
else if(op == 2) root[i] = root[x];
else {
scanf("%d", &y);
int fx = find(root[i], x), fy = find(root[i], y);
if(fa[fx] == fa[fy]) puts("1");
else puts("0");
}
}
return 0;
}

例题:https://www.luogu.com.cn/problem/P4768

先跑一次最短路,然后按照边权从大到小用带权并查集维护集合到源点的最短距离。保留所有版本的信息,然后对于每次查询找到对应版本号回答问题即可

复杂度:\(O(n\log m + (m+q) \log^2n)\)

带撤销并查集 & 可持久化并查集的更多相关文章

  1. 算法笔记--可撤销并查集 && 可持久化并查集

    可撤销并查集模板: struct UFS { stack<pair<int*, int>> stk; int fa[N], rnk[N]; inline void init(i ...

  2. [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)

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

  3. 【BZOJ3673/3674】可持久化并查集/可持久化并查集加强版 可持久化线段树

    [BZOJ3674]可持久化并查集加强版 Description Description:自从zkysb出了可持久化并查集后……hzwer:乱写能AC,暴力踩标程KuribohG:我不路径压缩就过了! ...

  4. BZOJ4358: permu(带撤销并查集 不删除莫队)

    题意 题目链接 Sol 感觉自己已经老的爬不动了.. 想了一会儿,大概用个不删除莫队+带撤销并查集就能搞了吧,\(n \sqrt{n} logn\)应该卡的过去 不过不删除莫队咋写来着?....跑去学 ...

  5. 【BZOJ】【3673】可持久化并查集 & 【3674】可持久化并查集加强版

    可持久化并查集 Orz hzwer & zyf 呃学习了一下可持久化并查集的姿势……其实并查集就是一个fa数组(可能还要带一个size或rank数组),那么我们对并查集可持久化其实就是实现一个 ...

  6. 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 1878  Solved: 846[Submit][Status ...

  7. bzoj3674 可持久化并查集

    我是萌萌的任意门 可持久化并查集的模板题-- 做法好像很多,可以标号法,可以森林法. 本来有O(mloglogn)的神算法(按秩合并+倍增),然而我这种鶸渣就只会写O(mlog2n)的民科算法--再加 ...

  8. 【BZOJ3673】&&【BZOJ3674】: 可持久化并查集 by zky 可持久化线段树

    没什么好说的. 可持久化线段树,叶子节点存放父亲信息,注意可以规定编号小的为父亲. Q:不是很清楚空间开多大,每次询问父亲操作后修改的节点个数是不确定的.. #include<bits/stdc ...

  9. 【BZOJ】3673: 可持久化并查集 by zky & 3674: 可持久化并查集加强版(可持久化线段树)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3674 http://www.lydsy.com/JudgeOnline/problem.php?id ...

随机推荐

  1. 观《if (domain logic) then CQRS, or Saga?》所悟

    引言 Udi Dahan曾在2017年阿姆斯特丹的DDD欧洲年会上发表过一篇演讲--if (domain logic) then CQRS, or Saga.视频是UP主从Youtube搬运的,我听力 ...

  2. Github 简单使用

    第一步:打开官网:https://github.com 注册一个帐户. 第二步:创建仓库 填写仓库的名字和描述. 创建好了之后,点击"Branch master",创建分支--在文 ...

  3. XSS - Pikachu

    概述: Cross-Site Scripting 简称为"CSS",为避免与前端叠成样式表的缩写"CSS"冲突,故又称XSS.一般XSS可以分为如下几种常见类型 ...

  4. ctfhub技能树—信息泄露—备份文件下载—bak文件

    打开靶机 查看页面信息 继续使用dirsearch进行扫描 python3 dirsearch.py -u http://challenge-d4234042e1d43e96.sandbox.ctfh ...

  5. SAP GUI用颜色区分不同的系统

    对于经常打开多个窗口的SAP用户,有时候可能同时登录了生产机.测试机和开发机,为了避免误操作,比如在测试要执行的操作,结果在生产机做了,结果可想而知. 虽然可以通过右下角查看再去判断,但是总是没有通过 ...

  6. CALL TRANSACTION 使用说明

    以调用事务VA03为例: 在程序中添加如下代码就可以实现  SET PARAMETER ID 'AUN' FIELD var. CALL TRANSACTION 'VA03' AND SKIP FIR ...

  7. 简单明朗的 RNN 写诗教程

    目录 简单明朗的 RNN 写诗教程 数据集介绍 代码思路 输入 and 输出 训练集构建 生成一首完整的诗 代码实现 读取文件 统计字数 构建word 与 id的映射 转成one-hot代码 随机打乱 ...

  8. H3C、Huawei、Cisco网络设备AAA TACACS认证配置

    TACACS技术白皮书 摘要:TACACS是实现AAA功能的一种安全协议,主要是通过TACACS客户端与TACACS服务器通信来实现多种用户的AAA功能. HWTACACS采用TCP协议承载报文,TC ...

  9. eNSP启动设备AR1失败记一次解决步骤

    eNSP稳定版本下载:   微信搜索公众号"疯刘小三" 关注后回复ensp即可获得下载链接地址 eNSP V100R002C00B510 Setup.exe 最近在用eNSp的时候 ...

  10. 为什么 TCP 协议有粘包问题

    为什么 TCP 协议有粘包问题 这部分转载自draveness博客. TCP/IP 协议簇建立了互联网中通信协议的概念模型,该协议簇中的两个主要协议就是 TCP 和 IP 协议.TCP/ IP 协议簇 ...