/*
并查集模板
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;
}
图论——并查集
最新推荐文章于 2022-09-29 09:35:48 发布