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(带删除的并查集)的更多相关文章

  1. 【uva11987】带删除的并查集

    题意:初始有N个集合,分别为 1 ,2 ,3 .....n.有三种操件1 p q 合并元素p和q的集合2 p q 把p元素移到q集合中3 p 输出p元素集合的个数及全部元素的和. 题解: 并查集.只是 ...

  2. UVA 11987 Almost Union-Find (单点修改的并查集)

    此题最难处理的操作就是将一个单点改变集合,而普通的并查集是不支持这种操作的. 当结点p是叶子结点的时候,直接pa[p] = root(q)是可以的, p没有子结点,这个操作对其它结点不会造成任何影响, ...

  3. [BZOJ 4025]二分图(线段树分治+带边权并查集)

    [BZOJ 4025]二分图(线段树分治+带边权并查集) 题面 给出一个n个点m条边的图,每条边会在时间s到t出现,问每个时间的图是否为一个二分图 \(n,m,\max(t_i) \leq 10^5\ ...

  4. Codevs 3287 货车运输 2013年NOIP全国联赛提高组(带权LCA+并查集+最大生成树)

    3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description A 国有 n 座 ...

  5. [洛谷P2024/POJ1182]食物链 - 带偏移量的并查集(2)

    Description 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A 吃 B,B吃 C,C 吃 A. 现有 N 个动物,以 1 - N 编号.每个动物都是 A,B,C 中的 ...

  6. [洛谷P1196][NOI2002]银河英雄传说 - 带偏移量的并查集(1)

    Description 公元五八〇一年,地球居民迁至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发 ...

  7. bzoj 1202 [HNOI2005]狡猾的商人——带偏移量的并查集

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1202 带偏移量的并查集. 注意先 find() 再调用 dis !!! 自己的对拍太水了. ...

  8. bzoj 3376 [Usaco2004 Open]Cube Stacking 方块游戏——带偏移量的并查集

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3376 带偏移量的并查集. #include<iostream> #include ...

  9. AcWing:240. 食物链(扩展域并查集 or 带边权并查集)

    动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形. A吃B, B吃C,C吃A. 现有N个动物,以1-N编号. 每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用 ...

随机推荐

  1. springboot开篇 (一)简单邮件发送

    上篇终结篇为spring 发送邮件,这次将使用springboot 发送邮件,同时本篇将作为springboot入门篇. 新建一个工程..工程目录结构如下,此次使用idea进行开发.对于一个长期使用e ...

  2. WebService,ASMX文件使用XML格式数据传递参数、验证与获取XML格式返回值的一种方式

    1:首先WebService方法定义,每个方法定义两个参数,一个用于验证权限,string格式的XML文本用于传输数据.最终目的实现,WebService方法,验证权限,获取XML数据,处理之后返回X ...

  3. Tomcat学习总结(一):目录简介

    一:下载地址和目录结构说明. Tomcat官方站点:http://jakarta.apache.org 下载Tomcat安装程序包:http://tomcat.apache.org/

  4. 【转载】MySQL数据库可以用任意ip连接访问的方法

    通过CMD命令行修改数据库表的一个字段的值,实现连接,访问. 第一步.找到MYSQL软件安装所在的bin目录: (1)cd\当前目录 (2)指定MYSQL安装的bin目录 (3)输入 -h local ...

  5. c# 圆上坐标点

    var x=-33204.0924438;  //圆心x var y=-9512.41208658; //圆心y var r=1000;//半径 var angle=30;//角度 var tmpX ...

  6. Windows 10 Framework 3.5 _x64 离线安装包 最新安装版

    原文:http://www.jb51.net/softs/325481.html Windows 10 Framework 3.5 离线安装包,适用于 Win10 和 Server 2016 离线安装 ...

  7. ubuntu16 下安装redis 以及设置其为开机启动

    1.下载redis安装包 sudo wget http://download.redis.io/releases/redis-3.2.6.tar.gz 2.解压 tar -zxvf  redis-3. ...

  8. .PHONY makefile中的伪目标

    我的理解: 拿clean举例,如果make完成后,自己另外定义一个名叫clean的文件,再执行make clean时,将不会执行rm命令. 为了避免出现这个问题,需要.PHONY: clean === ...

  9. 使用JSONP彻底解决Ajax跨域访问Cookie Session的方案

    最近做开发时要把图片文件放到另外一台服务器上(另外一个域名),因为这样分布式存放,网站打开速度会快很多.而我采用AJAX获取图片服务器上某用户的图片时遇到了问题,按照通常的方式无法获取信息,得到的Co ...

  10. VS2017配置cuda9.1编译不过问题。

    #if defined(_WIN32) #if _MSC_VER < 1600 || _MSC_VER > 1920 #error -- unsupported Microsoft Vis ...