题意:求出每个集合的元素个数,及总和,给出三个操作:

1 将含有a元素和b元素的集合合并;2 将a元素放入含有b元素的集合中;3 输出a元素所在集合的元素个数及总和;

思路:正常并查集,与并查集元素的删除

题目:

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 operati

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

/*对于删除操作,在完美的并查集中(所有节点都直接连接在根节点上),理论上只要把要删除的节点的上级重新指向自己就可以了。
但是实际情况中,我们的并查集形成的树的形态都是不可预估形态的,如果直接将一个节点指向自己可能会将他的“下级”和他一起删除,这就和我们的想法违背了。
所以在一个需要删除的并查集中初始化时就要处理一下:
首先可以将每一个点都设立一个虚拟父节点,这样根节点就是我们设立的虚拟节点,类似于将每个节点放到一个盒子中
如果删除某点,那么可以修改当前节点的父节点来导致当前点的孤立,即删除时把这个节点从当前盒子拿出来,放到另一个盒子中。
由于节点之间都是通过盒子来确定关系的,所以盒子中元素是否存在并不影响节点之间的关系。*/
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<queue>
#define Lint long long int
using namespace std;
const int MAXN=200010;
int f[MAXN];/*盒子内的元素链接*/
int sum[MAXN];/*集合内元素之和*/
int p[MAXN];/*盒子*/
int siz[MAXN];/*集合内元素个数,下标为盒子下标*/
int n,m,cnt;
int find(int x)
{return x==f[x] ? f[x] : f[x]=find( f[x] ) ;
}
int main()
{int opt,u,v,x,y;while( scanf("%d%d",&n,&m)!=EOF )//删除节点,就是把原先的节点设置为虚点,然后把点的位置用num数组指向新的位置。{cnt=n;for(int i=1; i<=n; i++)   f[i]=p[i]=sum[i]=i,siz[i]=1;for(int i=1; i<=m; i++){scanf("%d",&opt);if( opt==1 ){scanf("%d%d",&u,&v);u=p[u],v=p[v];u=find( u ),v=find( v );if( u==v )   continue ;f[u]=v;siz[v]+=siz[u],sum[v]+=sum[u];}if( opt==2 ){scanf("%d%d",&u,&v);x=find( p[u] ),y=find( p[v] );if( x==y )   continue ;sum[x]-=u,siz[x]--;/*盒子的名称不变,除去该元素*/x=p[u]=++cnt;/*重新申请一个内存,里面只有要操作的元素,改变该元素的祖先*/f[x]=y;sum[y]+=u,siz[y]++;}if( opt==3 ){scanf("%d",&u);u=find( p[u] );printf("%d %d\n",siz[u],sum[u]);}}}return 0;
}

Almost Union-Find UVA - 11987(并查集的删除操作)相关推荐

  1. HDU 2473 Junk-Mail Filter(并查集的删除操作)

    题目地址:HDU 2473 这题曾经碰到过,没做出来. .如今又做了做,还是没做出来. ... 这题涉及到并查集的删除操作.想到了设一个虚节点,可是我把虚节点设为了要删除的点的父节点.一直是栈溢出,目 ...

  2. Almost Union-Find(Uva 11987)并查集

    来自<算法竞赛入门经典训练指南> 1.题目原文 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemi ...

  3. 树形结构 —— 并查集 —— 并查集的删除操作

    对于删除操作,在完美的并查集中(所有节点都直接连接在根节点上),理论上只要把要删除的节点的上级重新指向自己就可以了. 但是实际情况中,并查集形成的树的形态都是不可预估的,如果直接将一个节点指向自己可能 ...

  4. 并查集的一般操作 ③

    RT 题目描述 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是: 我朋友的朋友是我的朋友: 我敌人的敌人也是我的朋友. 两个强盗是同一 ...

  5. 【CF566#D】 Restructuring Company (并查集---合并区间操作)

    题干: Even the most successful company can go through a crisis period when you have to make a hard dec ...

  6. *【HDU - 2473】Junk-Mail Filter (并查集--删点操作)

    题干: Recognizing junk mails is a tough task. The method used here consists of two steps:  1) Extract ...

  7. BZOJ4358: permu(带撤销并查集 不删除莫队)

    题意 题目链接 Sol 感觉自己已经老的爬不动了.. 想了一会儿,大概用个不删除莫队+带撤销并查集就能搞了吧,\(n \sqrt{n} logn\)应该卡的过去 不过不删除莫队咋写来着?....跑去学 ...

  8. 树形结构 —— 并查集

    [概述] 并查集(Union-Find Set)是一种用于分离集合操作的抽象数据类型,其处理的是集合(set)之间的关系,一般处理的是图的连通分量,当给出两个的元素的一个无序对 (a,b) 时,需要快 ...

  9. hdu 2473(并查集+删除操作)

    解题思路:这道题有并查集的删除操作,如果直接对这一棵树进行删除节点操作肯定是很困难的.所以可以建立虚拟节点,只要有一个节点要被删除,就直接把它投影到虚拟节点上,即用这个虚拟节点来代替我们要删除的节点. ...

最新文章

  1. Redis缓存穿透、击穿、雪崩、预热、更新、降级
  2. top统计mysql性能_mytop安装,使用mytop监控MySQL性能
  3. 形似“飞碟”的无人机你见过吗?它还可以灭火!
  4. 高通LCD的pwm背光驱动
  5. IntelliJ IDEA优秀插件(编程通用)
  6. mysql与php驱动程序_用PHP和MySQL构建一个数据库驱动的网站_php
  7. 如何定时备份远程mysql数据库
  8. linux断点续传程序,Linux下怎么实现断点续传
  9. scratch3转换html5,scratch转换器
  10. ACM算法-逃离机场
  11. C语言通讯录—简单模拟实现
  12. 通信教程 | CAN总线协议基础原理
  13. 《算法导论》:关于循环不变式
  14. Android相机开发: 触摸对焦,触摸测光,二指手势缩放
  15. web js智能识别收货地址
  16. 2021国内十大正规外汇投资理财app平台排行榜
  17. 对计算机接口提出的新要求吗,2017年秋微机原理与接口技术
  18. 未来在线教育的五种模式
  19. 【银行系列第一期】中国人民银行
  20. 快递员转行做站长赚钱变老板

热门文章

  1. 铜线越长发电量越多,6千米长的铜线能让电灯泡发光吗?
  2. 美丽又实用的欧拉螺线,数学界当之无愧的画家!
  3. 在中国,有这样一些村落
  4. 史上最难逻辑题!据说99.9%的人都做不出来……
  5. 这9个人气超高的公众号,你还没关注吗?
  6. 有哪些经济学理论可以用在谈恋爱上?
  7. 对5种主流编程语言的吐槽
  8. 提升方法之AdaBoost算法
  9. 数据挖掘算法之-关联规则挖掘(Association Rule)(购物篮分析)
  10. C语言计算一个数的平方根立方根,怎样快速计算出一个数的平方根立方根?