Almost Union-Find 并查集(脱离原来的树)
h: 0px; "> I hope you know the beautiful Union-Find structure. In this problem, you’re to implement something
similar, but not identical.
The data structure you need to write is also a collection of disjoint sets, supporting 3 operations:
1 p q Union the sets containing p and q. If p and q are already in the same set,
ignore this command.
2 p q Move p to the set containing q. If p and q are already in the same set,
ignore this command.
3 p Return the number of elements and the sum of elements in the set containing p.
Initially, the collection contains n sets: {1}, {2}, {3}, ..., {n}.
Input
There are several test cases. Each test case begins with a line containing two integers n and m
(1 ≤ n, m ≤ 100,000), the number of integers, and the number of commands. Each of the next m lines
contains a command. For every operation, 1 ≤ p, q ≤ n. The input is terminated by end-of-file (EOF).
Output
For each type-3 command, output 2 integers: the number of elements and the sum of elements.
Explanation
Initially: {1}, {2}, {3}, {4}, {5}
Collection after operation 1 1 2: {1,2}, {3}, {4}, {5}
Collection after operation 2 3 4: {1,2}, {3,4}, {5} (we omit the empty set that is produced when
taking out 3 from {3})
Collection after operation 1 3 5: {1,2}, {3,4,5}
Collection after operation 2 4 1: {1,2,4}, {3,5}
Sample Input
5 7
1 1 2
2 3 4
1 3 5
3 4
2 4 1
3 4
3 3
Sample Output
3 12
3 7
2 8
真是写残了,脑子都不转了,还好终于写过了,这道题是用到并查集,1,3比较好实现,就是2
不太好实现,要把p加入q就得让p跟原来的树撇清关系,因为p可能是根节点,这样会让p整个集合归入q不合题意,所以需要一个数组这里用newid每次将p move掉然后给p一个新的id重新初始化一切有关的,
还有就是fa数组保存的并不是父节点,要找父节点还是要用getf函数去找
代码:
#include <iostream> using namespace std; long long sum[];
int count[],fa[],newid[];
int n,m;
void init()
{
for(int i=;i<=n;i++)
fa[i]=i,count[i]=,sum[i]=(long long)i,newid[i]=i;
}
int getf(int x)
{
if(fa[x]!=x)fa[x]=getf(fa[x]);
return fa[x];
}
void merge(int x,int y)
{
int xx=getf(newid[x]),yy=getf(newid[y]);
fa[yy]=xx;
sum[xx]+=sum[yy];
count[xx]+=count[yy];
}
void move(int x)
{
int xx=getf(newid[x]);
count[xx]--;
sum[xx]-=(long long)x;
newid[x]=++n;//
fa[n]=n;//
count[n]=;///注意新指向的id父亲变成自己,成员和是x,这里就要求传入的参数就是输入的p本身,数量是1
sum[n]=(long long)x;///此四行代表给x赋一个新的id以后他就通过这个id来完成各指令 这样原来的x就被切断了 不会保证他的儿子随着x合并到新的群体
}
int main()
{
int op,p,q;
while(cin>>n>>m){
init();
while(m--)
{
cin>>op;
if(op==)
{
cin>>p;
//cout<<newid[p]<<endl;
cout<<count[getf(newid[p])]<<' '<<sum[getf(newid[p])]<<endl;///用getf
}
else
{
cin>>p>>q;
if(getf(newid[p])==getf(newid[q]))continue;///这也是
if(op==)merge(p,q);
else
{
move(p);
merge(q,p);
}
}
}
}
}
Almost Union-Find 并查集(脱离原来的树)的更多相关文章
- 【bzoj4399】魔法少女LJJ 并查集+权值线段树合并
题目描述 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了LJJ感叹道“这里真是个迷人的绿色世界,空气清新.淡雅,到处散发着醉人的奶浆味: ...
- poj2513--并查集+欧拉路+字典树
经典好题,自己不知道哪里错了交上去是RE,可能是数组开的不好吧,字典树老碰到这种问题.. 先马上别人的代码,有空对拍看看 #include <cstdio> #include <cs ...
- 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ...
- 2019.01.21 bzoj3674: 可持久化并查集加强版(主席树+并查集)
传送门 题意:维护可持久化并查集,支持在某个版本连边,回到某个版本,在某个版本 询问连通性. 思路: 我们用主席树维护并查集fafafa数组,由于要查询历史版本,因此不能够用路径压缩. 可以考虑另外一 ...
- 【并查集】BZOJ4551-[Tjoi2016&Heoi2016]树
NOIP太可怕了((( -口-) 题目链接 [题目大意] 给定一颗有根树(根为1),有以下两种操作: 1. 标记操作:对某个结点打上标记(在最开始,只有结点1有标记,其他结点均无标记,而且对于某个结点 ...
- E - Is It A Tree? 并查集判断是否为树
题目链接:https://vjudge.net/contest/271361#problem/E 具体思路:运用并查集,每一次连接上一个点,更新他的父亲节点,如果父亲节点相同,则构不成树,因为入读是2 ...
- BZOJ3673 & BZOJ3674 可持续化并查集 【可持续化线段树维护可持续化数组】
题目描述 n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0 输入格式 输出 ...
- BZOJ 3674 可持久化并查集加强版(主席树变形)
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MB Submit: 2515 Solved: 1107 [Submit][Sta ...
- bzoj 2733: [HNOI2012]永无乡【并查集+权值线段树】
bzoj上数组开大会T-- 本来想用set瞎搞的,想了想发现不行 总之就是并查集,每个点开一个动态开点的权值线段树,然后合并的时候把值并在根上,询问的时候找出在根的线段树里找出k小值,看看这个值属于哪 ...
- Bzoj 3673: 可持久化并查集 by zky(主席树+启发式合并)
3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MB Description n个集合 m个操作 操作: 1 a b 合并a,b所在集 ...
随机推荐
- 开关灯问题 BulbSwitch
2018-06-17 11:54:51 开关电灯问题是一个比较经典的趣味数学题,本文中主要介绍其中的一些常见情况. 一.Bulb Switch 问题描述: 问题求解: 初始状态:off, off, o ...
- Sqlite3,维基百科中的练习:
https://en.wikibooks.org/wiki/SQL_Exercises/The_computer_store 两个相连的表格 Manufactures: code, name prod ...
- [INS-20802] Oracle Net Configuration Assistant failed,Caught UnknownHostException
在64位Centos上安装64的oracle 11g R2,出现错误: [INS-20802] Oracle Net Configuration Assistant failed 根据提示查看日志文件 ...
- 基于DOMContentLoaded实现文档加载完成后执行的方法
我们有时可能需要一些在页面加载完成之后执行的方法,其实js原生就提供了onload方法,所以我们最简单的办法就是直接给onload赋值一个函数,在页面加载完成之后就会自动执行 widnow.onloa ...
- Axel and Marston in Bitland CodeForces - 782F (bitset优化)
题目链接 $dp[0/1][i][x][y]$表示起始边为0/1, 走$2^i$ 步, 是否能从$x$走到$y$ 则有转移方程 $dp[z][i][x][y]\mid=dp[z][i-1][x][k] ...
- 『Re』知识工程作业_主体识别
作业要求 环境路径 类似于这样的,一共50篇文档, 均为中文文档,是法院判决书的合集. 程序 程序如下,我完全使用正则表达式来实现功能, import re import glob import co ...
- YOLO v2 损失函数源码分析
损失函数的定义是在region_layer.c文件中,关于region层使用的参数在cfg文件的最后一个section中定义. 首先来看一看region_layer 都定义了那些属性值: layer ...
- uva-10561-nim
题意: 给出一个连续的棋盘,有的位置为'.',有的位置为'X',二者轮流下子,当有一方获得连续三个子的时候取胜. 对于胜态,一种情况是当前局面出现"XX"/"X.X&qu ...
- quartz---(1)
Quartz Quartz是什么? 简单的一些例子 Quartz是什么 简单的认为Quartz是是一个定时器. 1.1. 下载 http://www.quartz-scheduler.org/down ...
- 微信access_token全局缓存,处理过期
//PHP创建access_token.json文件,将access_token 和 生成时间expires 保存在其中, //{"access_token":"xxxx ...