Skip to content

Commit 7ef97b6

Browse files
committed
feat: add solutions to lc problem: No.0323.Number of Connected
Components in an Undirected Graph
1 parent fe2053a commit 7ef97b6

File tree

39 files changed

+1926
-7
lines changed

39 files changed

+1926
-7
lines changed

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

+184-2
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,209 @@
3333
<p><strong>注意:</strong><br>
3434
你可以假设在 <code>edges</code> 中不会出现重复的边。而且由于所以的边都是无向边,<code>[0, 1]</code> 与 <code>[1, 0]</code>&nbsp; 相同,所以它们不会同时在 <code>edges</code> 中出现。</p>
3535

36-
3736
## 解法
3837

3938
<!-- 这里可写通用的实现逻辑 -->
4039

40+
并查集。
41+
42+
模板 1——朴素并查集:
43+
44+
```python
45+
# 初始化,p存储每个点的祖宗节点
46+
p = [i for i in range(n)]
47+
48+
# 返回x的祖宗节点
49+
def find(x):
50+
if p[x] != x:
51+
# 路径压缩
52+
p[x] = find(p[x])
53+
return p[x]
54+
55+
56+
# 合并a和b所在的两个集合
57+
p[find(a)] = find(b)
58+
```
59+
60+
模板 2——维护 size 的并查集:
61+
62+
```python
63+
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
64+
p = [i for i in range(n)]
65+
size = [1] * n
66+
67+
# 返回x的祖宗节点
68+
def find(x):
69+
if p[x] != x:
70+
# 路径压缩
71+
p[x] = find(p[x])
72+
return p[x]
73+
74+
75+
# 合并a和b所在的两个集合
76+
size[find(b)] += size[find(a)]
77+
p[find(a)] = find(b)
78+
```
79+
80+
模板 3——维护到祖宗节点距离的并查集:
81+
82+
```python
83+
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
84+
p = [i for i in range(n)]
85+
d = [0] * n
86+
87+
# 返回x的祖宗节点
88+
def find(x):
89+
if p[x] != x:
90+
t = find(p[x])
91+
d[x] += d[p[x]]
92+
p[x] = t
93+
return p[x]
94+
95+
96+
# 合并a和b所在的两个集合
97+
p[find(a)] = find(b)
98+
d[find(a)] = dinstance
99+
```
100+
41101
<!-- tabs:start -->
42102

43103
### **Python3**
44104

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

47107
```python
48-
108+
class Solution:
109+
def countComponents(self, n: int, edges: List[List[int]]) -> int:
110+
p = [i for i in range(n)]
111+
112+
def find(x):
113+
if p[x] != x:
114+
p[x] = find(p[x])
115+
return p[x]
116+
117+
for a, b in edges:
118+
p[find(b)] = find(a)
119+
cnt = 0
120+
visit = [False] * n
121+
for i in range(n):
122+
if not visit[find(i)]:
123+
cnt += 1
124+
visit[find(i)] = True
125+
return cnt
49126
```
50127

51128
### **Java**
52129

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

55132
```java
133+
class Solution {
134+
private int[] p;
135+
136+
public int countComponents(int n, int[][] edges) {
137+
p = new int[n];
138+
for (int i = 0; i < n; ++i) {
139+
p[i] = i;
140+
}
141+
for (int[] e : edges) {
142+
int a = e[0], b = e[1];
143+
p[find(b)] = find(a);
144+
}
145+
146+
int cnt = 0;
147+
boolean[] visit = new boolean[n];
148+
for (int i = 0; i < n; ++i) {
149+
if (!visit[find(i)]) {
150+
++cnt;
151+
visit[find(i)] = true;
152+
}
153+
}
154+
return cnt;
155+
}
156+
157+
private int find(int x) {
158+
if (p[x] != x) {
159+
p[x] = find(p[x]);
160+
}
161+
return p[x];
162+
}
163+
}
164+
```
165+
166+
```cpp
167+
class Solution {
168+
public:
169+
vector<int> p;
170+
171+
int countComponents(int n, vector<vector<int>> &edges) {
172+
p.resize(n);
173+
for (int i = 0; i < n; ++i)
174+
{
175+
p[i] = i;
176+
}
177+
for (auto e : edges)
178+
{
179+
int a = e[0], b = e[1];
180+
p[find(b)] = find(a);
181+
}
182+
int cnt = 0;
183+
vector<bool> visit(n, false);
184+
for (int i = 0; i < n; ++i)
185+
{
186+
if (!visit[find(i)])
187+
{
188+
++cnt;
189+
visit[find(i)] = true;
190+
}
191+
}
192+
return cnt;
193+
}
194+
195+
int find(int x) {
196+
if (p[x] != x)
197+
{
198+
p[x] = find(p[x]);
199+
}
200+
return p[x];
201+
}
202+
};
203+
```
56204

205+
### **Go**
206+
207+
```go
208+
var p []int
209+
210+
func countComponents(n int, edges [][]int) int {
211+
p = make([]int, n)
212+
for i := 1; i < n; i++ {
213+
p[i] = i
214+
}
215+
for _, e := range edges {
216+
a, b := e[0], e[1]
217+
p[find(b)] = find(a)
218+
}
219+
cnt := 0
220+
visit := make([]bool, n)
221+
for i := 0; i < n; i++ {
222+
visit[i] = false
223+
}
224+
for i := 0; i < n; i++ {
225+
if !visit[find(i)] {
226+
cnt++
227+
visit[find(i)] = true
228+
}
229+
}
230+
return cnt
231+
}
232+
233+
func find(x int) int {
234+
if p[x] != x {
235+
p[x] = find(p[x])
236+
}
237+
return p[x]
238+
}
57239
```
58240

59241
### **...**

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

+125-2
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,144 @@
3535
<li>There are no repeated edges.</li>
3636
</ul>
3737

38-
3938
## Solutions
4039

4140
<!-- tabs:start -->
4241

4342
### **Python3**
4443

4544
```python
46-
45+
class Solution:
46+
def countComponents(self, n: int, edges: List[List[int]]) -> int:
47+
p = [i for i in range(n)]
48+
49+
def find(x):
50+
if p[x] != x:
51+
p[x] = find(p[x])
52+
return p[x]
53+
54+
for a, b in edges:
55+
p[find(b)] = find(a)
56+
cnt = 0
57+
visit = [False] * n
58+
for i in range(n):
59+
if not visit[find(i)]:
60+
cnt += 1
61+
visit[find(i)] = True
62+
return cnt
4763
```
4864

4965
### **Java**
5066

5167
```java
68+
class Solution {
69+
private int[] p;
70+
71+
public int countComponents(int n, int[][] edges) {
72+
p = new int[n];
73+
for (int i = 0; i < n; ++i) {
74+
p[i] = i;
75+
}
76+
for (int[] e : edges) {
77+
int a = e[0], b = e[1];
78+
p[find(b)] = find(a);
79+
}
80+
81+
int cnt = 0;
82+
boolean[] visit = new boolean[n];
83+
for (int i = 0; i < n; ++i) {
84+
if (!visit[find(i)]) {
85+
++cnt;
86+
visit[find(i)] = true;
87+
}
88+
}
89+
return cnt;
90+
}
91+
92+
private int find(int x) {
93+
if (p[x] != x) {
94+
p[x] = find(p[x]);
95+
}
96+
return p[x];
97+
}
98+
}
99+
```
100+
101+
### **C++**
102+
103+
```cpp
104+
class Solution {
105+
public:
106+
vector<int> p;
107+
108+
int countComponents(int n, vector<vector<int>> &edges) {
109+
p.resize(n);
110+
for (int i = 0; i < n; ++i)
111+
{
112+
p[i] = i;
113+
}
114+
for (auto e : edges)
115+
{
116+
int a = e[0], b = e[1];
117+
p[find(b)] = find(a);
118+
}
119+
int cnt = 0;
120+
vector<bool> visit(n, false);
121+
for (int i = 0; i < n; ++i)
122+
{
123+
if (!visit[find(i)])
124+
{
125+
++cnt;
126+
visit[find(i)] = true;
127+
}
128+
}
129+
return cnt;
130+
}
131+
132+
int find(int x) {
133+
if (p[x] != x)
134+
{
135+
p[x] = find(p[x]);
136+
}
137+
return p[x];
138+
}
139+
};
140+
```
52141

142+
### **Go**
143+
144+
```go
145+
var p []int
146+
147+
func countComponents(n int, edges [][]int) int {
148+
p = make([]int, n)
149+
for i := 1; i < n; i++ {
150+
p[i] = i
151+
}
152+
for _, e := range edges {
153+
a, b := e[0], e[1]
154+
p[find(b)] = find(a)
155+
}
156+
cnt := 0
157+
visit := make([]bool, n)
158+
for i := 0; i < n; i++ {
159+
visit[i] = false
160+
}
161+
for i := 0; i < n; i++ {
162+
if !visit[find(i)] {
163+
cnt++
164+
visit[find(i)] = true
165+
}
166+
}
167+
return cnt
168+
}
169+
170+
func find(x int) int {
171+
if p[x] != x {
172+
p[x] = find(p[x])
173+
}
174+
return p[x]
175+
}
53176
```
54177

55178
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
public:
3+
vector<int> p;
4+
5+
int countComponents(int n, vector<vector<int>> &edges) {
6+
p.resize(n);
7+
for (int i = 0; i < n; ++i)
8+
{
9+
p[i] = i;
10+
}
11+
for (auto e : edges)
12+
{
13+
int a = e[0], b = e[1];
14+
p[find(b)] = find(a);
15+
}
16+
int cnt = 0;
17+
vector<bool> visit(n, false);
18+
for (int i = 0; i < n; ++i)
19+
{
20+
if (!visit[find(i)])
21+
{
22+
++cnt;
23+
visit[find(i)] = true;
24+
}
25+
}
26+
return cnt;
27+
}
28+
29+
int find(int x) {
30+
if (p[x] != x)
31+
{
32+
p[x] = find(p[x]);
33+
}
34+
return p[x];
35+
}
36+
};

0 commit comments

Comments
 (0)