refine the union-find