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中的一种,但是我们并不知道它到底是哪一种. 有人用 ...
随机推荐
- 在弹框中获取foreach中遍历的id值,并传递给地址栏。
1.php有时候我们需要再弹框中获取foreach中遍历的数据(例如id),在弹框中点击按钮并传递给地址栏跳转.那么应该怎么做呢. 2. 点击取现按钮,如果没有设置密码->弹框 3. 点击去设置 ...
- Code Signal_练习题_palindromeRearranging
Given a string, find out if its characters can be rearranged to form a palindrome. Example For input ...
- 事件处理程序DOM0,DOM2,IE的区别总结
一.事件流 顺序 备注 事件冒泡 目标对象~document对象 事件捕获 document对象~目标对象 老版本浏览器不支持 DOM事件流 document对象~目标对象~document对 ...
- JavaWeb学习总结(八):HttpServletRequest对象
一.HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象 ...
- gulp 打包报错:Error: `libsass` bindings not found. Try reinstalling `node-sass`
看了网上很多帖子 有说切换node版本的 有说卸载重新装gulp-sass的 有说删除node_modules重新install的 但是我测试了下在我们的电脑环境下都不行,后来找到一个可以打包不报错的 ...
- 02--CSS的继承性和层叠性
一 继承性 css有两大特性:继承性和层叠性 面向对象语言都会存在继承的概念,在面向对象语言中,继承的特点:继承了父类的属性和方法.那么我们现在主要研究css,css就是在设置属性的.不会牵扯到方法的 ...
- Google APAC----Africa 2010, Qualification Round(Problem A. Store Credit)----Perl 解法
原题地址链接:https://code.google.com/codejam/contest/351101/dashboard#s=p0 问题描述: Problem You receive a cre ...
- iphone崩溃日志分析工具symbolicatecrash用法
Symbolicatecrash是Xcode自带的一个分析工具,可以通过机器上的崩溃日志和应用的.dSYM文件定位发生崩溃的位置,把crash日志中的地址替换成代码相应位置. 使用效果: 分析前: T ...
- Two references point to the same heap memory
Phone类 package com.itheima_03; /* * 手机类 */ public class Phone { String brand; int price; String colo ...
- php判断网站收录情况
php判断网站收录情况 <?php //检测网页是否被百度收录 function checkBaiduIndex($url){ $url='http://www.baidu.com/s?wd=' ...