一定注意每一次都要是 $root[cur]=root[cur-1]$,不然进行合并时如果 $a,b$ 在同一集合中就会使 $root[cur]=0$.

Code:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
//ty==1 for size, ty==0 for father void setIO(string a){ freopen((a+".in").c_str(),"r",stdin); } #define maxn 200005
int n,m,cur,root[maxn*4];
struct Node{
int f,siz;
Node(int f=0,int siz=0):f(f),siz(siz){}
};
struct Segment_Tree{
int lson[maxn*50],rson[maxn*50],fa[maxn*50],siz[maxn*50];
int nodes;
void build(int l,int r,int &o){
if(l>r)return;
o=++nodes;
if(l==r) { siz[o]=1,fa[o]=l; return; }
int mid=(l+r)>>1;
build(l,mid,lson[o]), build(mid+1,r,rson[o]);
}
int update(int l,int r,int o,int pos,int ty,int k){
int oo=++nodes;
lson[oo]=lson[o],rson[oo]=rson[o],fa[oo]=fa[o],siz[oo]=siz[o];
if(l==r) {
if(ty==1) siz[oo]=k;
if(ty==0) fa[oo]=k;
return oo;
}
int mid=(l+r)>>1;
if(pos<=mid) lson[oo]=update(l,mid,lson[o],pos,ty,k);
else rson[oo]=update(mid+1,r,rson[o],pos,ty,k);
return oo;
}
Node query(int l,int r,int o,int pos){
if(l==r){ return Node(fa[o],siz[o]); }
int mid=(l+r)>>1;
if(pos<=mid) return query(l,mid,lson[o],pos);
else return query(mid+1,r,rson[o],pos);
}
Node find(int x,int state){
Node p=query(1,n,root[state],x);
return p.f==x?p:find(p.f,state);
}
void merge(int a,int b,int state){
Node x=find(a,state), y=find(b,state);
if(x.f==y.f) return;
if(x.siz>y.siz)
root[cur]=update(1,n,root[state],x.f,1,y.siz+x.siz),root[cur]=update(1,n,root[cur],y.f,0,x.f);
else
root[cur]=update(1,n,root[state],y.f,1,y.siz+x.siz),root[cur]=update(1,n,root[cur],x.f,0,y.f);
}
int ask(int a,int b,int state){
Node x=find(a,state),y=find(b,state);
if(x.f==y.f)return 1; return 0;
}
}S;
int main(){
// setIO("input");
int opt,a,b,lastans=0;
scanf("%d%d",&n,&m);
S.build(1,n,root[0]);
for(cur=1;cur<=m;++cur){
scanf("%d",&opt);
root[cur]=root[cur-1];
switch(opt)
{
case 1: { scanf("%d%d",&a,&b),a^=lastans,b^=lastans,S.merge(a,b,cur-1); break;}
case 2: { scanf("%d",&a),a^=lastans,root[cur]=root[a]; break;}
case 3: { scanf("%d%d",&a,&b),a^=lastans,b^=lastans,lastans=S.ask(a,b,cur-1),printf("%d\n",lastans); break;}
}
}
return 0;
}

  

洛谷P3402 【模板】可持久化并查集的更多相关文章

  1. BZOJ3673 & BZOJ3674 & 洛谷3402:可持久化并查集——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=3673 https://www.lydsy.com/JudgeOnline/problem.php? ...

  2. 【模板】【P3402】可持久化并查集

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

  3. Bzoj1015/洛谷P1197 [JSOI2008]星球大战(并查集)

    题面 Bzoj 洛谷 题解 考虑离线做法,逆序处理,一个一个星球的加入.用并查集维护一下连通性就好了. 具体来说,先将被消灭的星球储存下来,先将没有被消灭的星球用并查集并在一起,这样做可以路径压缩,然 ...

  4. 洛谷1525 关押罪犯NOIP2010 并查集

    问题描述 S城现有两座监狱,一共关押着N名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”(一个正整数值)来表示某两 ...

  5. 洛谷P1525 关押罪犯(并查集、二分图判定)

    本人蒟蒻,只能靠题解AC,看到大佬们的解题思路,%%%%%% https://www.luogu.org/problemnew/show/P1525 题目描述 S城现有两座监狱,一共关押着N名罪犯,编 ...

  6. 洛谷 P2661 信息传递 Label:并查集||强联通分量

    题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知道自己的生日.之后每一 ...

  7. 洛谷 P1111 修复公路 Label:并查集

    题目背景 A地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车.政府派人修复这些公路. 题目描述 给出A地区的村庄数N,和公路数M,公路是双向的.并告诉你每条公路的连着哪两个村庄,并告诉你什么时 ...

  8. 洛谷P4092 [HEOI2016/TJOI2016]树 并查集/树链剖分+线段树

    正解:并查集/树链剖分+线段树 解题报告: 传送门 感觉并查集的那个方法挺妙的,,,刚好又要复习下树剖了,所以就写个题解好了QwQ 首先说下并查集的方法趴QwQ 首先离线,读入所有操作,然后dfs遍历 ...

  9. 洛谷P1955 程序自动分析 [NOI2015] 并查集

    正解:并查集+离散化 解题报告: 传送门! 其实题目还挺水的,,,但我太傻逼了直接想挂了,,,所以感觉还是有个小坑点所以还是写个题解记录下我的傻逼QAQ 首先这题一看,就长得很像NOIp关押罪犯?然后 ...

  10. 洛谷P3295 萌萌哒 [SCOI2016] 倍增+并查集

    正解:倍增+并查集 解题报告: 传送门! 首先不难想到暴力?就考虑把区间相等转化成对应点对相等,然后直接对应点连边,最后求有几个连通块就好辣 然后看下复杂度,修改是O(n2)查询是O(n),就比较容易 ...

随机推荐

  1. zzulioj--1712--神秘的数列(水题)

    1712: 神密的数列 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 122  Solved: 92 SubmitStatusWeb Board De ...

  2. Linux就该这么学 20181005(第八章防火墙)

    参考链接https://www.linuxprobe.com/ vim /etc/sysconfig/network-scripts/ifcfg-ens32 网络配置0 nmtui 网络配置1 nm- ...

  3. xBIM 基础05 3D墙案例

    系列目录    [已更新最新开发文章,点击查看详细]  使用编码的形式去生成一堵墙的模型需要做很多的工作. using System; using System.Collections.Generic ...

  4. JavaScript学习记录三

    title: JavaScript学习记录三 toc: true date: 2018-09-14 23:51:22 --<JavaScript高级程序设计(第2版)>学习笔记 要多查阅M ...

  5. 创建ios界面的三步骤

    1.加载数据 (包括懒加载和字典转模型等) 2.搭建界面 (常见的有九宫格算法和for循环的嵌套等) 3.实现用户交互 (通常用按钮实现)

  6. java算法数据结构

    原文地址:github.com/kdn251/interviews 译文出自:掘金翻译计划 译者:王下邀月熊 校对者:PhxNirvana.根号三 这个 链接 用来查看本翻译与英文版是否有差别(如果你 ...

  7. Ubuntu 16.04下安装64位谷歌Chromium(Chrome)浏览器

    在命令行下输入: sudo add-apt-repository ppa:a-v-shkop/chromium sudo apt-get update sudo apt-get install chr ...

  8. HDU 1285 确定比赛名次【拓扑排序】

    题意:中文的题目-----这道题让我终于明白了那个break的作用---因为题目中有这一句“符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前”@_@ #include<iostrea ...

  9. css3实现轮播图

    css3动画属性简写: animation: name  duration  timing-function  delay  iteration-count  direction  fill-mode ...

  10. Mojo C++ Bindings API

    This document is a subset of the Mojo documentation. Contents Overview Getting Started Interfaces Ba ...