UVA - 11987 Almost Union-Find(带删除的并查集)
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
题目意思:
初始时,一共有n个元素的组合。
给出三个操作:(k p q)
k==1 p q:合并p, q所在的集合
k==2 p q:把p移动到q所在的集合
k==3 p:输出p所在的集合的元素的个数和元素之和
分析:
所以就需要将删除的这个点的影响降到0,
也就是给删除的点申请一个新的id,
以后都是用这个新的id来表示这个点,
这样原来的那个集合里的点p就没意义,自然影响就为0.
#include <iostream>
#include<algorithm>
#include <cstdio>
#include<cstring>
#include<math.h>
#include<memory>
using namespace std;
typedef long long LL;
#define max_v 200005
int pa[max_v];
int sum[max_v];//sum[x] x(根结点)所在元素集合所有元素的和
int id[max_v];//id[x]=y 表示x结点现在的编号为y
int cnt[max_v];//cnt[x] x(根结点)所在集合元素的个数 int n,m,tot; void make_set(int x)
{
pa[x]=sum[x]=id[x]=x;
cnt[x]=;
}
int find_set(int x)
{
if(x!=pa[x])
pa[x]=find_set(pa[x]);
return pa[x];
}
void union_set(int x,int y)//x合并到y
{
x=find_set(x);
y=find_set(y);
if(x==y)
return ; pa[x]=y;
sum[y]+=sum[x];
cnt[y]+=cnt[x]; }
void del(int x)
{
int fx=find_set(id[x]);
sum[fx]-=x;
cnt[fx]--; tot++;
id[x]=tot;
pa[tot]=tot;
sum[tot]=x;
cnt[tot]=;
}
int main()
{
int n,m,k;
int x,y;
while(~scanf("%d %d",&n,&m))
{
tot=n;
for(int i=;i<=n;i++)
make_set(i);
for(int i=;i<m;i++)
{
scanf("%d",&k);
if(k==)//x合并到y
{
scanf("%d %d",&x,&y);
union_set(id[x],id[y]);
}else if(k==)//x从原有集合拿出放入到y
{
scanf("%d %d",&x,&y);
int fx=find_set(id[x]);
int fy=find_set(id[y]); if(fx!=fy)
{
del(x);
union_set(id[x],id[y]);
}
}else//输出x所在集合元素个数和所有元素和
{
scanf("%d",&x);
int fx=find_set(id[x]);
printf("%d %d\n",cnt[fx],sum[fx]);
}
}
}
return ;
}
UVA - 11987 Almost Union-Find(带删除的并查集)的更多相关文章
- 【uva11987】带删除的并查集
题意:初始有N个集合,分别为 1 ,2 ,3 .....n.有三种操件1 p q 合并元素p和q的集合2 p q 把p元素移到q集合中3 p 输出p元素集合的个数及全部元素的和. 题解: 并查集.只是 ...
- UVA 11987 Almost Union-Find (单点修改的并查集)
此题最难处理的操作就是将一个单点改变集合,而普通的并查集是不支持这种操作的. 当结点p是叶子结点的时候,直接pa[p] = root(q)是可以的, p没有子结点,这个操作对其它结点不会造成任何影响, ...
- [BZOJ 4025]二分图(线段树分治+带边权并查集)
[BZOJ 4025]二分图(线段树分治+带边权并查集) 题面 给出一个n个点m条边的图,每条边会在时间s到t出现,问每个时间的图是否为一个二分图 \(n,m,\max(t_i) \leq 10^5\ ...
- Codevs 3287 货车运输 2013年NOIP全国联赛提高组(带权LCA+并查集+最大生成树)
3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description A 国有 n 座 ...
- [洛谷P2024/POJ1182]食物链 - 带偏移量的并查集(2)
Description 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A 吃 B,B吃 C,C 吃 A. 现有 N 个动物,以 1 - N 编号.每个动物都是 A,B,C 中的 ...
- [洛谷P1196][NOI2002]银河英雄传说 - 带偏移量的并查集(1)
Description 公元五八〇一年,地球居民迁至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发 ...
- bzoj 1202 [HNOI2005]狡猾的商人——带偏移量的并查集
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1202 带偏移量的并查集. 注意先 find() 再调用 dis !!! 自己的对拍太水了. ...
- bzoj 3376 [Usaco2004 Open]Cube Stacking 方块游戏——带偏移量的并查集
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3376 带偏移量的并查集. #include<iostream> #include ...
- AcWing:240. 食物链(扩展域并查集 or 带边权并查集)
动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形. A吃B, B吃C,C吃A. 现有N个动物,以1-N编号. 每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用 ...
随机推荐
- C#获取AD域中计算机和用户的信息
如果你的计算机加入了某个AD域,则可以获取该域中所有的计算机和用户的信息. 所用程序集,需要.Net Framework 4. 添加程序集引用 System.DirectoryServices.Acc ...
- python中logging日志基本用法,和进程安全问题
低配版 import logging logging.debug('debug message') # 调试模式 logging.info('info message') # 正常运转模式 loggi ...
- 关于 PHPMailer 邮件发送类的使用心得(含多文件上传)
This is important for send mail PHPMailer 核心文件 class.phpmailer.php class.phpmaileroauth.php class.ph ...
- HTML 5 教程
HTML5 是下一代的 HTML.有必要再过一遍.看下要点. 具体看 http://www.w3school.com.cn/html5/index.asp 教程 和 HTML5 标签参考手册 ...
- Android Editable
在android的sdk中有讲,“This is the interface for text whose content and markup can be changed (as opposed ...
- INFA Transformation组件
RouterRouter和Filter很相似,Router可以用一或多个Filter来取代,不同的是用Router来生成多个组时输入数据只需处理一次,所以效率更高:Router 由一个输入组,一到多个 ...
- ASP.NET错误处理的方式(一)
对Web应用程序来说,发生不可预知的错误和异常在所难免,我们必须为Web程序提供错误处理机制.当错误发生时,我们必须做好两件事情:一是将错误信息记录日志,发邮件通知网站维护人员,方便技术人员对错误进行 ...
- 2 Docker 镜像基础
Docker 镜像可以从docker.io 下载,也可以自己通过Dockerfile来构建镜像,我有时从国外下载镜像时,网速不行,我就改成国内的镜像,修改如下: # vim /etc/docker/d ...
- C# 冒泡排序法、插入排序法、选择排序法
冒泡排序法 是数组等线性排列的数字从大到小或从小到大排序. 以从小到大排序为例. 数据 11, 35, 39, 30, 7, 36, 22, 13, 1, 38, 26, 18, 12, 5, 45, ...
- 乘风破浪:LeetCode真题_014_Longest Common Prefix
乘风破浪:LeetCode真题_014_Longest Common Prefix 一.前言 如何输出最长的共同前缀呢,在给定的字符串中,我们可以通过笨办法去遍历,直到其中某一个字符不相等了,这样就得 ...