没什么可说的,就是一个可持久化线段树维护一个数组fa以及deep按秩合并好了

注意一下强制在线

蒟蒻的我搞了好长时间QAQ

贴代码:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. struct trnt{
  5. int ls;
  6. int rs;
  7. int fa;
  8. int dp;
  9. }tr[];
  10. int root[];
  11. int siz;
  12. int n,m;
  13. int lastans;
  14. void Tr_build(int l,int r,int &spc)
  15. {
  16. if(!spc)
  17. spc=++siz;
  18. if(l==r)
  19. {
  20. tr[spc].fa=l;
  21. return ;
  22. }
  23. int mid=(l+r)/;
  24. Tr_build(l,mid,tr[spc].ls);
  25. Tr_build(mid+,r,tr[spc].rs);
  26. return ;
  27. }
  28. int ask(int l,int r,int pos,int spc)
  29. {
  30. if(l==r)
  31. return spc;
  32. int mid=(l+r)/;
  33. if(pos<=mid)
  34. return ask(l,mid,pos,tr[spc].ls);
  35. return ask(mid+,r,pos,tr[spc].rs);
  36. }
  37. int finf(int rt,int x)
  38. {
  39. int ff=ask(,n,x,rt);
  40. if(tr[ff].fa==x)
  41. return ff;
  42. return finf(rt,tr[ff].fa);
  43. }
  44. void unin(int l,int r,int &spc,int last,int pos,int ff)
  45. {
  46. spc=++siz;
  47. if(l==r)
  48. {
  49. tr[spc].fa=ff;
  50. tr[spc].dp=tr[last].dp;
  51. return ;
  52. }
  53. tr[spc].ls=tr[last].ls;
  54. tr[spc].rs=tr[last].rs;
  55. int mid=(l+r)/;
  56. if(pos<=mid)
  57. unin(l,mid,tr[spc].ls,tr[last].ls,pos,ff);
  58. else
  59. unin(mid+,r,tr[spc].rs,tr[last].rs,pos,ff);
  60. return ;
  61. }
  62. void grow(int l,int r,int pos,int spc)
  63. {
  64. if(l==r)
  65. {
  66. tr[spc].dp++;
  67. return ;
  68. }
  69. int mid=(l+r)/;
  70. if(pos<=mid)
  71. grow(l,mid,pos,tr[spc].ls);
  72. else
  73. grow(mid+,r,pos,tr[spc].rs);
  74. return ;
  75. }
  76. int main()
  77. {
  78. scanf("%d%d",&n,&m);
  79. Tr_build(,n,root[]);
  80. for(int i=;i<=m;i++)
  81. {
  82. root[i]=root[i-];
  83. int cmd;
  84. scanf("%d",&cmd);
  85. if(cmd==)
  86. {
  87. int x,y;
  88. scanf("%d%d",&x,&y);
  89. x=x^lastans;
  90. y=y^lastans;
  91. x=finf(root[i],x);
  92. y=finf(root[i],y);
  93. if(tr[x].fa==tr[y].fa)
  94. continue;
  95. if(tr[x].dp>tr[y].dp)
  96. std::swap(x,y);
  97. unin(,n,root[i],root[i-],tr[x].fa,tr[y].fa);
  98. if(tr[x].dp==tr[y].dp)
  99. grow(,n,tr[y].fa,root[i]);
  100. }else if(cmd==)
  101. {
  102. int x;
  103. scanf("%d",&x);
  104. x=x^lastans;
  105. root[i]=root[x];
  106. }else{
  107. int x,y;
  108. scanf("%d%d",&x,&y);
  109. x=x^lastans;
  110. y=y^lastans;
  111. x=finf(root[i],x);
  112. y=finf(root[i],y);
  113. if(tr[x].fa==tr[y].fa)
  114. lastans=;
  115. else
  116. lastans=;
  117. printf("%d\n",lastans);
  118. }
  119. }
  120. return ;
  121. }

BZOJ3674可持久化并查集(模板)的更多相关文章

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

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

  2. bzoj3674 可持久化并查集

    我是萌萌的任意门 可持久化并查集的模板题-- 做法好像很多,可以标号法,可以森林法. 本来有O(mloglogn)的神算法(按秩合并+倍增),然而我这种鶸渣就只会写O(mlog2n)的民科算法--再加 ...

  3. BZOJ3674: 可持久化并查集加强版

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3674 题解:主要是可持久化的思想.膜拜了一下hzwer的代码后懂了. 其实本质是可持久化fa数 ...

  4. 2019.01.21 bzoj3674: 可持久化并查集加强版(主席树+并查集)

    传送门 题意:维护可持久化并查集,支持在某个版本连边,回到某个版本,在某个版本 询问连通性. 思路: 我们用主席树维护并查集fafafa数组,由于要查询历史版本,因此不能够用路径压缩. 可以考虑另外一 ...

  5. bzoj3673 bzoj3674可持久化并查集

    并查集都写不来了qwq 之前写的是错的 sz的初值都是0,这样怎么加就都是0了,水这道题还是可以,但是加强版就过不了了 #include<cstdio> #include<cstri ...

  6. [BZOJ3674]可持久化并查集加强版&[BZOJ3673]可持久化并查集 by zky

    思路: 用主席树维护并查集森林,每次连接时新增结点. 似乎并不需要启发式合并,我随随便便写了一个就跑到了3674第一页?3673是这题的弱化版,本来写个暴力就能过,现在借用加强版的代码(去掉异或),直 ...

  7. BZOJ3674 可持久化并查集加强版 可持久化 并查集

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ3674 题意概括 n个集合 m个操作操作:1 a b 合并a,b所在集合2 k 回到第k次操作之后的 ...

  8. 【可持久化数组】【rope】bzoj3673 bzoj3674 可持久化并查集 by zky

    rope教程:http://blog.csdn.net/iamzky/article/details/38348653 Code(bzoj3673): #include<cstdio> # ...

  9. BZOJ 3674: 可持久化并查集模板

    Code: #include <cstdio> #include <algorithm> #include <cstring> #include <strin ...

随机推荐

  1. 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 ...

  2. 使用Hadoop ACL 控制訪问权限

    使用Hadoop ACL 控制訪问权限 一.HDFS訪问控制 hdfs-site.xml设置启动acl <property>  <name>dfs.permissions.en ...

  3. 源码编译安装Nginx全程视频演示

    基本步骤: 1.首先停止现有web系统, #/etc/init.d/apache2 stop 2.将源码拷贝到/usr/local/src #cp /home/ditatompel/Public/Ng ...

  4. 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 ...

  5. 关于echarts3版本里的tree图形显示Bug、无法缩放和移动

    在使用echarts3版本的js绘制tree图表的时候,如果想动态更新tree的数据,可能会出现图表渲染有异常,并且api给出的roam配置无法控制图表通过鼠标缩放和移动,如下图: 不过更改echar ...

  6. BZOJ1367: [Baltic2004]sequence(左偏树)

    Description Input Output 一个整数R Sample Input 7 9 4 8 20 14 15 18 Sample Output 13 解题思路: 有趣的数学题. 首先确定序 ...

  7. 【搭建Saltstack运维工具】

    目录 所谓Salt 开始搭建 配置接受密钥 salt命令 YAML详解 目标定位字符串 state模块定义主机状态 Salt采集静态信息之GrainsSalt @(Saltstack) *** 所谓S ...

  8. 【Henu ACM Round#17 E】Tree Construction

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 做这题之前先要知道二叉排序树的一个性质. 就是它的中序遍历的结果就是这个数组升序排序. (且每个节点的左边的节点都是比这个节点的值小 ...

  9. LoadRunner使用教程

    1.了解Loadrunner 1.1 LoadRunner 组件有哪些? LoadRunner 包含下列组件: ➤ 虚拟用户生成器用于捕获最终用户业务流程和创建自动性能测试脚本(也称为虚拟用户脚本). ...

  10. CentOS-6.4-minimal版中安装JDK_Maven_Subversion以及改动rpm包安装路径

    完整版见https://jadyer.github.io/2013/09/07/centos-config-develop/ /** * @see -------------------------- ...