Skip to content

Commit 834d9ee

Browse files
committed
feat: update union-find algorithm template
1 parent e57df73 commit 834d9ee

File tree

21 files changed

+210
-122
lines changed

21 files changed

+210
-122
lines changed

lcof2/剑指 Offer II 105. 岛屿的最大面积/README.md

+12-5
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ DFS 或并查集实现。
5555

5656
```python
5757
# 初始化,p存储每个点的祖宗节点
58-
p = [i for i in range(n)]
58+
p = list(range(n))
59+
5960
# 返回x的祖宗节点
6061
def find(x):
6162
if p[x] != x:
6263
# 路径压缩
6364
p[x] = find(p[x])
6465
return p[x]
66+
6567
# 合并a和b所在的两个集合
6668
p[find(a)] = find(b)
6769
```
@@ -70,32 +72,37 @@ p[find(a)] = find(b)
7072

7173
```python
7274
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
73-
p = [i for i in range(n)]
75+
p = list(range(n))
7476
size = [1] * n
77+
7578
# 返回x的祖宗节点
7679
def find(x):
7780
if p[x] != x:
7881
# 路径压缩
7982
p[x] = find(p[x])
8083
return p[x]
84+
8185
# 合并a和b所在的两个集合
82-
size[find(b)] += size[find(a)]
83-
p[find(a)] = find(b)
86+
if find(a) != find(b):
87+
size[find(b)] += size[find(a)]
88+
p[find(a)] = find(b)
8489
```
8590

8691
模板 3——维护到祖宗节点距离的并查集:
8792

8893
```python
8994
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
90-
p = [i for i in range(n)]
95+
p = list(range(n))
9196
d = [0] * n
97+
9298
# 返回x的祖宗节点
9399
def find(x):
94100
if p[x] != x:
95101
t = find(p[x])
96102
d[x] += d[p[x]]
97103
p[x] = t
98104
return p[x]
105+
99106
# 合并a和b所在的两个集合
100107
p[find(a)] = find(b)
101108
d[find(a)] = dinstance

lcof2/剑指 Offer II 116. 朋友圈/README.md

+15-9
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747

4848
<p><meta charset="UTF-8" />注意:本题与主站 547&nbsp;题相同:&nbsp;<a href="https://leetcode-cn.com/problems/number-of-provinces/">https://leetcode-cn.com/problems/number-of-provinces/</a></p>
4949

50-
5150
## 解法
5251

5352
<!-- 这里可写通用的实现逻辑 -->
@@ -62,13 +61,15 @@
6261

6362
```python
6463
# 初始化,p存储每个点的祖宗节点
65-
p = [i for i in range(n)]
64+
p = list(range(n))
65+
6666
# 返回x的祖宗节点
6767
def find(x):
6868
if p[x] != x:
6969
# 路径压缩
7070
p[x] = find(p[x])
7171
return p[x]
72+
7273
# 合并a和b所在的两个集合
7374
p[find(a)] = find(b)
7475
```
@@ -77,32 +78,37 @@ p[find(a)] = find(b)
7778

7879
```python
7980
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
80-
p = [i for i in range(n)]
81+
p = list(range(n))
8182
size = [1] * n
83+
8284
# 返回x的祖宗节点
8385
def find(x):
8486
if p[x] != x:
8587
# 路径压缩
8688
p[x] = find(p[x])
8789
return p[x]
90+
8891
# 合并a和b所在的两个集合
89-
size[find(b)] += size[find(a)]
90-
p[find(a)] = find(b)
92+
if find(a) != find(b):
93+
size[find(b)] += size[find(a)]
94+
p[find(a)] = find(b)
9195
```
9296

9397
模板 3——维护到祖宗节点距离的并查集:
9498

9599
```python
96100
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
97-
p = [i for i in range(n)]
101+
p = list(range(n))
98102
d = [0] * n
103+
99104
# 返回x的祖宗节点
100105
def find(x):
101106
if p[x] != x:
102107
t = find(p[x])
103108
d[x] += d[p[x]]
104109
p[x] = t
105110
return p[x]
111+
106112
# 合并a和b所在的两个集合
107113
p[find(a)] = find(b)
108114
d[find(a)] = dinstance
@@ -141,13 +147,13 @@ class Solution:
141147
class Solution:
142148
def findCircleNum(self, isConnected: List[List[int]]) -> int:
143149
n = len(isConnected)
144-
p = [i for i in range(n)]
145-
150+
p = list(range(n))
151+
146152
def find(x):
147153
if p[x] != x:
148154
p[x] = find(p[x])
149155
return p[x]
150-
156+
151157
for i in range(n):
152158
for j in range(n):
153159
if i != j and isConnected[i][j] == 1:

lcof2/剑指 Offer II 118. 多余的边/README.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848

4949
<p><meta charset="UTF-8" />注意:本题与主站 684&nbsp;题相同:&nbsp;<a href="https://leetcode-cn.com/problems/redundant-connection/">https://leetcode-cn.com/problems/redundant-connection/</a></p>
5050

51-
5251
## 解法
5352

5453
<!-- 这里可写通用的实现逻辑 -->
@@ -59,13 +58,15 @@
5958

6059
```python
6160
# 初始化,p存储每个点的祖宗节点
62-
p = [i for i in range(n)]
61+
p = list(range(n))
62+
6363
# 返回x的祖宗节点
6464
def find(x):
6565
if p[x] != x:
6666
# 路径压缩
6767
p[x] = find(p[x])
6868
return p[x]
69+
6970
# 合并a和b所在的两个集合
7071
p[find(a)] = find(b)
7172
```
@@ -74,32 +75,37 @@ p[find(a)] = find(b)
7475

7576
```python
7677
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
77-
p = [i for i in range(n)]
78+
p = list(range(n))
7879
size = [1] * n
80+
7981
# 返回x的祖宗节点
8082
def find(x):
8183
if p[x] != x:
8284
# 路径压缩
8385
p[x] = find(p[x])
8486
return p[x]
87+
8588
# 合并a和b所在的两个集合
86-
size[find(b)] += size[find(a)]
87-
p[find(a)] = find(b)
89+
if find(a) != find(b):
90+
size[find(b)] += size[find(a)]
91+
p[find(a)] = find(b)
8892
```
8993

9094
模板 3——维护到祖宗节点距离的并查集:
9195

9296
```python
9397
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
94-
p = [i for i in range(n)]
98+
p = list(range(n))
9599
d = [0] * n
100+
96101
# 返回x的祖宗节点
97102
def find(x):
98103
if p[x] != x:
99104
t = find(p[x])
100105
d[x] += d[p[x]]
101106
p[x] = t
102107
return p[x]
108+
103109
# 合并a和b所在的两个集合
104110
p[find(a)] = find(b)
105111
d[find(a)] = dinstance

solution/0200-0299/0261.Graph Valid Tree/README.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@
3030

3131
```python
3232
# 初始化,p存储每个点的祖宗节点
33-
p = [i for i in range(n)]
33+
p = list(range(n))
34+
3435
# 返回x的祖宗节点
3536
def find(x):
3637
if p[x] != x:
3738
# 路径压缩
3839
p[x] = find(p[x])
3940
return p[x]
41+
4042
# 合并a和b所在的两个集合
4143
p[find(a)] = find(b)
4244
```
@@ -45,38 +47,42 @@ p[find(a)] = find(b)
4547

4648
```python
4749
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
48-
p = [i for i in range(n)]
50+
p = list(range(n))
4951
size = [1] * n
52+
5053
# 返回x的祖宗节点
5154
def find(x):
5255
if p[x] != x:
5356
# 路径压缩
5457
p[x] = find(p[x])
5558
return p[x]
59+
5660
# 合并a和b所在的两个集合
57-
size[find(b)] += size[find(a)]
58-
p[find(a)] = find(b)
61+
if find(a) != find(b):
62+
size[find(b)] += size[find(a)]
63+
p[find(a)] = find(b)
5964
```
6065

6166
模板 3——维护到祖宗节点距离的并查集:
6267

6368
```python
6469
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
65-
p = [i for i in range(n)]
70+
p = list(range(n))
6671
d = [0] * n
72+
6773
# 返回x的祖宗节点
6874
def find(x):
6975
if p[x] != x:
7076
t = find(p[x])
7177
d[x] += d[p[x]]
7278
p[x] = t
7379
return p[x]
80+
7481
# 合并a和b所在的两个集合
7582
p[find(a)] = find(b)
7683
d[find(a)] = dinstance
7784
```
7885

79-
8086
<!-- tabs:start -->
8187

8288
### **Python3**

solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
```python
4545
# 初始化,p存储每个点的祖宗节点
46-
p = [i for i in range(n)]
46+
p = list(range(n))
4747

4848
# 返回x的祖宗节点
4949
def find(x):
@@ -61,7 +61,7 @@ p[find(a)] = find(b)
6161

6262
```python
6363
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
64-
p = [i for i in range(n)]
64+
p = list(range(n))
6565
size = [1] * n
6666

6767
# 返回x的祖宗节点
@@ -71,17 +71,17 @@ def find(x):
7171
p[x] = find(p[x])
7272
return p[x]
7373

74-
7574
# 合并a和b所在的两个集合
76-
size[find(b)] += size[find(a)]
77-
p[find(a)] = find(b)
75+
if find(a) != find(b):
76+
size[find(b)] += size[find(a)]
77+
p[find(a)] = find(b)
7878
```
7979

8080
模板 3——维护到祖宗节点距离的并查集:
8181

8282
```python
8383
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
84-
p = [i for i in range(n)]
84+
p = list(range(n))
8585
d = [0] * n
8686

8787
# 返回x的祖宗节点
@@ -107,7 +107,7 @@ d[find(a)] = dinstance
107107
```python
108108
class Solution:
109109
def countComponents(self, n: int, edges: List[List[int]]) -> int:
110-
p = [i for i in range(n)]
110+
p = list(range(n))
111111

112112
def find(x):
113113
if p[x] != x:
@@ -126,7 +126,7 @@ class Solution:
126126
```java
127127
class Solution {
128128
private int[] p;
129-
129+
130130
public int countComponents(int n, int[][] edges) {
131131
p = new int[n];
132132
for (int i = 0; i < n; ++i) {

solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
```python
4545
class Solution:
4646
def countComponents(self, n: int, edges: List[List[int]]) -> int:
47-
p = [i for i in range(n)]
47+
p = list(range(n))
4848

4949
def find(x):
5050
if p[x] != x:
@@ -61,7 +61,7 @@ class Solution:
6161
```java
6262
class Solution {
6363
private int[] p;
64-
64+
6565
public int countComponents(int n, int[][] edges) {
6666
p = new int[n];
6767
for (int i = 0; i < n; ++i) {

solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Solution:
22
def countComponents(self, n: int, edges: List[List[int]]) -> int:
3-
p = [i for i in range(n)]
3+
p = list(range(n))
44

55
def find(x):
66
if p[x] != x:

0 commit comments

Comments
 (0)