BZOJ3674可持久化并查集(模板)
没什么可说的,就是一个可持久化线段树维护一个数组fa以及deep按秩合并好了
注意一下强制在线
蒟蒻的我搞了好长时间QAQ
贴代码:
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- struct trnt{
- int ls;
- int rs;
- int fa;
- int dp;
- }tr[];
- int root[];
- int siz;
- int n,m;
- int lastans;
- void Tr_build(int l,int r,int &spc)
- {
- if(!spc)
- spc=++siz;
- if(l==r)
- {
- tr[spc].fa=l;
- return ;
- }
- int mid=(l+r)/;
- Tr_build(l,mid,tr[spc].ls);
- Tr_build(mid+,r,tr[spc].rs);
- return ;
- }
- int ask(int l,int r,int pos,int spc)
- {
- if(l==r)
- return spc;
- int mid=(l+r)/;
- if(pos<=mid)
- return ask(l,mid,pos,tr[spc].ls);
- return ask(mid+,r,pos,tr[spc].rs);
- }
- int finf(int rt,int x)
- {
- int ff=ask(,n,x,rt);
- if(tr[ff].fa==x)
- return ff;
- return finf(rt,tr[ff].fa);
- }
- void unin(int l,int r,int &spc,int last,int pos,int ff)
- {
- spc=++siz;
- if(l==r)
- {
- tr[spc].fa=ff;
- tr[spc].dp=tr[last].dp;
- return ;
- }
- tr[spc].ls=tr[last].ls;
- tr[spc].rs=tr[last].rs;
- int mid=(l+r)/;
- if(pos<=mid)
- unin(l,mid,tr[spc].ls,tr[last].ls,pos,ff);
- else
- unin(mid+,r,tr[spc].rs,tr[last].rs,pos,ff);
- return ;
- }
- void grow(int l,int r,int pos,int spc)
- {
- if(l==r)
- {
- tr[spc].dp++;
- return ;
- }
- int mid=(l+r)/;
- if(pos<=mid)
- grow(l,mid,pos,tr[spc].ls);
- else
- grow(mid+,r,pos,tr[spc].rs);
- return ;
- }
- int main()
- {
- scanf("%d%d",&n,&m);
- Tr_build(,n,root[]);
- for(int i=;i<=m;i++)
- {
- root[i]=root[i-];
- int cmd;
- scanf("%d",&cmd);
- if(cmd==)
- {
- int x,y;
- scanf("%d%d",&x,&y);
- x=x^lastans;
- y=y^lastans;
- x=finf(root[i],x);
- y=finf(root[i],y);
- if(tr[x].fa==tr[y].fa)
- continue;
- if(tr[x].dp>tr[y].dp)
- std::swap(x,y);
- unin(,n,root[i],root[i-],tr[x].fa,tr[y].fa);
- if(tr[x].dp==tr[y].dp)
- grow(,n,tr[y].fa,root[i]);
- }else if(cmd==)
- {
- int x;
- scanf("%d",&x);
- x=x^lastans;
- root[i]=root[x];
- }else{
- int x,y;
- scanf("%d%d",&x,&y);
- x=x^lastans;
- y=y^lastans;
- x=finf(root[i],x);
- y=finf(root[i],y);
- if(tr[x].fa==tr[y].fa)
- lastans=;
- else
- lastans=;
- printf("%d\n",lastans);
- }
- }
- return ;
- }
BZOJ3674可持久化并查集(模板)的更多相关文章
- bzoj3673可持久化并查集 by zky&&bzoj3674可持久化并查集加强版
bzoj3673可持久化并查集 by zky 题意: 维护可以恢复到第k次操作后的并查集. 题解: 用可持久化线段树维护并查集的fa数组和秩(在并查集里的深度),不能路径压缩所以用按秩启发式合并,可以 ...
- bzoj3674 可持久化并查集
我是萌萌的任意门 可持久化并查集的模板题-- 做法好像很多,可以标号法,可以森林法. 本来有O(mloglogn)的神算法(按秩合并+倍增),然而我这种鶸渣就只会写O(mlog2n)的民科算法--再加 ...
- BZOJ3674: 可持久化并查集加强版
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3674 题解:主要是可持久化的思想.膜拜了一下hzwer的代码后懂了. 其实本质是可持久化fa数 ...
- 2019.01.21 bzoj3674: 可持久化并查集加强版(主席树+并查集)
传送门 题意:维护可持久化并查集,支持在某个版本连边,回到某个版本,在某个版本 询问连通性. 思路: 我们用主席树维护并查集fafafa数组,由于要查询历史版本,因此不能够用路径压缩. 可以考虑另外一 ...
- bzoj3673 bzoj3674可持久化并查集
并查集都写不来了qwq 之前写的是错的 sz的初值都是0,这样怎么加就都是0了,水这道题还是可以,但是加强版就过不了了 #include<cstdio> #include<cstri ...
- [BZOJ3674]可持久化并查集加强版&[BZOJ3673]可持久化并查集 by zky
思路: 用主席树维护并查集森林,每次连接时新增结点. 似乎并不需要启发式合并,我随随便便写了一个就跑到了3674第一页?3673是这题的弱化版,本来写个暴力就能过,现在借用加强版的代码(去掉异或),直 ...
- BZOJ3674 可持久化并查集加强版 可持久化 并查集
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3674 题意概括 n个集合 m个操作操作:1 a b 合并a,b所在集合2 k 回到第k次操作之后的 ...
- 【可持久化数组】【rope】bzoj3673 bzoj3674 可持久化并查集 by zky
rope教程:http://blog.csdn.net/iamzky/article/details/38348653 Code(bzoj3673): #include<cstdio> # ...
- BZOJ 3674: 可持久化并查集模板
Code: #include <cstdio> #include <algorithm> #include <cstring> #include <strin ...
随机推荐
- CF 558A(Lala Land and Apple Trees-暴力)
A. Lala Land and Apple Trees time limit per test 1 second memory limit per test 256 megabytes input ...
- 使用Hadoop ACL 控制訪问权限
使用Hadoop ACL 控制訪问权限 一.HDFS訪问控制 hdfs-site.xml设置启动acl <property> <name>dfs.permissions.en ...
- 源码编译安装Nginx全程视频演示
基本步骤: 1.首先停止现有web系统, #/etc/init.d/apache2 stop 2.将源码拷贝到/usr/local/src #cp /home/ditatompel/Public/Ng ...
- codeforces 710C Magic Odd Square(构造或者n阶幻方)
Find an n × n matrix with different numbers from 1 to n2, so the sum in each row, column and both ma ...
- 关于echarts3版本里的tree图形显示Bug、无法缩放和移动
在使用echarts3版本的js绘制tree图表的时候,如果想动态更新tree的数据,可能会出现图表渲染有异常,并且api给出的roam配置无法控制图表通过鼠标缩放和移动,如下图: 不过更改echar ...
- BZOJ1367: [Baltic2004]sequence(左偏树)
Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sample Output 13 解题思路: 有趣的数学题. 首先确定序 ...
- 【搭建Saltstack运维工具】
目录 所谓Salt 开始搭建 配置接受密钥 salt命令 YAML详解 目标定位字符串 state模块定义主机状态 Salt采集静态信息之GrainsSalt @(Saltstack) *** 所谓S ...
- 【Henu ACM Round#17 E】Tree Construction
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 做这题之前先要知道二叉排序树的一个性质. 就是它的中序遍历的结果就是这个数组升序排序. (且每个节点的左边的节点都是比这个节点的值小 ...
- LoadRunner使用教程
1.了解Loadrunner 1.1 LoadRunner 组件有哪些? LoadRunner 包含下列组件: ➤ 虚拟用户生成器用于捕获最终用户业务流程和创建自动性能测试脚本(也称为虚拟用户脚本). ...
- CentOS-6.4-minimal版中安装JDK_Maven_Subversion以及改动rpm包安装路径
完整版见https://jadyer.github.io/2013/09/07/centos-config-develop/ /** * @see -------------------------- ...