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

#pragma once

创新互联公司成立于2013年,是专业互联网技术服务公司,拥有项目网站制作、成都网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元新疆做网站,已为上家服务,为新疆各地企业和个人服务,联系电话:18980820575

#include

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;

};


分享文章:朋友圈(使用并查集)的实现
当前URL:http://bzwzjz.com/article/ppoiic.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 手机网站制作设计 成都网站建设 手机网站建设 移动手机网站制作 定制网站建设 网站制作 成都网站制作 成都网站建设 成都网站设计 网站建设开发 成都h5网站建设 成都网站设计制作公司 成都企业网站建设 成都网站建设公司 网站建设 响应式网站设计 成都网站建设 成都网站建设 营销型网站建设 成都网站建设公司 网站制作 上市集团网站建设