UVA 11987 Almost Union-Find 并查集单点修改
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个点m个操作,
有三种操作自己看吧
题解:
来自深渊小鱼的解答
此题最难处理的操作就是将一个单点改变集合,而普通的并查集是不支持这种操作的。
当结点p是叶子结点的时候,直接pa[p] = root(q)是可以的,
p没有子结点,这个操作对其它结点不会造成任何影响,
而当p是父结点的时候这种操作会破坏子节点的路径,因此必须保留原来的路径。
我们希望pa[p] = root(q)的同时又保留原来的路径,那么只需要在点上做一个标记,
如果这个点被标记,就沿着新的路径寻找。
此时在修改操作的时候这个点一定是叶子结点,所以可以直接pa[p] = root(q),
而原来的点则变成一个虚点用来保留了原来的路径。
改变集合的操作以及查询都只涉及到单点,这个标记只影响这个点,在二次以及以上的寻找还是要按照原来的路径。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<vector>
#include<queue>
using namespace std;
const int N = 1e5+, M = , mod = 1e9 + , inf = 0x3f3f3f3f;
typedef long long ll; int fa[N],num[N],sum[N],fg[N],n,m,fa2[N];
int finds(int x,int d) {
if(fg[x]&&d) return finds(fa2[x],);
else return x==fa[x]?x:fa[x]=finds(fa[x],);
}
int main() {
while(scanf("%d%d",&n,&m)!=EOF) {
for(int i=;i<=n;i++) fa[i] = i,num[i] = ,fg[i] = , sum[i] = i;
for(int i=;i<=m;i++) {
int x,a,b,y;
scanf("%d",&x);
if(x==) {
scanf("%d",&y);
printf("%d %d\n",num[finds(y,)], sum[finds(y,)]);
}
else {
scanf("%d%d",&a,&b);
if(x==) {
int fx = finds(a,);
int fy = finds(b,);
if(fx==fy) continue;
fa[fx] = fy;
num[fy] += num[fx];
sum[fy] += sum[fx];
}
else {
int fx = finds(a,);
int fy = finds(b,);
if(fx == fy) continue;
num[fx]--;
sum[fx]-=a;
num[fy]++;
sum[fy]+=a;
fg[a] = ;
fa2[a] = fy;
}
}
}
}
return ;
}
UVA 11987 Almost Union-Find 并查集单点修改的更多相关文章
- UVA 11987 Almost Union-Find (并查集+删边)
开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 操作有三种: 1 xx1 yy1 : 合并xx1与yy1两个集合 2 xx1 yy1 :将xx1元素分离出来合到yy ...
- UVA 572 油田连通块-并查集解决
题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...
- UVA 12232 - Exclusive-OR(带权并查集)
UVA 12232 - Exclusive-OR 题目链接 题意:有n个数字.一開始值都不知道,每次给定一个操作,I a v表示确认a值为v,I a b v,表示确认a^b = v,Q k a1 a2 ...
- UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环
X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...
- UVa 1455 Kingdom 线段树 并查集
题意: 平面上有\(n\)个点,有一种操作和一种查询: \(road \, A \, B\):在\(a\),\(b\)两点之间加一条边 \(line C\):询问直线\(y=C\)经过的连通分量的个数 ...
- uva 1493 - Draw a Mess(并查集)
题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...
- UVA - 1160(简单建模+并查集)
A secret service developed a new kind of explosive that attain its volatile property only when a spe ...
- UVA 1493 Draw a Mess(并查集+set)
这题我一直觉得使用了set这个大杀器就可以很快的过了,但是网上居然有更好的解法,orz... 题意:给你一个最大200行50000列的墙,初始化上面没有颜色,接着在上面可能涂四种类型的形状(填充): ...
- Mobile Phone Network CodeForces - 1023F(并查集lca+修改环)
题意: 就是有几个点,你掌控了几条路,你的商业对手也掌控了几条路,然后你想让游客都把你的所有路都走完,那么你就有钱了,但你又想挣的钱最多,真是的过分..哈哈 游客肯定要对比一下你的对手的路 看看那个便 ...
随机推荐
- win7如何给虚拟机设置共享文件
友情提示:设置之前先把虚拟机关掉 1. 安装vmtools 安装过的,则不需要 重新安装 如果没有安装vmware tools,点击安装(需要联网下载) ,下载完成后,打开虚拟机 点击安装,安装完毕后 ...
- Blender插件之操作器(Operator)实战
前言 在Blender中, 操作器(Operator)是它的核心. 用户通过各种操作器来创建和操作场景中的物体. 操作器对象继承自 class bpy.types.Operator(bpy_struc ...
- Hadoop MapReduce编程 API入门系列之join(二十六)(未完)
不多说,直接上代码. 天气记录数据库 Station ID Timestamp Temperature 气象站数据库 Station ID Station Name 气象站和天气记录合并之后的示意图如 ...
- Unity3d 销毁
using UnityEngine; using System.Collections; public class destroy : MonoBehaviour { void Start () { ...
- 洛谷P1208 [USACO1.3]混合牛奶 Mixing Milk(贪心)
题目描述 由于乳制品产业利润很低,所以降低原材料(牛奶)价格就变得十分重要.帮助Marry乳业找到最优的牛奶采购方案. Marry乳业从一些奶农手中采购牛奶,并且每一位奶农为乳制品加工企业提供的价格是 ...
- 机器学习PAI为你自动写歌词,妈妈再也不用担心我的freestyle了(提供数据、代码)
背景 最近互联网上出现一个热词就是“freestyle”,源于一个比拼rap的综艺节目.在节目中需要大量考验选手的freestyle能力,freestyle指的是rapper即兴的根据一段主题讲一串r ...
- [原创]Java常见笔试题知识点汇总
前天数梦工厂来学校招聘,笔试题比较有特点,全是Java题,基本就是Java的一些特点.凭记忆按照题目找到一些必备知识点 (1). try {}里有一个return语句,那么紧跟在这个try后的fina ...
- Python:Matplotlib 画曲线和柱状图(Code)
原文链接:http://blog.csdn.net/ikerpeng/article/details/20523679 参考资料:http://matplotlib.org/gallery.html ...
- [Intermediate Algorithm] - Sum All Primes
题目 求小于等于给定数值的质数之和. 只有 1 和它本身两个约数的数叫质数.例如,2 是质数,因为它只能被 1 和 2 整除.1 不是质数,因为它只能被自身整除. 给定的数不一定是质数. 提示 For ...
- 02--C编程细节整理(一)
用C语言比较多,这篇是平时攒下的.有些内容在工作后可能会很常见,但是不用容易忘,所以就写篇博客吧. 1. printf的用法 %*可以用来跳过字符,可以用于未知缩进.像下面一样. for ...