first version implemention of union-find algorithm