implement final UF version with path compression