传送门

Description

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-N的数,一开始每个数自形成一个集合,要求支持以下操作

  • 将元素 p 所在集合与元素 q 所在集合合并
  • 将元素 p 移到元素 q 所在集合
  • 询问元素 p 所在集合有多少个元素,和为多少

题解:

第一和第三个操作都是很裸的并查集,关键在于操作2,很容易可以想到,元素 p 移到元素 q所在集合,可以先在元素 p 所在集合将其删除,然后把元素 p 与元素 q 所在集合合并。而这一步的关键在于删除操作,如果 p 是叶子节点,直接改变 p 的父亲指向就行,但是 p 即所在集合的根节点又当如何,将其删除的话其子节点需要重新建立关系,这个过程还是有一定复杂性的。我们可以采取另外的思路:二次哈希法(ReHash),对于每个结点都有一个哈希值,在进行查找之前需要将x转化成它的哈希值HASH[x],那么在进行删除的时候,只要将x的哈希值进行改变,变成一个从来没有出现过的值(可以采用一个计数器来实现这一步),然后对新的值建立集合,因为只有它一个元素,所以必定是一个新的集合。这样做可以保证每次删除操作的时间复杂度都是O(1)的,而且不会破坏原有树结构,唯一的一个缺点就是每删除一个结点其实是多申请了一块内存,如果删除操作无限制,那么内存会无限增长。

简单的说,就是建立虚拟节点,这样原来的节点还在树当中,并不会破坏其结构,开辟一个id[ ]数组表示元素 x 对应的值。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int cnt,fa[maxn],num[maxn],sum[maxn],id[maxn];

void init(int N)
{
	for (int i = 0;i <= N;i++)
	{
		fa[i] = id[i] = sum[i] = i;
		num[i] = 1;
	}
	cnt = N;
}

int find(int x)
{
	int r = x;
	while (r != fa[r])	r = fa[r];
	int i = x,j;
	while (i != r)
	{
		j = fa[i];
		fa[i] = r;
		i = j;
	}
	return r;
}

void Union(int x,int y)
{
	int fx = find(id[x]),fy = find(id[y]);
	fa[fx] = fy;
	num[fy] += num[fx];
	sum[fy] += sum[fx];
}

void Delete(int x)
{
	int fx = find(id[x]);
	--num[fx];
	sum[fx] -= x;
	id[x] = ++cnt,fa[id[x]] = id[x],num[id[x]] = 1,sum[id[x]] = x;
}

int main()
{
	int N,M;
	while (~scanf("%d%d",&N,&M))
	{
		int opt,x,y;
		init(N);
		while (M--)
		{
			scanf("%d",&opt);
			if (opt == 1)
			{
				scanf("%d%d",&x,&y);
				if (find(id[x]) != find(id[y]))	Union(x,y);
			}
			else if (opt == 2)
			{
				scanf("%d%d",&x,&y);
				if (find(id[x]) != find(id[y]))	Delete(x),Union(x,y);
			}
			else
			{
				scanf("%d",&x);
				printf("%d %d\n",num[find(id[x])],sum[find(id[x])]);
			}
		}
	}
	return 0;
}

  

UVa 11987 Almost Union-Find(支持删除操作的并查集)的更多相关文章

  1. UVa 10129 Play On Words【欧拉道路 并查集 】

    题意:给出n个单词,问这n个单词能否首尾接龙,即能否构成欧拉道路 按照紫书上的思路:用并查集来做,取每一个单词的第一个字母,和最后一个字母进行并查集的操作 但这道题目是欧拉道路(下面摘自http:// ...

  2. UVA - 11987 Almost Union-Find(带删除的并查集)

    I hope you know the beautiful Union-Find structure. In this problem, you’re to implement something s ...

  3. 【uva 247】Calling Circles(图论--Floyd 传递闭包+并查集 连通分量)

    题意:有N个人互相打了M次电话,请找出所有电话圈(Eg.a→b,b→c,c→d,d→a 就算一个电话圈)并输出.(N≤25,L≤25,注意输出格式) 解法:由于N比较小所有n^2或n^3的复杂度都没有 ...

  4. 洛谷UVA11987Almost Union-Find题解--并查集的删除

    题目链接 https://www.luogu.org/problemnew/show/UVA11987 分析 分析下操作发现就是加了个删除操作的并查集,怎么做删除操作呢. 我们用一个\(id[]\)记 ...

  5. 并查集(删除) UVA 11987 Almost Union-Find

    题目传送门 题意:训练指南P246 分析:主要是第二种操作难办,并查集如何支持删除操作?很巧妙的方法:将并查集树上p的影响消除,即在祖先上(sz--, sum -= p),然后为p换上马甲:id[p] ...

  6. hdu4496并查集的删边操作

    题意:       给你一个图,问你删除一些边后还有几个连通快.. 思路:       典型的并查集删边操作,并查集的删边就是先把不删除的边并查集一边(本题没有不删除的边),然后逆序吧所有要删除的边以 ...

  7. POJ 1611 The Suspects 并查集 Union Find

    本题也是个标准的并查集题解. 操作完并查集之后,就是要找和0节点在同一个集合的元素有多少. 注意这个操作,须要先找到0的父母节点.然后查找有多少个节点的额父母节点和0的父母节点同样. 这个时候须要对每 ...

  8. UVA - 11987 Almost Union-Find[并查集 删除]

    UVA - 11987 Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, y ...

  9. UVA - 11987 Almost Union-Find 并查集的删除

    Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you're to imp ...

随机推荐

  1. Replication-Replication Distribution Subsystem: agent xxxxxx failed. Column names in each table must be unique

    最近遇到一个关于发布订阅(Replication)的奇葩问题,特此记录一下这个案例.我们一SQL SERVER数据库服务器出现大量告警.告警信息如下所示: DESCRIPTION: Replicati ...

  2. .NET/ASP.NET MVC(模块化开发AraeRegistration)

    阅读目录: 1.开篇介绍 2.AreaRegistration注册路由(传递路由上下文进行模块化注册) 1]开篇介绍 ASP.NET Routing 路由功能非常强大,设计的也很巧妙:如果说ASP.N ...

  3. 烂泥:haproxy与nginx、zabbix集成

    本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb. 昨天介绍了haproxy的手机匹配规则,今天再来介绍下haproxy与nginx.za ...

  4. 理解 Nova 架构 - 每天5分钟玩转 OpenStack(23)

    Compute Service Nova 是 OpenStack 最核心的服务,负责维护和管理云环境的计算资源. OpenStack 作为 IaaS 的云操作系统,虚拟机生命周期管理也就是通过 Nov ...

  5. win7+ubuntu双系统 重装win7后grub修复

    问题:之前安装的是win7+ubuntu双系统,重装win7后启动选项没有了,直接进入的是win7系统,无法进入ubuntu系统了. 解决办法:我们需要修复grub,将ubuntu系统重新挂载一下,具 ...

  6. [WPF系列]-TreeView的常用事项

    引言 项目经常会用Treeview来组织一些具有层级结构的数据,本节就将项目使用Treeview常见的问题作一个总结. DataBinding数据绑定 DataTemplate自定义 <Hier ...

  7. RS-232 vs. TTL Serial Communication(转载)

    RS-232串口一度像现在的USB接口一样,是PC的标准接口,用来连接打印机.Modem和其他一些外设.后来逐渐被USB接口所取代,现在PC上已经看不到它的身影了.开发调试时如果用到串口,一般都是用U ...

  8. 破解Java to C# Converter

    起因 最近在对接一个第三方平台.该平台只提供了Java版本的SDK,C#版本的还处于敬请期待状态.由于C#可以复用绝大部分代码,便考虑找一个Java到C#的转换器,在试用了几个软件之后,发现还是Jav ...

  9. Python的with用法理解

    Python的with理解 标签(空格分隔): Python with做为一种上下文管理器,在Python中的作用可以简单的理解为是用来代替try...except...finally的处理流程. w ...

  10. Spring源码分析——资源访问利器Resource之实现类分析

    今天来分析Spring的资源接口Resource的各个实现类.关于它的接口和抽象类,参见上一篇博文——Spring源码分析——资源访问利器Resource之接口和抽象类分析 一.文件系统资源 File ...