bzoj3673 bzoj3674可持久化并查集
并查集都写不来了qwq
之前写的是错的
sz的初值都是0,这样怎么加就都是0了,水这道题还是可以,但是加强版就过不了了
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream> using namespace std; template<typename Q> Q &read(Q &x) {
static char c, f;
for(f = ; c = getchar(), !isdigit(c); ) if(c == '-') f = ;
for(x = ; isdigit(c); c = getchar()) x = x * + c - '';
if(f) x = -x; return x;
}
template<typename Q> Q read() {
static Q x; read(x); return x;
} const int N = + ; struct Node *newnode(int, Node *, Node *); struct Node {
int v;
Node *ch[];
int query(int l, int r, int k) {
if(l == r) return v;
int mid = (l + r) >> ;
if(k <= mid) return ch[]->query(l, mid, k);
else return ch[]->query(mid + , r, k);
}
Node *modify(int l, int r, int k, int w) {
if(l == r) return newnode(w, , );
int mid = (l + r) >> ;
if(k <= mid) return newnode(, ch[]->modify(l, mid, k, w), ch[]);
return newnode(, ch[], ch[]->modify(mid + , r, k, w));
}
}pool[N * ], *pis = pool; Node *newnode(int v, Node *lc, Node *rc) {
pis->v = v, pis->ch[] = lc, pis->ch[] = rc;
return pis++;
} int n, now; struct parr {
Node *root[N];
int vt;
void init(int w) {
root[] = newnode(w, pis, pis);
}
int get(int k, int h) const {
return root[h]->query(, n, k);
}
void modify(int k, int w) {
root[vt] = root[vt]->modify(, n, k, w);
}
void newver(int h) {
root[++vt] = root[h];
}
} sz, fa; int find(int x) {
int y;
while(x)
y = x, x = fa.get(x, now);
return y;
} void unite(int x, int y) {
sz.newver(now), fa.newver(now);
x = find(x), y = find(y);
if(x == y) return;
int sx = sz.get(x, now), sy = sz.get(y, now);
if(sx < sy) swap(x, y), swap(sx, sy);
fa.modify(y, x);
sz.modify(x, sx + sy);
now = sz.vt;
} int version[N];
int main() {
#ifdef DEBUG
freopen("in.txt", "r", stdin);
#endif sz.init(), fa.init();
int m, ans = ; read(n), read(m);
for(int i = ; i <= m; i++) {
int opt = read<int>();
if(opt == ) {
int u, v; read(u) ^= ans, read(v) ^= ans;
unite(u, v);
} else if(opt == ) {
int h; read(h) ^= ans;
now = version[h];
} else if(opt == ) {
int u, v; read(u) ^= ans, read(v) ^= ans;
printf("%d\n", ans = find(u) == find(v));
}
version[i] = now;
} return ;
}
bzoj3673 bzoj3674可持久化并查集的更多相关文章
- 【可持久化数组】【rope】bzoj3673 bzoj3674 可持久化并查集 by zky
rope教程:http://blog.csdn.net/iamzky/article/details/38348653 Code(bzoj3673): #include<cstdio> # ...
- 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数组,由于要查询历史版本,因此不能够用路径压缩. 可以考虑另外一 ...
- [BZOJ3674]可持久化并查集加强版&[BZOJ3673]可持久化并查集 by zky
思路: 用主席树维护并查集森林,每次连接时新增结点. 似乎并不需要启发式合并,我随随便便写了一个就跑到了3674第一页?3673是这题的弱化版,本来写个暴力就能过,现在借用加强版的代码(去掉异或),直 ...
- [BZOJ3673&3674]可持久化并查集&加强版
题目大意:让你实现一个可持久化的并查集(3674强制在线). 解题思路:刚刚介绍了一个叫rope的神器:我是刘邦,在这两题(实际上两题没什么区别)就派上用场了. 正解应该是主席树||可持久化平衡树,然 ...
- BZOJ3673/3674:可持久化并查集
Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...
- [bzoj3673/3674可持久化并查集加强版]
n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0<n,m<=2 ...
随机推荐
- MySQL全文检索笔记 转载
1. MySQL 4.x版本及以上版本提供了全文检索支持,但是表的存储引擎类型必须为MyISAM,以下是建表SQL,注意其中显式设置了存储引擎类型 CREATE TABLE articles ( id ...
- URL 路由访问报错
错误: 错误分析: 控制器的文件名命名有问题(index.php) 在TP中控制器命名规范(IndexController.class.php) 相信许多PHP开发者在使用ThinkPHP ...
- hibernate映射
三种方式: 持久化注解 目前开发主流方式 XML配置描述文件(XML deployment descriptor,可以让Hibernate的PO类与JPA实体类兼容,实际中很少用) ...
- ecshop标签
页面标题 {$page_title}页面关键字 {$keywords} 产品分类 父分类列表 {foreach from=$cate ...
- #Leet Code# Same Tree
语言:Python 描述:使用递归实现 # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # ...
- 如何删除github上的项目
如果大伙用过github,那么就会知道它上面没有明显的选项可以让你直接删除掉一个Repositories或者项目.因此对很多github的新手来说一开始也常觉得这个事情比较犯难.这里我给大家简单介绍一 ...
- highcharts-Highmaps 动态传入城市名称
做前端按地区(地图)分布监控数据展示用了 HIGHMAPS JAVASCRIPT MAPS 控件,很好很强大. 基础实现是这样的:调用插件动态传入需要展示的数据(data),插件会在地图数据(mapd ...
- 10055 - Hashmat the Brave Warrior
Problem A Hashmat the brave warrior Input: standard input Output: standard output Hashmat is a brave ...
- qt5使用curl实现文件下载的示例程序 good
http://blog.csdn.net/xueyushenzhou/article/details/51702672#t3 http://download.csdn.net/detail/xueyu ...
- 使用HttpServletRequestWrapper在filter修改request参数
javax.servlet.ServletRequest中的 Map<String, String[]> parameterMap = request.getParameterMap(); ...