Skip to content

Commit 06ca535

Browse files
committed
docs: union find
add size count
1 parent 31fcb94 commit 06ca535

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

problems/problems_827/solution.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def point_to_idx(x, y):
2222
for dx, dy in DIRS:
2323
if 0 <= (nx := i + dx) < n and 0 <= (ny := j + dy) < n and grid[nx][ny] == 1:
2424
uf.union(p, point_to_idx(nx, ny))
25-
ans = max(uf.count)
25+
ans = max(uf.size)
2626
for i, row in enumerate(grid):
2727
for j, val in enumerate(row):
2828
if val == 0:
@@ -34,26 +34,25 @@ def point_to_idx(x, y):
3434
if root in explored:
3535
continue
3636
explored.add(root)
37-
tot += uf.count[root]
37+
tot += uf.size[root]
3838
ans = max(ans, tot)
3939

4040
return ans
4141

4242
class UnionFind:
43-
def __init__(self, size):
44-
self.parent = list(range(size))
45-
self.rank = [1] * size
46-
self.count = [1] * size
47-
self.size = size
48-
self.cc = size
43+
def __init__(self, n: int):
44+
self.parent = list(range(n))
45+
self.rank = [1] * n
46+
self.size = [1] * n
47+
self.cc = n
4948

50-
def find(self, x):
49+
def find(self, x: int) -> int:
5150
while self.parent[x] != x:
5251
self.parent[x] = self.parent[self.parent[x]] # 路径压缩
5352
x = self.parent[x]
5453
return x
5554

56-
def union(self, x, y):
55+
def union(self, x: int, y: int) -> bool:
5756
root_x = self.find(x)
5857
root_y = self.find(y)
5958

@@ -63,14 +62,14 @@ def union(self, x, y):
6362
# 按秩合并
6463
if self.rank[root_x] > self.rank[root_y]:
6564
self.parent[root_y] = root_x
66-
self.count[root_x] += self.count[root_y]
65+
self.size[root_x] += self.size[root_y]
6766
else:
6867
self.parent[root_x] = root_y
6968
if self.rank[root_x] == self.rank[root_y]:
7069
self.rank[root_y] += 1
71-
self.count[root_y] += self.count[root_x]
70+
self.size[root_y] += self.size[root_x]
7271
self.cc -= 1
7372
return True
7473

75-
def is_connected(self, x, y):
74+
def is_connected(self, x:int, y:int) -> bool:
7675
return self.find(x) == self.find(y)

templates/data_structure/union_find_set.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77

88
```python
99
class UnionFind:
10-
def __init__(self, size):
11-
self.parent = list(range(size))
12-
self.rank = [1] * size
13-
self.size = size
10+
def __init__(self, n: int):
11+
self.parent = list(range(n))
12+
self.rank = [1] * n
13+
self.size = [1] * n
14+
self.cc = n
1415

15-
def find(self, x):
16+
def find(self, x: int) -> int:
1617
while self.parent[x] != x:
1718
self.parent[x] = self.parent[self.parent[x]] # 路径压缩
1819
x = self.parent[x]
1920
return x
2021

21-
def union(self, x, y):
22+
def union(self, x: int, y: int) -> bool:
2223
root_x = self.find(x)
2324
root_y = self.find(y)
2425

@@ -28,15 +29,18 @@ class UnionFind:
2829
# 按秩合并
2930
if self.rank[root_x] > self.rank[root_y]:
3031
self.parent[root_y] = root_x
32+
self.size[root_x] += self.size[root_y]
3133
else:
3234
self.parent[root_x] = root_y
3335
if self.rank[root_x] == self.rank[root_y]:
3436
self.rank[root_y] += 1
35-
self.size -= 1
37+
self.size[root_y] += self.size[root_x]
38+
self.cc -= 1
3639
return True
3740

38-
def is_connected(self, x, y):
41+
def is_connected(self, x:int, y:int) -> bool:
3942
return self.find(x) == self.find(y)
43+
4044
```
4145

4246
```go

0 commit comments

Comments
 (0)