Problem G
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:
-
Union the sets containing
and . If and are already in the same set, ignore this command. -
Move
to the set containing . If and are already in the same set, ignore this command -
Return the number of elements and the sum of elements in the set containing
.
Initially, the collection contains
As an example, consider the sequence of operations in sample input 1 below.
-
Initially:
-
Collection after operation 1 1 2:
-
Collection after operation 2 3 4:
(we omit the empty set that is produced when taking out from ) -
Collection after operation 1 3 5:
-
Collection after operation 2 4 1:
Input
There are several test cases. Each test case begins with a
line containing two integers
Output
For each type-
Sample Input 1 | Sample Output 1 |
---|---|
5 7 1 1 2 2 3 4 1 3 5 3 4 2 4 1 3 4 3 3 |
3 12 3 7 2 8 |