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

#include <leetcode.h>

Public 成员函数

 UnionFind (int m, int n)
 
bool contains (pair< int, int > p) const
 
pair< int, int > find (pair< int, int > val)
 
int get_size (pair< int, int > p)
 
void merge (pair< int, int > a, pair< int, int > b)
 
bool same (pair< int, int > a, pair< int, int > b)
 

Private 属性

int m
 
int n
 
unordered_map< pair< int, int >, pair< int, int >, function< unsigned int(const pair< int, int > &)> > parent
 
unordered_map< pair< int, int >, int, function< unsigned int(const pair< int, int > &)> > rank
 
unordered_map< pair< int, int >, int, function< unsigned int(const pair< int, int > &)> > size
 

详细描述

在文件 leetcode.h1665 行定义.

构造及析构函数说明

◆ UnionFind()

UnionFind::UnionFind ( int  m,
int  n 
)

在文件 leetcode.cpp4255 行定义.

4256 : m(m), n(n) {
4257 parent = unordered_map<pair<int, int>,
4258 pair<int, int>,
4259 function<unsigned int(const pair<int, int> &)>>(
4260 m * n,
4261 [](const pair<int, int> &p) { return static_cast<unsigned int>(p.first * 50 + p.second); });
4262 size = unordered_map<pair<int, int>,
4263 int,
4264 function<unsigned int(const pair<int, int> &)>>(
4265 m * n,
4266 [](const pair<int, int> &p) { return static_cast<unsigned int>(p.first * 50 + p.second); });
4267 rank = unordered_map<pair<int, int>,
4268 int,
4269 function<unsigned int(const pair<int, int> &)>>(
4270 m * n,
4271 [](const pair<int, int> &p) { return static_cast<unsigned int>(p.first * 50 + p.second); });
4272 for(int i = 0; i < m; i++) {
4273 for(int j = 0; j < n; j++) {
4274 pair<int, int> p = make_pair(i, j);
4275 parent[p] = p;
4276 size[p] = 1;
4277 rank[p] = 0;
4278 }
4279 }
4280 }
unordered_map< pair< int, int >, int, function< unsigned int(const pair< int, int > &)> > size
Definition: leetcode.h:1670
unordered_map< pair< int, int >, int, function< unsigned int(const pair< int, int > &)> > rank
Definition: leetcode.h:1671
unordered_map< pair< int, int >, pair< int, int >, function< unsigned int(const pair< int, int > &)> > parent
Definition: leetcode.h:1669

引用了 m, n, parent, rank , 以及 size.

成员函数说明

◆ contains()

bool UnionFind::contains ( pair< int, int >  p) const

在文件 leetcode.cpp4309 行定义.

4309{ return parent.contains(p); }

引用了 parent.

◆ find()

pair< int, int > UnionFind::find ( pair< int, int >  val)

在文件 leetcode.cpp4282 行定义.

4282 {
4283 if(parent[val] != val) {
4284 parent[val] = find(parent[val]);
4285 }
4286 return parent[val];
4287 }
pair< int, int > find(pair< int, int > val)
Definition: leetcode.cpp:4282

引用了 find() , 以及 parent.

被这些函数引用 find(), get_size(), merge() , 以及 same().

◆ get_size()

int UnionFind::get_size ( pair< int, int >  p)

在文件 leetcode.cpp4311 行定义.

4311{ return size[find(p)]; }

引用了 find() , 以及 size.

◆ merge()

void UnionFind::merge ( pair< int, int >  a,
pair< int, int >  b 
)

在文件 leetcode.cpp4289 行定义.

4289 {
4290 const auto pa = find(a);
4291 const auto pb = find(b);
4292 if(pa != pb) {
4293 const int sum = size[pa] + size[pb];
4294 if(rank[pa] > rank[pb]) {
4295 parent[pb] = pa;
4296 } else {
4297 parent[pa] = pb;
4298 if(rank[pa] == rank[pb]) {
4299 ++rank[pb];
4300 }
4301 }
4302 size[pa] = sum;
4303 size[pb] = sum;
4304 }
4305 }

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

◆ same()

bool UnionFind::same ( pair< int, int >  a,
pair< int, int >  b 
)

在文件 leetcode.cpp4307 行定义.

4307{ return find(a) == find(b); }

引用了 find().

类成员变量说明

◆ m

int leetcode::max_area_of_island::UnionFind::m
private

在文件 leetcode.h1667 行定义.

被这些函数引用 UnionFind().

◆ n

int leetcode::max_area_of_island::UnionFind::n
private

在文件 leetcode.h1668 行定义.

被这些函数引用 UnionFind().

◆ parent

unordered_map<pair<int, int>, pair<int, int>, function<unsigned int(const pair<int, int> &)> > leetcode::max_area_of_island::UnionFind::parent
private

在文件 leetcode.h1669 行定义.

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

◆ rank

unordered_map<pair<int, int>, int, function<unsigned int(const pair<int, int> &)> > leetcode::max_area_of_island::UnionFind::rank
private

在文件 leetcode.h1671 行定义.

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

◆ size

unordered_map<pair<int, int>, int, function<unsigned int(const pair<int, int> &)> > leetcode::max_area_of_island::UnionFind::size
private

在文件 leetcode.h1670 行定义.

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


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