UVA - 11987 Almost Union-Find(带删除的并查集)
来源:互联网 发布:淘宝代销可以刷信誉吗 编辑:程序博客网 时间:2024/05/22 11:56
Description
Problem A
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
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}
Rujia Liu's Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yiming Li
Note: Please make sure to test your program with the gift I/O files before submitting!
带删除的并查集,用id[i]=ii代表数字i当前的位置,每次删除把id[i]指向其他位置,原来的位置舍去。
#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int MAXN = 2e5 + 5;int F[MAXN], cnt[MAXN], id[MAXN], sum[MAXN];int dex;int n, k;void init(){for (int i = 0; i <= n; i++){F[i] = id[i] = sum[i] = i;cnt[i] = 1;}dex = n;}int find(int t){if (F[t] == t) return t;return F[t] = find(F[t]);}void Union(int a, int b){int p = find(a);int q = find(b);if (p != q){F[p] = q;cnt[q] += cnt[p];sum[q] += sum[p];}}void move(int a){int p = find(id[a]);cnt[p]--;sum[p] -= a;id[a] = ++dex;F[dex] = dex;cnt[dex] = 1;sum[dex] = a;}int main(){while (scanf("%d%d", &n, &k) != EOF){init();int op,a,b;while (k--){cin >> op;if (op == 1){cin >> a >> b;Union(id[a], id[b]);}else if (op == 2){cin >> a >> b;int p = find(id[a]);int q = find(id[b]);if (p != q){move(a);Union(id[a], id[b]);}}else if (op == 3){cin >> a;int p = find(id[a]);cout << cnt[p] << " " << sum[p] << endl;}}}}
- 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 并查集节点删除
- 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(并查集)
- 拼音排序
- 11、缓存在AFNetworking中是如何工作的?AFImageCache和NSUrlCache给你答案
- 开发日志 2015-03-27
- Ubuntu与Windows双系统修改默认启动项
- HttpClient使用详解
- UVA - 11987 Almost Union-Find(带删除的并查集)
- Sublime Text 2 入门
- Ruby on rails ActiveRecord ‘or’方式选择数据
- 交叉验证
- ISODATA算法
- find trace file name
- LeetCode 75 Sort Colors
- 活到老学到老就测试一下吧
- 60. permutation sequence leetcode python