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

5 6
1 1 2
3 1 2
2 0
3 1 2
2 1
3 1 2

Sample Output

1
0
1

思路:

用主席树去维护一个可持久化的数组,并查集的操作就变成了在这个可持久化数组上跳来跳去,
连接两个点x,y就直接在主席树上下标为x点赋值为y,这样查询的时候只要一直跳就可以跳到根节点
实现代码;
#include<bits/stdc++.h>
using namespace std;
#define mid int m = (l + r) >> 1
const int M = 2e6 + ; int sum[M],ls[M],rs[M],dep[M],n,idx,root[M];
void build(int l,int r,int &rt){
rt = ++idx;
if(l == r){
sum[rt] = l;
return ;
}
mid;
build(l,m,ls[rt]); build(m+,r,rs[rt]);
return ;
} void update(int old,int &rt,int p,int c,int l,int r){
rt = ++idx; ls[rt] = ls[old]; rs[rt] = rs[old];
dep[rt] = dep[old];
if(l == r){
sum[rt] = c;
return ;
}
mid;
if(p <= m) update(ls[old],ls[rt],p,c,l,m);
else update(rs[old],rs[rt],p,c,m+,r);
} int query(int p,int l,int r,int rt){
if(l == r) return rt;
mid;
if(p <= m) return query(p,l,m,ls[rt]);
else return query(p,m+,r,rs[rt]);
} void add(int p,int l,int r,int rt){
if(l == r){
dep[rt] ++;
return;
}
mid;
if(p <= m) add(p,l,m,ls[rt]);
else add(p,m+,r,rs[rt]);
} int fd(int x,int rt){
int pos = query(x,,n,rt);
if(x == sum[pos]) return pos;
return fd(sum[pos],rt);
} int main()
{
int q,op,x,y,k;
scanf("%d%d",&n,&q);
build(,n,root[]);
for(int i = ;i <= q;i ++){
scanf("%d",&op);
if(op == ){
scanf("%d%d",&x,&y);
root[i] = root[i-];
int fx = fd(x,root[i-]);
int fy = fd(y,root[i-]);
if(sum[fx] == sum[fy]) continue;
if(dep[fx] > dep[fy]) swap(fx,fy);
update(root[i-],root[i],sum[fx],sum[fy],,n);
if(dep[fx] == dep[fy]) add(sum[fy],,n,root[i]);
}
else if(op == ){
scanf("%d",&k);
root[i] = root[k];
}
else {
root[i] = root[i-];
scanf("%d%d",&x,&y);
int fx = fd(x,root[i]);
int fy = fd(y,root[i]);
if(sum[fx] == sum[fy]) printf("1\n");
else printf("0\n");
}
}
return ;
}

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

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

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

  2. BZOJ 3673 可持久化并查集 by zky && BZOJ 3674 可持久化并查集加强版 可持久化线段树

    既然有了可持久化数组,就有可持久化并查集.. 由于上课讲过说是只能按秩合并(但是我也不确定...),所以就先写了按秩合并,相当于是维护fa[]和rk[] getf就是在这棵树中找,直到找到一个点的fa ...

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

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

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

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

  5. BZOJ 3673: 可持久化并查集(可持久化并查集+启发式合并)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3673 题意: 思路: 可持久化数组可以用可持久化线段树来实现,并查集的查询操作和原来的一般并查集操作 ...

  6. bzoj 3673 可持久化并查集

    本质上是维护两个可持久化数组,用可持久化线段树维护. /************************************************************** Problem: ...

  7. BZOJ 3674 可持久化并查集加强版(主席树变形)

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MB Submit: 2515  Solved: 1107 [Submit][Sta ...

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

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

  9. BZOJ 3674 可持久化并查集加强版(路径压缩版本)

    /* bzoj 3674: 可持久化并查集加强版 http://www.lydsy.com/JudgeOnline/problem.php?id=3674 用可持久化线段树维护可持久化数组从而实现可持 ...

随机推荐

  1. 05 Django REST Framework 分页

    01-分页模式 rest framework中提供了三种分页模式: from rest_framework.pagination import PageNumberPagination, LimitO ...

  2. Codeforces Round #485 (Div. 2)-B-High School: Become Human

    B. High School: Become Human time limit per test 1 second memory limit per test 256 megabytes input ...

  3. hibernate异常找不到get方法org.hibernate.PropertyNotFoundException: Could not find a getter for did in class com.javakc.hibernate.manytomany.entity.CourseEntity

    属性的get方法没找到,可能是CourseEntity类中对应属性没有get方法,如果有就看CourseEntity.hbm.xml属性名称,应该是写错了不和CourseEntity类中属性名相同,修 ...

  4. Mike and distribution CodeForces - 798D (贪心+思维)

    题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...

  5. Elasticsearch之配置详解

    Cluster 集群名称,默认为elasticsearch: cluster.name: elasticsearch 设置一个节点的并发数量,有两种情况,一种是在初始复苏过程中: cluster.ro ...

  6. 软件工程(FZU2015) 赛季得分榜,第五回合

    SE_FZU目录:1 2 3 4 5 6 7 8 9 10 11 12 13 积分规则 积分制: 作业为10分制,练习为3分制:alpha30分: 团队项目分=团队得分+个人贡献分 个人贡献分: 个人 ...

  7. 第五章 动态SQL 批量操作

    用于实现动态SQL的元素主要有 if trim where set choose(when.otherwise) foreach MyBatis  缓存 一级缓存 在test类中 调用相同的方法 第二 ...

  8. js数据放入缓存,需要再调用

    再贴代码之前先描述下,这个技术应用的场景:一个页面的http请求次数能少点就少,这样大大提高用户体验.所以再一个页面发起一个请求,把所有数据都拿到后储存在缓存里面,你想用的时候再调用出来,这个是非常好 ...

  9. linux 安装ssh以及ssh用法与免密登录

    想要免费登录就是把本地机器的id_rsa_pub的内容放到远程服务器的authorized_keys里面 一.配置yum和hosts文件 配置hosts文件: 命令:vi /etc/hosts 在文件 ...

  10. 缓存session,cookie,sessionStorage,localStorage的区别

    https://www.cnblogs.com/cencenyue/p/7604651.html(copy) 浅谈session,cookie,sessionStorage,localStorage的 ...