oneby7

Graphs Union-find

466. Disjoint Set Union

Medium

There are n elements, numbered from 0 to n − 1. At the start, each element is in a set of its own. Write a class DisjointSet that merges these sets and answers questions about them:

  • DisjointSet(n) creates the n one-element sets.
  • unionSets(x, y) merges the set that holds x with the set that holds y. It returns true if they were two different sets, and false if x and y were already in the same set, in which case nothing changes.
  • connected(x, y) returns true if x and y are in the same set, and false otherwise.
  • setSize(x) returns the number of elements in the set that holds x.
  • count() returns the number of sets.

Every element is in exactly one set at any time, so the sets never overlap. Each call should take close to constant time on average.

Your class is tested by replaying a list of calls on a new DisjointSet(n). ops[i] names the i-th call: "unionSets", "connected", "setSize" or "count". args[i] holds its arguments: [x, y] for unionSets and connected, [x] for setSize, and [] for count. The answer is the list of every value the calls return, in order.

In C# and Go the methods are UnionSets, Connected, SetSize and Count, and in Rust union_sets, connected, set_size and count. Go creates the object with Constructor(n), and Rust with DisjointSet::new(n). The names in ops are the same in every language.

Example 1

Input: n = 5 ops = ["unionSets", "unionSets", "connected", "connected", "setSize", "count", "unionSets", "setSize", "count"] args = [[0, 1], [3, 4], [1, 0], [1, 3], [4], [], [1, 4], [3], []]

Output: [true,true,true,false,2,3,true,4,2]

Explanation: The first two merges give the sets {0, 1}, {2} and {3, 4}. So 1 is with 0 but not with 3, the set of 4 has 2 elements, and there are 3 sets. unionSets(1, 4) then merges {0, 1} with {3, 4}, which leaves a set of 4 elements and 2 sets in all.

Example 2

Input: n = 4 ops = ["unionSets", "unionSets", "unionSets", "unionSets", "connected", "setSize", "count"] args = [[0, 1], [1, 2], [2, 0], [3, 3], [0, 2], [1], []]

Output: [true,true,false,false,true,3,2]

Explanation: After the first two merges, 0, 1 and 2 share one set, so unionSets(2, 0) finds them together and returns false. unionSets(3, 3) returns false too, because 3 is always in its own set. Neither call changes anything.

Example 3

Input: n = 1 ops = ["count", "setSize", "connected", "unionSets"] args = [[], [0], [0, 0], [0, 0]]

Output: [1,1,true,false]

Explanation: With one element there is one set, of size 1. An element is always in the same set as itself.

Check your understanding

Work out the answer for this input before you write any code.

n = 4 ops = ["unionSets", "unionSets", "unionSets", "count"] args = [[0, 1], [1, 0], [2, 3], []]

Constraints

  • 1 ≤ n ≤ 105
  • 1 ≤ ops.length ≤ 2 × 105
  • Each ops[i] is "unionSets", "connected", "setSize" or "count".
  • args.length equals ops.length, and args[i] holds two numbers for unionSets and connected, one for setSize and none for count.
  • 0 ≤ x, y < n for every argument.

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.