problemscpp
A collection of my answers to algorithm problems in c++.
Public 成员函数 | Private 属性 | 所有成员列表
UnionFind类 参考

并查集 更多...

#include <templates.h>

Public 成员函数

 UnionFind (int n)
 
unsigned count ()
 
int find (int x)
 
int get_size (int x)
 
bool same (int x, int y)
 
void unite (int x, int y)
 

Private 属性

vector< int > parent
 
vector< int > rank
 
vector< int > size
 

详细描述

并查集

在文件 templates.h91 行定义.

构造及析构函数说明

◆ UnionFind()

UnionFind::UnionFind ( int  n)
explicit

在文件 templates.cpp478 行定义.

478 {
479 this->parent = vector<int>(n);
480 this->rank = vector<int>(n);
481 this->size = vector<int>(n);
482 for(int i = 0; i < n; i++) {
483 this->parent[i] = i;
484 this->rank[i] = 0;
485 size[i] = 1;
486 }
487}
vector< int > size
Definition: templates.h:95
vector< int > rank
Definition: templates.h:94
vector< int > parent
Definition: templates.h:93

引用了 parent, rank , 以及 size.

成员函数说明

◆ count()

unsigned UnionFind::count ( )

在文件 templates.cpp518 行定义.

518 {
519 unordered_set<int> us;
520 for(int i = 0; i < size.size(); i++) {
521 us.insert(find(i));
522 }
523 return us.size();
524}
int find(int x)
Definition: templates.cpp:489

引用了 find() , 以及 size.

◆ find()

int UnionFind::find ( int  x)

在文件 templates.cpp489 行定义.

489 {
490 if(parent[x] != x) {
491 parent[x] = find(parent[x]);
492 }
493 return parent[x];
494}

引用了 find() , 以及 parent.

被这些函数引用 count(), find(), leetcode::process_restricted_friend_requests::Solution::friendRequests(), get_size(), pat::a::a1021::main(), same() , 以及 unite().

◆ get_size()

int UnionFind::get_size ( int  x)

在文件 templates.cpp516 行定义.

516{ return size[find(x)]; }

引用了 find() , 以及 size.

被这些函数引用 acwing::acwing837::main().

◆ same()

bool UnionFind::same ( int  x,
int  y 
)

在文件 templates.cpp514 行定义.

514{ return find(x) == find(y); }

引用了 find().

被这些函数引用 acwing::acwing837::main() , 以及 acwing::acwing859::main().

◆ unite()

void UnionFind::unite ( int  x,
int  y 
)

在文件 templates.cpp496 行定义.

496 {
497 x = find(x);
498 y = find(y);
499 if(x == y) {
500 return;
501 }
502 if(rank[x] < rank[y]) {
503 parent[x] = y;
504 size[y] += size[x];
505 } else {
506 parent[y] = x;
507 size[x] += size[y];
508 if(rank[x] == rank[y]) {
509 rank[x]++;
510 }
511 }
512}

引用了 find(), parent, rank , 以及 size.

被这些函数引用 leetcode::process_restricted_friend_requests::Solution::friendRequests(), acwing::acwing837::main(), acwing::acwing859::main() , 以及 pat::a::a1021::main().

类成员变量说明

◆ parent

vector<int> UnionFind::parent
private

在文件 templates.h93 行定义.

被这些函数引用 UnionFind(), find() , 以及 unite().

◆ rank

vector<int> UnionFind::rank
private

在文件 templates.h94 行定义.

被这些函数引用 UnionFind() , 以及 unite().

◆ size

vector<int> UnionFind::size
private

在文件 templates.h95 行定义.

被这些函数引用 UnionFind(), count(), get_size() , 以及 unite().


该类的文档由以下文件生成: