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

#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://csruizhi.cn/article/ppoiic.html

其他资讯

Copyright © 2007-2024 成都优众联杰科技有限公司 All Rights Reserved 蜀ICP备2024116266号
友情链接: 营销网站建设 重庆网站制作 成都网站设计 成都网站建设公司 网站设计公司 广安网站设计 自适应网站设计 泸州网站建设 app网站建设 成都企业网站建设公司 外贸网站设计方案 网站建设公司 网站建设改版 成都网站设计 手机网站制作设计 成都模版网站建设 成都企业网站制作 成都网站设计 成都商城网站制作 四川成都网站设计 成都网站建设 成都响应式网站建设公司