并查集
基本操作和模板
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int father[10001]; int m,x1,y1; int find(int x)//寻找根节点并且压缩路径 { if (father[x]!=x) father[x]=find(father[x]);//递归寻找根节点 return father[x]; } int union (int xi,int yi)//合并两个集合 { xi=find(xi); yi=find(yi); father[xi]=yi; }