并查集

基本操作和模板

#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;
}



 

posted @ 2016-11-15 16:21  2000xyy  阅读(92)  评论(0编辑  收藏  举报