基本模板:
/*UF 模板*/
class UF
{
public:
/*初始化*/
void UF(int n)
{
id.reset(n);
for(int i=0; i < n; i++)
{
id[i] = i;
}
};
/*连接*/
boolean connect(int i,int j)
{
return root(i) == root(j);
}
/*并集合*/
void unionSet(int p, int q)
{
int i = root(p);
int j = root(q);
id[i] = j;
}
private:
int root(int i)
{
while(i != id[i])
{
id[i] = id[id[i]];
i = id[i];
}
return i;
}
private:
vector<int> id;
};