final version of union find problem