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

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

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

总时间复杂度$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. 发布本地jar到Nexus仓库

    mvn deploy:deploy-file -Durl=http://192.168.0.4:8081/nexus/content/repositories/thirdparty -Dreposit ...

  2. 2018年 7月总结&8月计划

    7月悄然而过... 英语: a打卡率:1号上课没有完成听力,7号上课没有完成阅读,21,22号考试 没有阅读 PS:学习效果测评 1)不要再阅读china English 2)单词要拼写 3)听力句子 ...

  3. php操作rabbitmq

    1.配置交换机,队列,然后绑定 <?php $conn_args = [ 'host' => '127.0.0.1', //rabbitmq 服务器host 'port' => 56 ...

  4. UVALive - 4126 Password Suspects (AC自动机+状压dp)

    给你m个字符串,让你构造一个字符串,包含所有的m个子串,问有多少种构造方法.如果答案不超过42,则按字典序输出所有可行解. 由于m很小,所以可以考虑状压. 首先对全部m个子串构造出AC自动机,每个节点 ...

  5. 欧拉函数 and 大数欧拉 (初步)

    前两天总结了素数筛法,其中就有Eular筛法.现在他又来了→→ φ(n),一般被称为欧拉函数.其定义为:小于n的正整数中与n互质的数的个数. 毕竟是伟大的数学家,所以以他名字命名的东西很多辣. 对于φ ...

  6. SQL夯实基础(六):MqSql Explain

    关系型数据库中,互联网相关行业使用最多的无疑是mysql,虽然我们C# Developer很多用的都是sql server ,但是学习一些mysql方面的知识也是必要的,他山之石么. 先上一个expl ...

  7. BZOJ4198:[NOI2015]荷马史诗

    浅谈\(Huffman\)树:https://www.cnblogs.com/AKMer/p/10300870.html 题目传送门:https://lydsy.com/JudgeOnline/pro ...

  8. maven用途、核心概念、用法、常用参数和命令、扩展

    设置问题解决. http://trinea.iteye.com/blog/1290898 本文由浅入深,主要介绍maven的用途.核心概念(Pom.Repositories.Artifact.Buil ...

  9. [转载]Sleep(x)的使用和sleep(0)作用解析

    链接:http://blog.csdn.net/m_leonwang/article/details/28434383 假设现在是 2012-12-16 3:37:40,如果我调用一下 Thread. ...

  10. Linux测试程序 - 多线程

    #include <sched.h> #include <pthread.h> main(){ pthread_t id0, id1, id2; ret=pthread_cre ...