#pragma once
#include <iostream>
using namespace Std;
Class Unionfindset
{
Public
Unionfindset (int n)
{
_n = n + 1;
_set = new Int[_n];
for (int i = 1; i < _n; ++i)
{
_set[i] =-1;
}
}
int getfriendset (int n, int m, int r[][2])
{
for (int i = 0; i < m; ++i)
{
Unionfriendset (r[i][0],r[i][1]);
}
int count = 0;
for (int i = 1; i < _n; ++i)
{
if (_set[i] <0)
++count;
}
return count;
}
void Unionfriendset (int n,int m)
{
int root1 = Getroot (n);
int root2 = Getroot (m);
if (root1! = Root2)
{
_SET[ROOT1] + = _set[root2];
_set[root2] = ROOT1;
}
}
int getroot (int x)
{
while (_set[x] >= 0)
{
x = _set[x];
}
return x;
}
Protected
int *_set;
size_t _n;
};
This article is from the "end-of-the-guest" blog, please be sure to keep this source http://zheng2048.blog.51cto.com/10612048/1813888
The realization of the circle of friends (using and checking the set)