implement weighted-union-find with path compression