首页 > 其他 > 详细

图论——并查集

时间:2015-01-24 18:46:56      阅读:223      评论:0      收藏:0      [点我收藏+]

/*
	并查集模板
		by:mfcheer
*/

#include <iostream>  
#include <cstdio>  
#include <string>  
#include <cstring>  
#include <cstdlib>  
#include <algorithm>  
#include <vector>  
#include <set>  
#include <map>  

using namespace std;
#define N 100010

int n, m;
int fa[N];

int findd(int x)
{
	if (fa[x] == -1)
		return x;
	else
		return fa[x] = findd(fa[x]);
}

void un(int x, int y)
{
	int fx = findd(x);
	int fy = findd(y);
	if (fx == fy)
		return;
	fa[fx] = fy;
}

int main()
{
	memset(fa, -1, sizeof(fa));
	while (cin >> n >> m)
	{
		un(n,m);
	}
	return 0;
}


图论——并查集

原文:http://blog.csdn.net/u014427196/article/details/43087169

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!