A union-find algorithm is an algorithm that performs two useful operations on such a data structure:
Find: Determine which subset a particular element is in. This can be used for determining if two elements are in the same subset.
Union: Join two subsets into a single subset.

root = [i for i in range(MAX_LEN)]
def find(x):
if root[x] == x:
return x
else:
return find(root[x])
def union(x, y):
x = find(x)
y = find(y)
root[y] = x
Above implementations are simple but not optimized.
