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 并查集单点修改的更多相关文章

  1. UVA 11987 Almost Union-Find (并查集+删边)

    开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 操作有三种: 1 xx1 yy1 : 合并xx1与yy1两个集合 2 xx1 yy1 :将xx1元素分离出来合到yy ...

  2. UVA 572 油田连通块-并查集解决

    题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...

  3. 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 ...

  4. UVA 1160 - X-Plosives 即LA3644 并查集判断是否存在环

    X-Plosives A secret service developed a new kind ofexplosive that attain its volatile property only ...

  5. UVa 1455 Kingdom 线段树 并查集

    题意: 平面上有\(n\)个点,有一种操作和一种查询: \(road \, A \, B\):在\(a\),\(b\)两点之间加一条边 \(line C\):询问直线\(y=C\)经过的连通分量的个数 ...

  6. uva 1493 - Draw a Mess(并查集)

    题目链接:uva 1493 - Draw a Mess 题目大意:给定一个矩形范围,有四种上色方式,后面上色回将前面的颜色覆盖,最后问9种颜色各占多少的区域. 解题思路:用并查集维护每一个位置相应下一 ...

  7. UVA - 1160(简单建模+并查集)

    A secret service developed a new kind of explosive that attain its volatile property only when a spe ...

  8. UVA 1493 Draw a Mess(并查集+set)

    这题我一直觉得使用了set这个大杀器就可以很快的过了,但是网上居然有更好的解法,orz... 题意:给你一个最大200行50000列的墙,初始化上面没有颜色,接着在上面可能涂四种类型的形状(填充):  ...

  9. Mobile Phone Network CodeForces - 1023F(并查集lca+修改环)

    题意: 就是有几个点,你掌控了几条路,你的商业对手也掌控了几条路,然后你想让游客都把你的所有路都走完,那么你就有钱了,但你又想挣的钱最多,真是的过分..哈哈 游客肯定要对比一下你的对手的路 看看那个便 ...

随机推荐

  1. 使用 `ConfigMap` 挂载配置文件

    使用 ConfigMap 挂载配置文件 Intro 有一些敏感信息比如数据库连接字符串之类的出于安全考虑,这些敏感信息保存在了 Azure KeyVault 中,最近应用上了 k8s 部署,所以想把 ...

  2. A - Antipalindrome

    Problem description A string is a palindrome if it reads the same from the left to the right and fro ...

  3. 决策树构建算法之—C4.5

    这个网站值得收藏一下,原文链接:http://shiyanjun.cn/archives/428.html 决策树算法的优越性在于:离散学习算法进行组合总可以表达任意复杂的布尔函数,并不受数据集的限制 ...

  4. 几个方便编程的C++特性

    前言: C++11的自动化特性给编程提供了不少方便,同时也给调试增加了很多负担,至于取舍看程序员的风格和侧重而定. auto:自动类型推断 在C++11之前,auto关键字用来指定存储期.在新标准中, ...

  5. MVC传值前台

    ViewBag.model = bLL.GetModel((int)id); ViewBag.RecruitmentTime = ViewBag.model.RecruitmentTime.ToStr ...

  6. js脚本捕获页面 GET 方式请求的参数?其实直接使用 window.location.search 获得

    js脚本捕获页面 GET 方式请求的参数?其实直接使用 window.location.search 获得

  7. SpringMVC源码阅读

    在研究SpringMVC工作流程的同时记录下过程,以便以后浏览. 版本号:5.0.4 前沿:我们在使用SpringMVC的时候会在web.xml中配置以下servlet <!-- 配置sprin ...

  8. C++基础 (1) 第一天 C++相对C的改进 命名空间 引用

    第一天 语法 STL 数据结构  设计模式… 2 C++语言的间接 C++ = C语言+面向对象 本贾尼 语言分类: 不关心效率 只关心架构:java/脚本语言 效率:(内存要自己管理了,操作指针)C ...

  9. WEBGL学习【十五】利用WEBGL实现三维场景的一般思路总结

    实现三维场景载入操作的实现步骤: 主要知识点:着色器,纹理贴图,文件载入 实现思路: 获取canvas,初始化WEBGL上下文信息. 主要是实现WEBGL上下文的获取,设置视的大小,此时gl存储了WE ...

  10. HDU2149 - Public Sale【巴什博弈】

    虽然不想,但是现实总归是现实,Lele始终没有逃过退学的命运,因为他没有拿到奖学金.现在等待他的,就是像FarmJohn一样的农田生涯.  要种田得有田才行,Lele听说街上正在举行一场别开生面的拍卖 ...