class UnionFindSet
{
public:
UnionFindSet(int size) {
sets.resize(size);
for (size_t i = 0; i < size; i++) sets[i] = i;
};
void setUnion(int a, int b)//合并集合
{
int s1 = getSet(a), s2 = getSet(b);
sets[s1] = s2;
};
int getSet(int a) //查询集合,在查询的过程修改每个元素的集合为最终集合
{
int father;
if (a != sets[a])
{
father = getSet(sets[a]);
sets[a] = father;
return father;
}
return a;
};
private:
vector<int> sets;
};