UVA 11987 Almost Union-Find (并查集)
来源:互联网 发布:java compare返回值 编辑:程序博客网 时间:2024/05/17 22:32
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). The size of input file does not exceed 5MB.
Output
For each type-3 command, output 2 integers: the number of elements and the sum of elements.
Sample Input
5 71 1 22 3 41 3 53 42 4 13 43 3
Output for the Sample Input
3 123 72 8题意:给定n个城市,m种操作。接下来m行输入m组操作
1为连接城市a,b。2为把a拿出来(即a的所有连接都断掉),再连接a,b。3为输出该点的连接中共有几点和这些点的和。
思路:并查集的使用。。这题多了个删除所以麻烦点。我是用2个数组parent和father来查找父节点。
然后操作1时直接连接没什么好说的。。操作2的时候。利用另一个数组father来把该点连接到另一片图中。
这样最后在输出的时候。只要查找每个点的father的根节点的情况就可以了。。。
#include <stdio.h>#include <string.h>int n, m;int sb;int x, y;int dian;int parent[100005];int father[100005];int sum[100005];int num[100005];int find(int x){ if (parent[x] != x)return parent[x] = find(parent[x]); elsereturn x;}int main(){ while (scanf("%d%d", &n, &m) != EOF) {int i;for (i = 1; i <= n ; i ++){ parent[i] = i; father[i] = i; num[i] = 1; sum[i] = i;}for (i = 0; i < m ; i++){scanf("%d", &sb);if (sb == 1){scanf("%d%d", &x, &y);if (father[x] == father[y])continue;int pa = find(father[x]);int pb = find(father[y]);if (pa == pb)continue;num[pb] += num[pa];sum[pb] += sum[pa];parent[pa] = pb;}else if (sb == 2){scanf("%d%d", &x, &y);if (father[x] == father[y])continue;int pa = find(father[x]);int pb = find(father[y]);if (pa == pb)continue;father[x] = father[y];num[pa] --;num[pb] ++;sum[pa] -= x;sum[pb] += x;}else if (sb == 3){scanf("%d", &dian);printf("%d %d\n", num[find(father[dian])], sum[find(father[dian])]);}} } return 0;}
- uva 11987 Almost Union-Find(加权并查集)
- UVA 11987 Almost Union-Find(并查集)
- uva 11987 Almost Union-Find(并查集)
- UVA 11987 Almost Union-Find (并查集)
- UVA 11987 Almost Union-Find(并查集)
- UVa:11987 Almost Union-Find(并查集)
- uva 11987 Almost Union-Find (并查集)
- UVA 11987 Almost Union-Find(并查集)
- UVA 11987 Almost Union-Find(hash + 并查集)
- UVA 11987 Almost Union-Find (并查集+删边)
- uva 11987 - Almost Union-Find(并查集)
- UVa 11987,Almost Union-Find,并查集
- UVA 11987 Almost Union-Find 并查集节点删除
- UVA 11987 - Almost Union-Find(并查集)
- UVA - 11987 Almost Union-Find 并查集
- uva 11987 Almost Union-Find (并查集)
- UVA 11987 Almost Union-Find (并查集)
- Almost Union-Find(Uva 11987)并查集
- hdu-4585-Shaolin 平衡二叉树
- Linux中关机命令详解
- NGINX LUA 边下载边发送文件内容
- HDU 1014 Uniform Generator
- HDU 4584
- UVA 11987 Almost Union-Find (并查集)
- 随笔1(QAction与状态栏添加)
- HDU 4585
- 时间的计算类CaculateDate
- poj 2112 (最大流+二分)
- 两分钟彻底让你明白Android Activity生命周期(图文)!
- C++学习过程6
- hdu 4631(增量最近点对)
- Android全屏,在任意地方,不必在onCreate()时初始化全屏