并查集
int fa[maxn]; int findfa(int x) //并查集的两种操作 1.找father (寻找根) { int rtfa = x; while(rtfa!=fa[rtfa]) rtfa = fa[rtfa]; int i=x,j; //路径压缩 将途中所有的点的father从上一级改到根 while(fa[i]!=rtfa) { j = fa[i]; fa[i] =rtfa; i = j; } return rtfa; } void join(int x,int y) //2.合并fa (将y加入到x的集合中) { int fx = findfa(x); int fy = findfa(y); if(fx!=fy) fa[fx] =fy; }