The realization of the circle of friends (using and checking the set)

Source: Internet
Author: User

#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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.