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所在集 ...
随机推荐
- 51nod 1682 中位数计数(前缀和)
51nod 1682 中位数计数 思路: sum[i]表示到i为止的前缀和(比a[i]小的记为-1,相等的记为0,比a[i]大的记为1,然后求这些-1,0,1的前缀和): hash[sum[i]+N] ...
- Linux上安装Oracle的教程-centos7
一.安装Oracle前准备 1.创建运行oracle数据库的系统用户和用户组 [humf@localhost ~]$ su root #切换到root Password: [root@lo ...
- Java 常用对象-基本类型的封装类
2017-11-04 20:39:26 基本类型封装类:基本类型的封装类的好处是可以在对象中定义更多的功能方法操作该数据. 常用操作之一:用于基本数据类型与字符串的转换. 基本类型和包装类的对应: b ...
- Netty优雅退出机制和原理
1.进程的优雅退出 1.1.Kill -9 PID带来的问题 在Linux上通常会通过kill -9 pid的方式强制将某个进程杀掉,这种方式简单高效,因此很多程序的停止脚本经常会选择使用kill - ...
- PyQt5-GUI生成随机生成小工具
自己修改了代码:实现了自动生成SSN,手机号和姓名的功能 import sys from PyQt5.QtGui import * from PyQt5.QtWidgets import * from ...
- English trip V1 - 1.How Do You Feel Now? Teacher:Lamb Key:形容词(Adjectives)
In this lesson you will learn to describe people, things, and feelings.在本课中,您将学习如何描述人,事和感受. STARTER ...
- 5-15 devise(git指南,部分认为重要的内容的译文)
git指南:https://github.com/plataformatec/devise 维基百科上有how to 的很多文章. Stackoverflow有大量问题的答案. 全的文档devise: ...
- python-day43--多表查询
一.多表连接查询: #重点:外链接语法 准备表 #建表 create table department( id int, name varchar(20) ); create table ...
- OC 构造方法(对象初始化)
一.构造方法 (一)构造方法的调用 完整的创建一个可用的对象:Person *p=[Person new]; New方法的内部会分别调用两个方法来完成2件事情,1)使用alloc方法来分配存储空间(返 ...
- OC Block(代码块)
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...