File tree 21 files changed +210
-122
lines changed
0200-0299/0261.Graph Valid Tree
0300-0399/0323.Number of Connected Components in an Undirected Graph
0500-0599/0547.Number of Provinces
0684.Redundant Connection
0737.Sentence Similarity II
0765.Couples Holding Hands
0924.Minimize Malware Spread
0990.Satisfiability of Equality Equations
1000-1099/1061.Lexicographically Smallest Equivalent String
1300-1399/1319.Number of Operations to Make Network Connected
21 files changed +210
-122
lines changed Original file line number Diff line number Diff line change @@ -55,13 +55,15 @@ DFS 或并查集实现。
55
55
56
56
``` python
57
57
# 初始化,p存储每个点的祖宗节点
58
- p = [i for i in range (n)]
58
+ p = list (range (n))
59
+
59
60
# 返回x的祖宗节点
60
61
def find (x ):
61
62
if p[x] != x:
62
63
# 路径压缩
63
64
p[x] = find(p[x])
64
65
return p[x]
66
+
65
67
# 合并a和b所在的两个集合
66
68
p[find(a)] = find(b)
67
69
```
@@ -70,32 +72,37 @@ p[find(a)] = find(b)
70
72
71
73
``` python
72
74
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
73
- p = [i for i in range (n)]
75
+ p = list ( range (n))
74
76
size = [1 ] * n
77
+
75
78
# 返回x的祖宗节点
76
79
def find (x ):
77
80
if p[x] != x:
78
81
# 路径压缩
79
82
p[x] = find(p[x])
80
83
return p[x]
84
+
81
85
# 合并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)
84
89
```
85
90
86
91
模板 3——维护到祖宗节点距离的并查集:
87
92
88
93
``` python
89
94
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
90
- p = [i for i in range (n)]
95
+ p = list ( range (n))
91
96
d = [0 ] * n
97
+
92
98
# 返回x的祖宗节点
93
99
def find (x ):
94
100
if p[x] != x:
95
101
t = find(p[x])
96
102
d[x] += d[p[x]]
97
103
p[x] = t
98
104
return p[x]
105
+
99
106
# 合并a和b所在的两个集合
100
107
p[find(a)] = find(b)
101
108
d[find(a)] = dinstance
Original file line number Diff line number Diff line change 47
47
48
48
<p ><meta charset =" UTF-8 " />注意:本题与主站 547  ; 题相同:  ; <a href =" https://leetcode-cn.com/problems/number-of-provinces/ " >https://leetcode-cn.com/problems/number-of-provinces/</a ></p >
49
49
50
-
51
50
## 解法
52
51
53
52
<!-- 这里可写通用的实现逻辑 -->
62
61
63
62
``` python
64
63
# 初始化,p存储每个点的祖宗节点
65
- p = [i for i in range (n)]
64
+ p = list (range (n))
65
+
66
66
# 返回x的祖宗节点
67
67
def find (x ):
68
68
if p[x] != x:
69
69
# 路径压缩
70
70
p[x] = find(p[x])
71
71
return p[x]
72
+
72
73
# 合并a和b所在的两个集合
73
74
p[find(a)] = find(b)
74
75
```
@@ -77,32 +78,37 @@ p[find(a)] = find(b)
77
78
78
79
``` python
79
80
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
80
- p = [i for i in range (n)]
81
+ p = list ( range (n))
81
82
size = [1 ] * n
83
+
82
84
# 返回x的祖宗节点
83
85
def find (x ):
84
86
if p[x] != x:
85
87
# 路径压缩
86
88
p[x] = find(p[x])
87
89
return p[x]
90
+
88
91
# 合并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)
91
95
```
92
96
93
97
模板 3——维护到祖宗节点距离的并查集:
94
98
95
99
``` python
96
100
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
97
- p = [i for i in range (n)]
101
+ p = list ( range (n))
98
102
d = [0 ] * n
103
+
99
104
# 返回x的祖宗节点
100
105
def find (x ):
101
106
if p[x] != x:
102
107
t = find(p[x])
103
108
d[x] += d[p[x]]
104
109
p[x] = t
105
110
return p[x]
111
+
106
112
# 合并a和b所在的两个集合
107
113
p[find(a)] = find(b)
108
114
d[find(a)] = dinstance
@@ -141,13 +147,13 @@ class Solution:
141
147
class Solution :
142
148
def findCircleNum (self , isConnected : List[List[int ]]) -> int :
143
149
n = len (isConnected)
144
- p = [i for i in range (n)]
145
-
150
+ p = list ( range (n))
151
+
146
152
def find (x ):
147
153
if p[x] != x:
148
154
p[x] = find(p[x])
149
155
return p[x]
150
-
156
+
151
157
for i in range (n):
152
158
for j in range (n):
153
159
if i != j and isConnected[i][j] == 1 :
Original file line number Diff line number Diff line change 48
48
49
49
<p ><meta charset =" UTF-8 " />注意:本题与主站 684  ; 题相同:  ; <a href =" https://leetcode-cn.com/problems/redundant-connection/ " >https://leetcode-cn.com/problems/redundant-connection/</a ></p >
50
50
51
-
52
51
## 解法
53
52
54
53
<!-- 这里可写通用的实现逻辑 -->
59
58
60
59
``` python
61
60
# 初始化,p存储每个点的祖宗节点
62
- p = [i for i in range (n)]
61
+ p = list (range (n))
62
+
63
63
# 返回x的祖宗节点
64
64
def find (x ):
65
65
if p[x] != x:
66
66
# 路径压缩
67
67
p[x] = find(p[x])
68
68
return p[x]
69
+
69
70
# 合并a和b所在的两个集合
70
71
p[find(a)] = find(b)
71
72
```
@@ -74,32 +75,37 @@ p[find(a)] = find(b)
74
75
75
76
``` python
76
77
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
77
- p = [i for i in range (n)]
78
+ p = list ( range (n))
78
79
size = [1 ] * n
80
+
79
81
# 返回x的祖宗节点
80
82
def find (x ):
81
83
if p[x] != x:
82
84
# 路径压缩
83
85
p[x] = find(p[x])
84
86
return p[x]
87
+
85
88
# 合并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)
88
92
```
89
93
90
94
模板 3——维护到祖宗节点距离的并查集:
91
95
92
96
``` python
93
97
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
94
- p = [i for i in range (n)]
98
+ p = list ( range (n))
95
99
d = [0 ] * n
100
+
96
101
# 返回x的祖宗节点
97
102
def find (x ):
98
103
if p[x] != x:
99
104
t = find(p[x])
100
105
d[x] += d[p[x]]
101
106
p[x] = t
102
107
return p[x]
108
+
103
109
# 合并a和b所在的两个集合
104
110
p[find(a)] = find(b)
105
111
d[find(a)] = dinstance
Original file line number Diff line number Diff line change 30
30
31
31
``` python
32
32
# 初始化,p存储每个点的祖宗节点
33
- p = [i for i in range (n)]
33
+ p = list (range (n))
34
+
34
35
# 返回x的祖宗节点
35
36
def find (x ):
36
37
if p[x] != x:
37
38
# 路径压缩
38
39
p[x] = find(p[x])
39
40
return p[x]
41
+
40
42
# 合并a和b所在的两个集合
41
43
p[find(a)] = find(b)
42
44
```
@@ -45,38 +47,42 @@ p[find(a)] = find(b)
45
47
46
48
``` python
47
49
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
48
- p = [i for i in range (n)]
50
+ p = list ( range (n))
49
51
size = [1 ] * n
52
+
50
53
# 返回x的祖宗节点
51
54
def find (x ):
52
55
if p[x] != x:
53
56
# 路径压缩
54
57
p[x] = find(p[x])
55
58
return p[x]
59
+
56
60
# 合并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)
59
64
```
60
65
61
66
模板 3——维护到祖宗节点距离的并查集:
62
67
63
68
``` python
64
69
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
65
- p = [i for i in range (n)]
70
+ p = list ( range (n))
66
71
d = [0 ] * n
72
+
67
73
# 返回x的祖宗节点
68
74
def find (x ):
69
75
if p[x] != x:
70
76
t = find(p[x])
71
77
d[x] += d[p[x]]
72
78
p[x] = t
73
79
return p[x]
80
+
74
81
# 合并a和b所在的两个集合
75
82
p[find(a)] = find(b)
76
83
d[find(a)] = dinstance
77
84
```
78
85
79
-
80
86
<!-- tabs:start -->
81
87
82
88
### ** Python3**
Original file line number Diff line number Diff line change 43
43
44
44
``` python
45
45
# 初始化,p存储每个点的祖宗节点
46
- p = [i for i in range (n)]
46
+ p = list ( range (n))
47
47
48
48
# 返回x的祖宗节点
49
49
def find (x ):
@@ -61,7 +61,7 @@ p[find(a)] = find(b)
61
61
62
62
``` python
63
63
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
64
- p = [i for i in range (n)]
64
+ p = list ( range (n))
65
65
size = [1 ] * n
66
66
67
67
# 返回x的祖宗节点
@@ -71,17 +71,17 @@ def find(x):
71
71
p[x] = find(p[x])
72
72
return p[x]
73
73
74
-
75
74
# 合并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)
78
78
```
79
79
80
80
模板 3——维护到祖宗节点距离的并查集:
81
81
82
82
``` python
83
83
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
84
- p = [i for i in range (n)]
84
+ p = list ( range (n))
85
85
d = [0 ] * n
86
86
87
87
# 返回x的祖宗节点
@@ -107,7 +107,7 @@ d[find(a)] = dinstance
107
107
``` python
108
108
class Solution :
109
109
def countComponents (self , n : int , edges : List[List[int ]]) -> int :
110
- p = [i for i in range (n)]
110
+ p = list ( range (n))
111
111
112
112
def find (x ):
113
113
if p[x] != x:
@@ -126,7 +126,7 @@ class Solution:
126
126
``` java
127
127
class Solution {
128
128
private int [] p;
129
-
129
+
130
130
public int countComponents (int n , int [][] edges ) {
131
131
p = new int [n];
132
132
for (int i = 0 ; i < n; ++ i) {
Original file line number Diff line number Diff line change 44
44
``` python
45
45
class Solution :
46
46
def countComponents (self , n : int , edges : List[List[int ]]) -> int :
47
- p = [i for i in range (n)]
47
+ p = list ( range (n))
48
48
49
49
def find (x ):
50
50
if p[x] != x:
@@ -61,7 +61,7 @@ class Solution:
61
61
``` java
62
62
class Solution {
63
63
private int [] p;
64
-
64
+
65
65
public int countComponents (int n , int [][] edges ) {
66
66
p = new int [n];
67
67
for (int i = 0 ; i < n; ++ i) {
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
def countComponents (self , n : int , edges : List [List [int ]]) -> int :
3
- p = [ i for i in range (n )]
3
+ p = list ( range (n ))
4
4
5
5
def find (x ):
6
6
if p [x ] != x :
You can’t perform that action at this time.
0 commit comments