Hi!
We are suppose to create a Unionfind like structure to solve the
following question in JAVA:
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 q1 p q
Union the sets containing pp and qq.
If pp and qq are already in the same set,
ignore this command.
2 p q2 p q
Move pp to the set containing qq.
If pp and qq are already in the same set,
ignore this command
3 p3 p
Return the number of elements and the sum of elements in the set
containing pp.
Initially, the collection
contains nn sets: {1},{2},{3},…,{n}{1},{2},{3},…,{n}.
As an example, consider the sequence of operations in sample
input 1 below.
Initially: {1},{2},{3},{4},{5}{1},{2},{3},{4},{5}
Collection after operation 1 1
2: {1,2},{3},{4},{5}{1,2},{3},{4},{5}
Collection after operation 2 3
4: {1,2},{3,4},{5}{1,2},{3,4},{5} (we omit the empty set
that is produced when taking out 33 from {3}{3})
Collection after operation 1 3
5: {1,2},{3,4,5}{1,2},{3,4,5}
Collection after operation 2 4
1: {1,2,4},{3,5}{1,2,4},{3,5}
Input
There are several test cases. Each test case begins with a line
containing two
integers nn and mm (1≤n,m≤1000001≤n,m≤100000),
the number of integers, and the number of commands. Each of the
next mm lines contains a command. For every
operation, 1≤p,q≤n1≤p,q≤n. The input is terminated by
end-of-file (EOF). There are at most 2020 cases, and the
size of the input file does not exceed 55 MB.
Output
For each type-33 command, output 22 integers: the
number of elements and the sum of elements.
How do I do this?
Hi! We are suppose to create a Unionfind like structure to solve the following question in JAVA: Almost Union-Find I hop
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am