Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b04d7fe

Browse files
committed
feat: add solutions to lc problem: No.0547.Number of Provinces
1 parent 14c5a12 commit b04d7fe

File tree

4 files changed

+393
-1
lines changed

4 files changed

+393
-1
lines changed

‎solution/0500-0599/0547.Number of Provinces/README.md‎

Lines changed: 194 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,70 @@
5252

5353
<!-- 这里可写通用的实现逻辑 -->
5454

55-
深度优先搜索。判断学生与学生之间是否属于同一个连通分量,最后连通分量的总数即为结果。
55+
**方法一:深度优先搜索**
56+
57+
判断学生与学生之间是否属于同一个连通分量,最后连通分量的总数即为结果。
58+
59+
**方法二:并查集**
60+
61+
模板 1——朴素并查集:
62+
63+
```python
64+
# 初始化,p存储每个点的祖宗节点
65+
p = [i for i in range(n)]
66+
# 返回x的祖宗节点
67+
def find(x):
68+
if p[x] != x:
69+
# 路径压缩
70+
p[x] = find(p[x])
71+
return p[x]
72+
# 合并a和b所在的两个集合
73+
p[find(a)] = find(b)
74+
```
75+
76+
模板 2——维护 size 的并查集:
77+
78+
```python
79+
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
80+
p = [i for i in range(n)]
81+
size = [1] * n
82+
# 返回x的祖宗节点
83+
def find(x):
84+
if p[x] != x:
85+
# 路径压缩
86+
p[x] = find(p[x])
87+
return p[x]
88+
# 合并a和b所在的两个集合
89+
size[find(b)] += size[find(a)]
90+
p[find(a)] = find(b)
91+
```
92+
93+
模板 3——维护到祖宗节点距离的并查集:
94+
95+
```python
96+
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
97+
p = [i for i in range(n)]
98+
d = [0] * n
99+
# 返回x的祖宗节点
100+
def find(x):
101+
if p[x] != x:
102+
t = find(p[x])
103+
d[x] += d[p[x]]
104+
p[x] = t
105+
return p[x]
106+
# 合并a和b所在的两个集合
107+
p[find(a)] = find(b)
108+
d[find(a)] = dinstance
109+
```
56110

57111
<!-- tabs:start -->
58112

59113
### **Python3**
60114

61115
<!-- 这里可写当前语言的特殊实现逻辑 -->
62116

117+
深度优先搜索:
118+
63119
```python
64120
class Solution:
65121
def findCircleNum(self, isConnected: List[List[int]]) -> int:
@@ -79,10 +135,32 @@ class Solution:
79135
return num
80136
```
81137

138+
并查集:
139+
140+
```python
141+
class Solution:
142+
def findCircleNum(self, isConnected: List[List[int]]) -> int:
143+
n = len(isConnected)
144+
p = [i for i in range(n)]
145+
146+
def find(x):
147+
if p[x] != x:
148+
p[x] = find(p[x])
149+
return p[x]
150+
151+
for i in range(n):
152+
for j in range(n):
153+
if i != j and isConnected[i][j] == 1:
154+
p[find(i)] = find(j)
155+
return sum(i == find(i) for i in range(n))
156+
```
157+
82158
### **Java**
83159

84160
<!-- 这里可写当前语言的特殊实现逻辑 -->
85161

162+
深度优先搜索:
163+
86164
```java
87165
class Solution {
88166
public int findCircleNum(int[][] isConnected) {
@@ -109,6 +187,121 @@ class Solution {
109187
}
110188
```
111189

190+
并查集:
191+
192+
```java
193+
class Solution {
194+
private int[] p;
195+
196+
public int findCircleNum(int[][] isConnected) {
197+
int n = isConnected.length;
198+
p = new int[n];
199+
for (int i = 0; i < n; ++i) {
200+
p[i] = i;
201+
}
202+
for (int i = 0; i < n; ++i) {
203+
for (int j = 0; j < n; ++j) {
204+
if (isConnected[i][j] == 1) {
205+
p[find(i)] = find(j);
206+
}
207+
}
208+
}
209+
int cnt = 0;
210+
for (int i = 0; i < n; ++i) {
211+
if (i == find(i)) {
212+
++cnt;
213+
}
214+
}
215+
return cnt;
216+
}
217+
218+
private int find(int x) {
219+
if (p[x] != x) {
220+
p[x] = find(p[x]);
221+
}
222+
return p[x];
223+
}
224+
}
225+
```
226+
227+
### **C++**
228+
229+
```cpp
230+
class Solution {
231+
public:
232+
vector<int> p;
233+
234+
int findCircleNum(vector<vector<int>> &isConnected) {
235+
int n = isConnected.size();
236+
p.resize(n);
237+
for (int i = 0; i < n; ++i)
238+
{
239+
p[i] = i;
240+
}
241+
for (int i = 0; i < n; ++i)
242+
{
243+
for (int j = 0; j < n; ++j)
244+
{
245+
if (isConnected[i][j])
246+
{
247+
p[find(i)] = find(j);
248+
}
249+
}
250+
}
251+
int cnt = 0;
252+
for (int i = 0; i < n; ++i)
253+
{
254+
if (i == find(i))
255+
++cnt;
256+
}
257+
return cnt;
258+
}
259+
260+
int find(int x) {
261+
if (p[x] != x)
262+
{
263+
p[x] = find(p[x]);
264+
}
265+
return p[x];
266+
}
267+
};
268+
```
269+
270+
### **Go**
271+
272+
```go
273+
var p []int
274+
275+
func findCircleNum(isConnected [][]int) int {
276+
n := len(isConnected)
277+
p = make([]int, n)
278+
for i := 1; i < n; i++ {
279+
p[i] = i
280+
}
281+
for i := 0; i < n; i++ {
282+
for j := 0; j < n; j++ {
283+
if isConnected[i][j] == 1 {
284+
p[find(i)] = find(j)
285+
}
286+
}
287+
}
288+
cnt := 0
289+
for i := 0; i < n; i++ {
290+
if i == find(i) {
291+
cnt++
292+
}
293+
}
294+
return cnt
295+
}
296+
297+
func find(x int) int {
298+
if p[x] != x {
299+
p[x] = find(p[x])
300+
}
301+
return p[x]
302+
}
303+
```
304+
112305
### **...**
113306

114307
```

‎solution/0500-0599/0547.Number of Provinces/README_EN.md‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,24 @@ class Solution:
6565
return num
6666
```
6767

68+
```python
69+
class Solution:
70+
def findCircleNum(self, isConnected: List[List[int]]) -> int:
71+
n = len(isConnected)
72+
p = [i for i in range(n)]
73+
74+
def find(x):
75+
if p[x] != x:
76+
p[x] = find(p[x])
77+
return p[x]
78+
79+
for i in range(n):
80+
for j in range(n):
81+
if i != j and isConnected[i][j] == 1:
82+
p[find(i)] = find(j)
83+
return sum(i == find(i) for i in range(n))
84+
```
85+
6886
### **Java**
6987

7088
```java
@@ -93,6 +111,119 @@ class Solution {
93111
}
94112
```
95113

114+
```java
115+
class Solution {
116+
private int[] p;
117+
118+
public int findCircleNum(int[][] isConnected) {
119+
int n = isConnected.length;
120+
p = new int[n];
121+
for (int i = 0; i < n; ++i) {
122+
p[i] = i;
123+
}
124+
for (int i = 0; i < n; ++i) {
125+
for (int j = 0; j < n; ++j) {
126+
if (isConnected[i][j] == 1) {
127+
p[find(i)] = find(j);
128+
}
129+
}
130+
}
131+
int cnt = 0;
132+
for (int i = 0; i < n; ++i) {
133+
if (i == find(i)) {
134+
++cnt;
135+
}
136+
}
137+
return cnt;
138+
}
139+
140+
private int find(int x) {
141+
if (p[x] != x) {
142+
p[x] = find(p[x]);
143+
}
144+
return p[x];
145+
}
146+
}
147+
```
148+
149+
### **C++**
150+
151+
```cpp
152+
class Solution {
153+
public:
154+
vector<int> p;
155+
156+
int findCircleNum(vector<vector<int>> &isConnected) {
157+
int n = isConnected.size();
158+
p.resize(n);
159+
for (int i = 0; i < n; ++i)
160+
{
161+
p[i] = i;
162+
}
163+
for (int i = 0; i < n; ++i)
164+
{
165+
for (int j = 0; j < n; ++j)
166+
{
167+
if (isConnected[i][j])
168+
{
169+
p[find(i)] = find(j);
170+
}
171+
}
172+
}
173+
int cnt = 0;
174+
for (int i = 0; i < n; ++i)
175+
{
176+
if (i == find(i))
177+
++cnt;
178+
}
179+
return cnt;
180+
}
181+
182+
int find(int x) {
183+
if (p[x] != x)
184+
{
185+
p[x] = find(p[x]);
186+
}
187+
return p[x];
188+
}
189+
};
190+
```
191+
192+
### **Go**
193+
194+
```go
195+
var p []int
196+
197+
func findCircleNum(isConnected [][]int) int {
198+
n := len(isConnected)
199+
p = make([]int, n)
200+
for i := 1; i < n; i++ {
201+
p[i] = i
202+
}
203+
for i := 0; i < n; i++ {
204+
for j := 0; j < n; j++ {
205+
if isConnected[i][j] == 1 {
206+
p[find(i)] = find(j)
207+
}
208+
}
209+
}
210+
cnt := 0
211+
for i := 0; i < n; i++ {
212+
if i == find(i) {
213+
cnt++
214+
}
215+
}
216+
return cnt
217+
}
218+
219+
func find(x int) int {
220+
if p[x] != x {
221+
p[x] = find(p[x])
222+
}
223+
return p[x]
224+
}
225+
```
226+
96227
### **...**
97228

98229
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /