【BZOJ3673】&&【BZOJ3674】: 可持久化并查集 by zky 可持久化线段树
没什么好说的。
可持久化线段树,叶子节点存放父亲信息,注意可以规定编号小的为父亲。
Q:不是很清楚空间开多大,每次询问父亲操作后修改的节点个数是不确定的。。
#include<bits/stdc++.h>
#define ll long long
#define N 20005
using namespace std;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
int n,m,rt[N],tot;
int f[N*],ls[N*],rs[N*];
void build(int &x,int l,int r){
x=++tot;
if(l==r){f[x]=l;return;}
int mid=l+r>>;
build(ls[x],l,mid);
build(rs[x],mid+,r);
}
int find(int x,int l,int r,int pos){
if(l==r)return f[x];
int mid=l+r>>;
if(pos<=mid)return find(ls[x],l,mid,pos);
else return find(rs[x],mid+,r,pos);
}
void update(int pre,int &x,int l,int r,int pos,int val){
x=++tot;ls[x]=ls[pre];rs[x]=rs[pre];
if(l==r){f[x]=val;return;}
int mid=l+r>>;
if(pos<=mid)update(ls[pre],ls[x],l,mid,pos,val);
else update(rs[pre],rs[x],mid+,r,pos,val);
}
int findfa(int ti,int x){
int tmp=find(rt[ti],,n,x);
if(tmp==x)return x;
else{
tmp=findfa(ti,tmp);
update(rt[ti],rt[ti],,n,x,tmp);
return tmp;
}
}
void unite(int ti,int x,int y){
int fx=findfa(ti,x),fy=findfa(ti,y);
if(fx<fy)swap(x,y),swap(fx,fy);
if(fx!=fy)update(rt[ti],rt[ti],,n,fx,fy);
}
int main(){
n=read();m=read();
build(rt[],,n);
for(int i=;i<=m;i++){
rt[i]=rt[i-];
int t=read();
if(t==){
int x=read(),y=read();
unite(i,x,y);
}
else if(t==){
int k=read();rt[i]=rt[k];
}
else{
int x=read(),y=read();
findfa(i,x)==findfa(i,y)?puts(""):puts("");
}
}
return ;
}
3673: 可持久化并查集 by zky
Time Limit: 5 Sec Memory Limit: 128 MB
Submit: 1506 Solved: 677
[Submit][Status][Discuss]
Description
n个集合 m个操作
操作:
1 a b 合并a,b所在集合
2 k 回到第k次操作之后的状态(查询算作操作)
3 a b 询问a,b是否属于同一集合,是则输出1否则输出0
0<n,m<=2*10^4
Input
Output
Sample Input
1 1 2
3 1 2
2 0
3 1 2
2 1
3 1 2
Sample Output
0
1
【BZOJ3673】&&【BZOJ3674】: 可持久化并查集 by zky 可持久化线段树的更多相关文章
- BZOJ3673 可持久化并查集 by zky 可持久化 并查集
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3673 题意概括 n个集合 m个操作操作:1 a b 合并a,b所在集合2 k 回到第k次操作之后的 ...
- bzoj3673可持久化并查集 by zky&&bzoj3674可持久化并查集加强版
bzoj3673可持久化并查集 by zky 题意: 维护可以恢复到第k次操作后的并查集. 题解: 用可持久化线段树维护并查集的fa数组和秩(在并查集里的深度),不能路径压缩所以用按秩启发式合并,可以 ...
- BZOJ3673 可持久化并查集 by zky 【主席树】
BZOJ3673 可持久化并查集 by zky Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a ...
- 【BZOJ】3673: 可持久化并查集 by zky & 3674: 可持久化并查集加强版(可持久化线段树)
http://www.lydsy.com/JudgeOnline/problem.php?id=3674 http://www.lydsy.com/JudgeOnline/problem.php?id ...
- 3673: 可持久化并查集 by zky
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 2170 Solved: 978[Submit][Status ...
- Bzoj 3673: 可持久化并查集 by zky(主席树+启发式合并)
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MB Description n个集合 m个操作 操作: 1 a b 合并a,b所在集 ...
- [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)
Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...
- bzoj 3673&3674: 可持久化并查集 by zky
Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...
- bzoj 3674: 可持久化并查集加强版 (启发式合并+主席树)
Description Description:自从zkysb出了可持久化并查集后……hzwer:乱写能AC,暴力踩标程KuribohG:我不路径压缩就过了!ndsf:暴力就可以轻松虐!zky:…… ...
随机推荐
- test1.A[【dfs简单题】
Test1.A Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 sdut 2274:http://acm.sdut.edu.cn/ ...
- AOJ673 聪明的输入法(字典树)
#include<cstdio> #include <cstdlib> #include <cstring> #include <iostream> # ...
- js判断当前的访问是手机/电脑
<script type="text/javascript"> var commonURL = 'http://www.xxx.com/'; function mobi ...
- 【leetcode】Sqrt(x)
题目描述: Implement int sqrt(int x). Compute and return the square root of x. 实现开根号,并且返回整数值(这个很重要,不是整数的话 ...
- [Oracle] PL/SQL学习笔记
-- 1. 使用一个变量 declare -- Local variables here v_name ); begin -- Test statements here select t.user_n ...
- android 消息推送
android 消息推送 极光推送百度云推送(语音)友盟消息推送
- 智能车学习(十)——MMA8451加速度计的使用
一.驱动说明: 就是使用I2C的通信方式驱动这款加速度计就行了,代码的话选择使用51单片机的代码进行移植. 二.代码分享: 1.头文件: #ifndef MMA8451_H #define MMA84 ...
- Struts2零配置介绍(约定访问)
从struts2.1开始,struts2 引入了Convention插件来支持零配置,使用约定无需struts.xml或者Annotation配置 需要 如下四个JAR包 插件会自动搜索如下类 act ...
- 【criteria CascadeType】级联的不同情况
使用criteria进行增删改查操作,可能会发生级联删除的情况,例如对员工表进行删除,可能会级联删除掉部门表中的某一条信息[类似这样的情况] 对此,我们可以在实体类中对级联的关系进行管理: 对于cri ...
- 【jackson 异常】com.fasterxml.jackson.databind.JsonMappingException异常处理
项目中,父层是Gene.java[基因实体] 子层是Corlib.java[文集库实体],一种基因对用多个文集库文章 但是在查询文集库这个实体的时候报错:[com.fasterxml.jackson ...