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中的一种,但是我们并不知道它到底是哪一种. 有人用 ...
随机推荐
- Inter网关做Team的方法
1.到Inter官网上找到相关驱动程序 2.在安装过程中,要求勾选高级网络 3.驱动安装完成后,打开网卡的属性窗口,在“分组”选项卡中,勾中,并输入Team名称 4.选择需要做Team的网卡 5.选择 ...
- input textarea监听鼠标粘贴
发现一个问题,在input/textarea中如果是鼠标粘贴内容进去,发现判断不了value的改变,html代码如下: <!doctype html> <html> <h ...
- BZOJ2476: 战场的数目(矩阵快速幂)
题意 题目链接 Sol 神仙题Orzzz 考虑两边是否有\(1\) 设\(f[i]\)表示周长为\(2i\)的方案数 第一种情况:左侧或右侧有一个1,那么把这个1删去,对应的方案数为\(f[i - 1 ...
- JS实现小图放大轮播效果
JS实现小图放大轮播页面效果入下(图片为优行商旅页面照片): 实现效果:图片自动轮播,鼠标移入停止,移出继续轮播点击下方小图可以实现切换 步骤一:建立HTML布局,具体如下: <body> ...
- void()表达式结果是SyntaxError
void是一元运算符,他出现在操作数之前,操作数可以使任意类型,操作数会照常计算,但忽略计算结果并返回undefined. 因此在操作数具有副作用的时候使用void来让程序根据语义 console.l ...
- Java学习笔记(5)----使用正则表达式解决Google Code Jam Qualification2009赛题 Alien Language
原题地址:https://code.google.com/codejam/contest/90101/dashboard#s=p0 题目描述: Problem After years of study ...
- thinkPHP -01- thinkPHP5.0 安装与测试
thinkPHP -01- thinkPHP5.0 安装与测试 1.thinkPHP 5 官网下载地址:http://www.thinkphp.cn/down.html 2.打开 Wampserver ...
- Week2——XML
一.什么是XML? XML是可扩展标记性语言,类似于HTML,被设计为传输和存储数据,其焦点是数据的内容.XML的标签没有被预定义,用户就需要自行定义标签,也可随意定义标签.XML 允许创作者定义自己 ...
- 听说是个集成版的监控,不知道你听过没? C+C+N
Cnyunwei-Cacti+Nagios 下载地址1:http://pan.baidu.com/s/1mgn1rsc 下载地址2:http://sourceforge.net/projects/cn ...
- ActiveMQ5.8.0安装及启动
一.下载 官网地址:http://activemq.apache.org/download.html Windows版本:apache-activemq-5.8.0-bin.zip Linux版本:a ...