欢迎访问~原文出处——博客园-zhouzhendong

去博客园看该题解


题目传送门 - BZOJ3673


题意概括

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

0<n,m<=2*10^4


题解

  上板子


代码

#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;
const int N=20005;
bool isd(char ch){
return '0'<=ch&&ch<='9';
}
void read(int &x){
x=0;
char ch=getchar();
while (!isd(ch))
ch=getchar();
while (isd(ch))
x=x*10+ch-48,ch=getchar();
}
int n,m,size,root[N],fa[N*50],ls[N*50],rs[N*50],depth[N*50];
int build(int L,int R){
int rt=++size;
if (L==R){
fa[rt]=L,depth[rt]=0;
return rt;
}
int mid=(L+R)>>1;
ls[rt]=build(L,mid);
rs[rt]=build(mid+1,R);
return rt;
}
int query(int rt,int le,int ri,int pos){
if (le==ri)
return rt;
int mid=(le+ri)>>1;
if (pos<=mid)
return query(ls[rt],le,mid,pos);
else
return query(rs[rt],mid+1,ri,pos);
}
void Modify(int prt,int &rt,int le,int ri,int pos,int val){
rt=++size;
if (le==ri){
fa[rt]=val;
depth[rt]=depth[prt];
return;
}
ls[rt]=ls[prt],rs[rt]=rs[prt];
int mid=(le+ri)>>1;
if (pos<=mid)
Modify(ls[prt],ls[rt],le,mid,pos,val);
else
Modify(rs[prt],rs[rt],mid+1,ri,pos,val);
}
void add(int rt,int le,int ri,int pos){
if (le==ri){
depth[rt]++;
return;
}
int mid=(le+ri)>>1;
if (pos<=mid)
add(ls[rt],le,mid,pos);
else
add(rs[rt],mid+1,ri,pos);
}
int find(int rt,int pos){
int p=query(rt,1,n,pos);
if (pos==fa[p])
return p;
return find(rt,fa[p]);
}
int main(){
size=0;
read(n),read(m);
root[0]=build(1,n);
for (int i=1;i<=m;i++){
int op,a,b;
root[i]=root[i-1];
read(op),read(a);
if (op==1){
read(b);
a=find(root[i],a),b=find(root[i],b);
if (fa[a]==fa[b])
continue;
if (depth[a]>depth[b])
swap(a,b);
Modify(root[i-1],root[i],1,n,fa[a],fa[b]);
if (depth[a]==depth[b])
add(root[i],1,n,fa[b]);
}
else if (op==2)
root[i]=root[a];
else {
root[i]=root[i-1];
read(b);
a=find(root[i],a),b=find(root[i],b);
printf("%d\n",fa[a]==fa[b]);
}
}
return 0;
}

  

BZOJ3673 可持久化并查集 by zky 可持久化 并查集的更多相关文章

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

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

  2. BZOJ3673 可持久化并查集 by zky 【主席树】

    BZOJ3673 可持久化并查集 by zky Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a ...

  3. bzoj3673可持久化并查集 by zky&&bzoj3674可持久化并查集加强版

    bzoj3673可持久化并查集 by zky 题意: 维护可以恢复到第k次操作后的并查集. 题解: 用可持久化线段树维护并查集的fa数组和秩(在并查集里的深度),不能路径压缩所以用按秩启发式合并,可以 ...

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

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

  5. 3673: 可持久化并查集 by zky

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

  6. Bzoj 3673: 可持久化并查集 by zky(主席树+启发式合并)

    3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MB Description n个集合 m个操作 操作: 1 a b 合并a,b所在集 ...

  7. bzoj 3673&3674: 可持久化并查集 by zky

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

  8. bzoj 3673&3674 可持久化并查集&加强版(可持久化线段树+启发式合并)

    CCZ在2015年8月25日也就是初三暑假要结束的时候就已经能切这种题了%%% 学习了另一种启发式合并的方法,按秩合并,也就是按树的深度合并,实际上是和按树的大小一个道理,但是感觉(至少在这题上)更好 ...

  9. 并查集——poj1703(带权并查集入门)

    传送门:Find them, Catch them 题意:警察抓获N个罪犯,这些罪犯只可能属于两个团伙中的一个,现在给出M个条件(D a b表示a和b不在同一团伙),对于每一个询问(A a b)确定a ...

随机推荐

  1. 8.SpringBoot 模板引擎 Thymeleaf

    1.模板引擎原理 JSP.Velocity.Freemarker.Thymeleaf 都是模板引擎.SpringBoot推荐的Thymeleaf:语法更简单,功能更强大: Thymeleaf模板引擎 ...

  2. python自带的调试器

    python是自带调试器的. 比如你写了一个python程序,名叫test.py. 你想调试一下这个程序,你可以执行 python -m pdb test.py,就会进入test.py的调试. 想查看 ...

  3. 第二节--Servlet

    1.在tomcat的webapp下新建一个web项目test 要有WEB-INF目录,其下有web.xml.   2.WEB-INF下文件是给tomcat使用的 3.用户访问localhost:848 ...

  4. 深度神经网络tricks and tips

    1)data augmentation (augment 增加,aug:to increase 词根,同August(奥古斯特即凯撒大帝,自认为最伟大的帝王,他出生在八月,他以自己的名字命名这个月)同 ...

  5. js 获取属性名称

    $(function ()        {            myfun();        })        function myfun()        {            var ...

  6. Node.js的基本操作(使用Vue前必学)

    安装 Windows下不需要讲,Mac下的安装步骤: https://www.cnblogs.com/bobi1234/category/1367458.html 全局安装淘宝镜像 npm insta ...

  7. Python中的部分特殊属性

    __name__ is the class name;   返回类名 __module__ is the module name in which the class was defined;   定 ...

  8. A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)

    题目链接:https://cn.vjudge.net/contest/284294#problem/A 题目大意:主席树查询区间第k小. 具体思路:主席树入门. AC代码: #include<i ...

  9. IMU 预积分推导

    给 StereoDSO 加 IMU,想直接用 OKVIS 的代码,但是有点看不懂.知乎上郑帆写的文章<四元数矩阵与 so(3) 左右雅可比>提到 OKVIS 的预积分是使用四元数,而预积分 ...

  10. Protues常用元器件查找对应表

    原理图常用库文件:Miscellaneous Devices.ddbDallas Microprocessor.ddbIntel Databooks.ddbProtel DOS Schematic L ...