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 ...
随机推荐
- json数据转化成实体 存到数据库.
直接看步骤吧 1.一般我们会调用别人给的webservice获取一个字符串数据.如果为String data="xxxxxxxxxx"; 这个data事实上就是样例Enterpr ...
- HMACSHA256 Class
https://msdn.microsoft.com/en-us/library/system.security.cryptography.hmacsha256(v=vs.110).aspx Comp ...
- SQL调用Webservices
IF NOT object_id('proc_CallWebServices') IS NULL DROP PROCEDURE proc_CallWebServices GO CREATE PROCE ...
- PHP写文件到指定位置
<?php $fp = fopen("output.json", "r+"); $flag = fseek($fp, -3, SEEK_END); if( ...
- Announcing Zuul: Edge Service in the Cloud--转
原文地址:http://techblog.netflix.com/2013/06/announcing-zuul-edge-service-in-cloud.html The Netflix st ...
- MySQL Server 5.5.44免安装版配置详解
转载地址:http://wenku.baidu.com/view/2a8bfe6a25c52cc58bd6beff.html### 一 下载MySQL http://dev.mysql.com/dow ...
- Network Stack
Network Stack 目录 1 Overview 2 Code Layout 3 Anatomy of a Network Request (focused on HTTP) 3.1 URLRe ...
- css line-height详解
行高指的是文本行的基线间的距离(更简单来说,行高是指文字尺寸与行距之间的和). 而基线(Base line),指的是一行字横排时下沿的基础线, 基线并不是汉字的下端沿,而是英文字母x的下端沿,同时还有 ...
- Linux发行版centos, ubuntu等
公司装的是centos,centos其实就是无支持版的redhat. redhat是一个服务器的操作系统它的稳定性是比较高的,同时提供在线管理服务,服务器故障预警等,当然前提是要购买昂贵的服务. Su ...
- eclipse中编码的设置
Windows-------->prefenerce------>General-------->Workspace