Disjoint-set (Disjoint-Set) is a can maintain a plurality of non-overlapping sets. It has two basic operations:
GET query an element which belongs to the set
MERGE Merge two sets into a set , which is to point to a parent node to another parent node
During the inquiry, we can make use of the path back the way all the elements point to the parent node, this operation is called path compression .
template:
int fa[SIZE];
int get(int x){
if(x == fa[x]) return x;
else return fa[x] = get(fa[x]); //路径压缩
}
void merge(int x, int y){
fa[get(x)] = get(y);
return;
}