const int DefaultSize = 10;
class UFSets {
public:
UFSets(int sz = DefaultSize);
~UFSets() { delete[] parent; }
int Find(int x);
void Union(int Root1, int Root2);
private:
int* parent;
int Size;
};
void UFSets::Union(int Root1, int Root2) {
parent[Root1] += parent[Root2];
parent[Root2] = Root1;
}
int UFSets::Find(int x) {
while (parent[x] >= 0)
x = parent[x];
return x;
}
UFSets::UFSets(int sz = DefaultSize) {
Size = sz;
parent = new int[Size];
for (int i = 0; i < Size; i++)
parent[i] = -1;
}