Loading repovive.com/problems/classics/21
Implement a Union-Find (Disjoint Set Union) data structure that supports the following operations on n elements (numbered 1 to n):
UNION u v: Merge the sets containing elements u and vFIND u v: Check if elements u and v are in the same setInitially, each element is in its own set.
5 7 FIND 1 2 UNION 1 2 FIND 1 2 UNION 2 3 FIND 1 3 FIND 1 4 UNION 4 5
For this input, the required output is NO YES YES NO.
NO YES YES NO
3 4 UNION 1 2 UNION 2 3 FIND 1 3 FIND 1 2
For this input, the required output is YES YES.
YES YES
4 4 FIND 1 2 FIND 2 3 FIND 3 4 FIND 1 4
For this input, the required output is NO NO NO NO.
NO NO NO NO