Join operation for two set of sets

As a part of a problem that I am working on, I am required to write a function in CPP that will join two different sets of sets.

Example 1-

A = {{1,2}, {3,4}}
B = {{1,3}}
Join(A, B) = C = {{1,2,3,4}}

Example 2 -

A = {{1,2}, {3,4}}
B = {{1}, {5}}
Join(A, B) = C = {{1,2}, {3,4}, {5}}

Example 3 -
A = {{1,2,3}, {5,6},{4,7,8}}
B = {{3,5,7}, {2,6,8},{1,4}}
Join(A, B) = C = {{1,2,3,4,5,6,7,8}}

We can assume that the sets inside a set A are mutually disjoint and the sets inside the result set should also be mutually disjoint. I did understand that we can use the Union-Find algorithm to solve this, but I think it needs to be tweaked in order to solve this problem.

How can we solve this problem? If not Union-Find, which another algorithm can we use?

Comments (0)