温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

朋友圈(使用并查集)的实现

发布时间:2020-07-10 07:09:20 来源:网络 阅读:384 作者:zheng_feng 栏目:编程语言

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

};


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI