Ultraviolet-11987-Almost Union-Find (and query set again ~)
UV-11987
Almost Union-Find
Time Limit:1000 MS |
|
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 trying 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 5 MB.
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}, {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!
Source
Root: aoapc I: Beginning Algorithm Contests -- Training Guide (Rujia Liu): Chapter 3. Data Structures: Fundamental Data Structures: Exercises: Intermediate
Root: Prominent Problemsetters: Rujia Liu
Root: Rujia Liu's Presents: Present 3: A Data Structure Contest
Idea: 1, 3 is better to implement, 2 is a little troublesome, 2 is the delete operation of the query set, you need to set up another set of real [] arrays to determine the actual address of the element, each time an element is deleted, it is placed at the end .. I thought it would be okay to direct p to the q root, but I found this was wrong. If p is a leaf node, yes, but if p is the root of a set. We just want to remove this element. If we direct p to the root of q, the leaf nodes will also pass... If another set of real arrays is set, the impact can be reduced to 0 ..
AC code:
# Include
# Include
# Include # define LL long longusing namespace std; const int maxn = 200005; int pa [maxn], real [maxn], cnt [maxn]; int n, m, vnum; LL sum [maxn]; int find (int x) {return pa [x]! = X? Pa [x] = find (pa [x]): x;} void Union (int a, int B) {int a1 = find (real [a]), b1 = find (real [B]); pa [a1] = b1; sum [b1] + = sum [a1]; cnt [b1] + = cnt [a1];} void Move (int a) {int t = find (real [a]); sum [t]-= a, cnt [t] --; real [a] = ++ vnum; // and check the set here to n, so you must first ++, before the samples of ++ have not passed, check for half-day ==|| sum [real [a] = a, cnt [real [a] = 1, pa [real [a] = real [a];} int main () {while (scanf ("% d", & n, & m )! = EOF) {for (int I = 0; I <= n; I ++) {pa [I] = real [I] = sum [I] = I; cnt [I] = 1;} vnum = n; int ord, p, q; while (m --) {scanf ("% d", & ord ); if (ord = 1) {scanf ("% d", & p, & q); if (find (real [p])! = Find (real [q]) Union (p, q);} else if (ord = 2) {scanf ("% d", & p, & q); if (find (real [p])! = Find (real [q]) Move (p), Union (p, q);} else if (ord = 3) {scanf ("% d ", & p); int tmp = find (real [p]); printf ("% d % lld \ n", cnt [tmp], sum [tmp]);} return 0 ;}