move UnionFind structure outside