查:

int find(int x){
	if(fa[x]==x)return x;
	else return fa[x]=find(fa[x]);
}

并:

if(find(u)!=find(v))fa[find(v)]=find(u);