mfcheer

图论——并查集

版权声明:转载请注明出处。 https://blog.csdn.net/u014427196/article/details/43087169

/*
	并查集模板
		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;
}
← 全部文章

留言