dsa · medium

Union-Find (Disjoint Set)

GreyOrangeGraphDesign

UnionFind(n) nodes 0..n-1. union(a,b), find(x) canonical parent, connected(a,b). Path compression + union by rank.

Fill in the UnionFind class. The starter already walks ops / args and calls your methods — leave the driver at the bottom as-is. Constructors contribute null; booleans print as true / false.

**Example**

`` UnionFind(5) → null union(0, 1) → true union(1, 2) → true connected(0, 2) → true connected(0, 3) → false union(3, 4) → true connected(0, 3) → false ``

Constraints

1 <= n <= 200

Examples

Example 1

Input:
["UnionFind","union","union","connected","connected","union","connected"]
[[5],[0,1],[1,2],[0,2],[0,3],[3,4],[0,3]]

Expected:
[null,true,true,true,false,true,false]

Open in the Dojo editor