Almost Union-find
| Time Limit: 1000MS |
|
Memory Limit: Unknown |
|
64bit IO Format: %lld &%llu |
Submit Status
Description
Problem Aalmost Union-find
I hope you know the beautiful union-find structure. In this problem, you ' re-implement something similar, but not identical.
The data structure 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 is already in the same set, ignore this command.
0 0 3
Move p to the set containing Q. If p and Q is already in the same set, ignore this command
5 6
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 is several test cases. Each test case begins with a line containing the 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: {$}, {3}, {4}, {5}
Collection after Operation 2 3 4: {--}, {3,4}, {5} (we omit the empty set that was produced when taking out 3 from {3})
Collection after Operation 1 3 5: {}, {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!
With the deleted and check set.
Action 2 actually removes p from the original collection and adds it to the collection where Q is located.
And check set does not delete the operation, so change the idea, let P point in its original set of influence of 0, in the opening up a new node as the number p node, join Q set.
Use Id[i]=ii to represent the current position of the number I, each time remove the id[i] point to other locations, the original location.
#include <iostream> #include <cstring> #include <cstdio>using namespace std;const int maxn = 200000+100 ; int Parent[maxn];int cnt[maxn];int sum[maxn];int N, m;int dex;int id[maxn];void make_set () {dex = n;for (int i = 0; i < = N; i++) {Parent[i] = i;cnt[i] = 1;sum[i] = I;id[i] = i;}} int find_set (int t) {if (parent[t] = = t) return T;elsereturn parent[t] = Find_set (Parent[t]);} void Union_set (int a, int b) {int T1 = Find_set (a); int t2 = Find_set (b); if (t1! = t2) {Parent[t2]=t1;cnt[t1] + = cnt[t2];sum[ T1] + = Sum[t2];}} void Move (int a) {int p = find_set (Id[a]); cnt[p]--;sum[p]-= A; The effect of eliminating the point in the original set of the position id[a] = ++dex; Open a new position representing the point of this number parent[dex] = Dex;cnt[dex] = 1;sum[dex] = A;} int main () {while (scanf ("%d%d", &n, &m)! = EOF) {make_set (); int Op;int A, b;while (m--) {scanf ("%d", &op); if (op = = 1) {scanf ("%d%d", &a, &b); Union_set (Id[a], id[b]);} else if (op = = 2) {scanf ("%d%d", &a, &b), int t1 = Find_set (Id[a]), int t2 = Find_set (Id[b]), if (t1! = t2) {Move (a); Remove Union_set (Id[a], id[b]) from the original set; Merge into the new collection}}else{scanf ("%d", &a), int p = find_set (Id[a]);p rintf ("%d%d\n", Cnt[p], sum[p]);}}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA-11987 almost Union-find (with delete and check set)