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. POJ 2752 KMP中next数组的应用

    题意: 让你从小到大输出给的字符串中既是前缀又是后缀的子串的长度. 思路: 先要了解这个东西: KMP中next数组表示的含义:记录着字符串匹配过程中失配情况下可以向前多跳几个字符,它描述的也是子串的 ...

  2. (转)vue router 如何使用params query传参,以及有什么区别

    写在前面: 传参是前端经常需要用的一个操作,很多场景都会需要用到上个页面的参数,本文将会详细介绍vue router 是如何进行传参的,以及一些小细节问题.有需要的朋友可以做一下参考,喜欢的可以点波赞 ...

  3. VM虚拟机NAT模式主机与虚拟机ping不通解决方案

    VM虚拟机与真机通信三种模式, 桥接模式,NAT 模式 ,HOST-ONLY 模式. NAT模式 使用虚拟机的一个虚拟网卡做NAT网关,在nat网关上配dhcp ,或者直接用静态地址.就相当于形成了一 ...

  4. Qt5—嵌入停靠窗口QDockWidget

    参考链接:http://blog.csdn.net/summer_xiyer/article/details/12875899 新建一个GUI工程: QDockWidget是QWidget的子类,也等 ...

  5. Find Bugs

    为什么没有早点知道有这么好用的插件呢?

  6. Laravel 查询某天数据 whereDate

  7. Ubuntu终端命令行缩短显示路径

    平时我们使用linux终端命令行的时候,常常会被一个问题困扰,那就是文件路径过长, 有时候甚至超过了一行,这样看起来非常别扭,其实只要两步就可以解决这个问题: 1,修改.bashrc文件(用户根目录下 ...

  8. Redmine 甘特图导出 PDF 和 PNG 中文乱码问题

    Redmine使用了RMagick来处理图片,fpdf处理PDF,并在调用时设定了字体PDF中文字体 redmine 中关于PDF字体设置的代码 case pdf_encoding           ...

  9. 写shell工具类,一个常用实例

    简述: 当我们常用到某些指令时,我们就需要将这个命令进行封装.封装的设计和扩展,因人而异.但为了每个人都能够了解到这个命令,常需要写出这个类的help. 关键字: 函数.getopts 函数 通过自定 ...

  10. jmeter目录讲解

    1.jmeter目录详解 2.这是bin目录下的examples 3.这是bin目录下的report-template 注意啦:jmeterw.cmd运行不带命令的窗口会侵占8080端口,会和tomc ...