不相交集的类架构
class DisjSets { public: explicit DisjSets(int numElement); int find (int x) const; int find (int x); void unionSets1(int root1,int root2); void unionSets2(int root1,int root2) private: vector<int> s; }; DisjSets::DisjSets(int numElement):s(numElement) { for (int i=0;i<s.size();i++) s[i]=i; //不相交集的初始化例程 } void DisjSets::unionSets1(int root1,int root2) { s[root2]=root1; } int DisjSets::find(int x)const { if(s[x]<0) return x; else return find(s[x]); } //按高度求并的程序 void DisjSets::unionSets2(int root1,int root2) { if(s[root2]<s[root1]) s[root1]=root2; else { if(s[root1]==s[root2]) s[root1]--; s[root2] = root1; } } //路径压缩 int DisjSets::find(int x) { if(s[x]<0) return x; else return s[x]=find(s[x]); }