点开这题纯属无聊……不过既然写掉了,那就丢一个模板好了

不得不说,可持久化并查集实现真的很暴力,就是把并查集的数组弄一个主席树可持久化。

有一点要注意的是不能写路径压缩,这样跳版本的时候会错,所以弄一个按秩合并来降低时间复杂度。

总时间复杂度$O(nlog^2n)$。

听说用siz实现按秩合并会比较好,因为这样还能查询集合大小,有时间以后补上。

Code:

#include <cstdio>
using namespace std; const int N = 1e5 + ;
const int M = 2e5 + ; int n, qn; inline void read(int &X) {
X = ;
char ch = ;
int op = ;
for(; ch > ''|| ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} struct Node {
int lc, rc, dep, fa;
}; namespace PUfs {
Node s[N * ];
int root[M * ], nodeCnt; #define mid ((l + r) >> 1) void build(int &p, int l, int r) {
p = ++nodeCnt;
if(l == r) {
s[p].fa = l;
return;
} build(s[p].lc, l, mid);
build(s[p].rc, mid + , r);
} void modify(int &p, int l, int r, int x, int f, int pre) {
p = ++nodeCnt;
if(l == r) {
s[p].fa = f;
s[p].dep = s[pre].dep;
return;
} s[p].lc = s[pre].lc, s[p].rc = s[pre].rc;
if(x <= mid) modify(s[p].lc, l, mid, x, f, s[pre].lc);
else modify(s[p].rc, mid + , r, x, f, s[pre].rc);
} void add(int p, int l, int r, int x) {
if(l == r) {
s[p].dep++;
return;
}
if(x <= mid) add(s[p].lc, l, mid, x);
else add(s[p].rc, mid + , r, x);
} int query(int p, int l, int r, int x) {
if(l == r) return p; if(x <= mid) return query(s[p].lc, l, mid, x);
else return query(s[p].rc, mid + , r, x);
} int find(int now, int x) {
int f = query(now, , n, x);
if(s[f].fa == x) return f;
return find(now, s[f].fa);
} } using namespace PUfs; inline void print(int p) {
printf("%d: ", p);
for(int j = ; j <= n; j++)
printf("%d ", s[find(root[p], j)].fa);
/* for(int j = 1; j <= n; j++)
printf("%d ", query(root[p], 1, n, j)); */
printf("\n");
} inline void swap(int &x, int &y) {
int t = x;
x = y;
y = t;
} int main() {
read(n), read(qn); nodeCnt = ;
build(root[], , n); // print(0); for(int op, i = ; i <= qn; i++) {
read(op);
if(op == ) {
root[i] = root[i - ];
int x, y;
read(x), read(y);
int fx = find(root[i], x), fy = find(root[i], y);
if(s[fx].fa == s[fy].fa) continue;
if(s[fx].dep > s[fy].dep) swap(fx, fy);
modify(root[i], , n, s[fx].fa, s[fy].fa, root[i - ]);
if(s[fx].dep == s[fy].dep) add(root[i], , n, s[fy].fa);
}
if(op == ) {
int k;
read(k);
root[i] = root[k];
}
if(op == ) {
root[i] = root[i - ];
int x, y;
read(x), read(y);
int fx = find(root[i], x), fy = find(root[i], y);
if(s[fx].fa == s[fy].fa) puts("");
else puts("");
}
// print(i);
} return ;
}

Luogu 3402 可持久化并查集的更多相关文章

  1. 洛谷P4768 [NOI2018]归程 [可持久化并查集,Dijkstra]

    题目传送门 归程 格式难调,题面就不放了. 分析: 之前同步赛的时候反正就一脸懵逼,然后场场暴力大战,现在呢,还是不会$Kruskal$重构树,于是就拿可持久化并查集做. 但是之前做可持久化并查集的时 ...

  2. 洛谷P3402 【模板】可持久化并查集 [主席树,并查集]

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

  3. bzoj3673 & bzoj3674 & 洛谷P3402 可持久化并查集

    题目:bzoj3673:https://www.lydsy.com/JudgeOnline/problem.php?id=3673 bzoj3674:https://www.lydsy.com/Jud ...

  4. 带撤销并查集 & 可持久化并查集

    带撤销并查集支持从某个元素从原来的集合中撤出来,然后加入到一个另外一个集合中,或者删除该元素 用一个映射来表示元素和并查集中序号的关系,代码中用\(to[x]\) 表示x号元素在并查集中的 id 删除 ...

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

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

  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. winform使用相对路径读取文件的方法

    获取exe文件的路径进行截取,分两次进行,然后拼接文件名,形成全路径 代码如下: string haarXmlPath = @"haarcascade_frontalface_alt_tre ...

  2. L121

    今天上午签字仪式的布置与该场合的严肃性非常协调.The setting for this morning's signing ceremony matched the solemnity of the ...

  3. python虚拟开发环境搭建(virtualenv和virtualenvwrapper)

    虚拟开发环境的搭建 (0) 搭建虚拟环境的意义 使不同的开发环境独立 环境升级不影响其他开发环境,也不影响全局 防止包管理的混乱 (1) 指定 虚拟环境的创建目录 环境变量设置 创建 WORKON_H ...

  4. file_put_contents();

    file_put_contents(); 用于获取文件中的内容,可以填写网址,但是需要以http://开头

  5. Centos 7 安装 Python3.7

    目录 下载Python Python安装 遇到问题 错误: configure: error: no acceptable C compiler found in $PATH 错误: can't de ...

  6. 生成Texture2D纹理图片

    using UnityEngine;using System.Collections; public class ProceduralTexture : MonoBehaviour{ public i ...

  7. 浅议Windows 2000/XP Pagefile组织管理

    任何时候系统内存资源相对磁盘空间来说都是相形见拙的.因为虚拟内存机制,使我们可以有相对丰富的地址资源(通常32bit的虚拟地址,可以有4G的寻址 空间),而这些资源对物理内存来说一般情况是总是绰绰有余 ...

  8. 第六篇 VIM你值得拥有!

    vim 是一个具有很多命令的功能非常强大的编辑器.限于篇幅,在本教程当中      就不详细介绍了.本教程的设计目标是讲述一些必要的基本命令,而掌握好这      些命令,您就能够很容易将vim当作一 ...

  9. Jet Brains家族XX方法

    声明:本文转载自 https://www.jianshu.com/p/f404994e2843 2018/11/23

  10. laravel csrf保护

    有时候我们的项目需要和外部的项目进行接口对接,如果是post的方式请求;laravel要求csrf保护 但是别人是ci框架或者没有csrf_token的;该如何处理呢? 可以把我们不需要csrf的ur ...