Skip to content

Commit 0201d7b

Browse files
committed
feat: add solutions to lc problem: No.1319.Number of Operations to Make
Network Connected
1 parent 437ee3d commit 0201d7b

File tree

6 files changed

+441
-4
lines changed

6 files changed

+441
-4
lines changed

solution/1300-1399/1319.Number of Operations to Make Network Connected/README.md

Lines changed: 190 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,215 @@
5858
<li>两台计算机不会通过多条线缆连接。</li>
5959
</ul>
6060

61-
6261
## 解法
6362

6463
<!-- 这里可写通用的实现逻辑 -->
6564

65+
并查集。
66+
67+
模板 1——朴素并查集:
68+
69+
```python
70+
# 初始化,p存储每个点的祖宗节点
71+
p = [i for i in range(n)]
72+
# 返回x的祖宗节点
73+
def find(x):
74+
if p[x] != x:
75+
# 路径压缩
76+
p[x] = find(p[x])
77+
return p[x]
78+
# 合并a和b所在的两个集合
79+
p[find(a)] = find(b)
80+
```
81+
82+
模板 2——维护 size 的并查集:
83+
84+
```python
85+
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
86+
p = [i for i in range(n)]
87+
size = [1] * n
88+
# 返回x的祖宗节点
89+
def find(x):
90+
if p[x] != x:
91+
# 路径压缩
92+
p[x] = find(p[x])
93+
return p[x]
94+
# 合并a和b所在的两个集合
95+
size[find(b)] += size[find(a)]
96+
p[find(a)] = find(b)
97+
```
98+
99+
模板 3——维护到祖宗节点距离的并查集:
100+
101+
```python
102+
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
103+
p = [i for i in range(n)]
104+
d = [0] * n
105+
# 返回x的祖宗节点
106+
def find(x):
107+
if p[x] != x:
108+
t = find(p[x])
109+
d[x] += d[p[x]]
110+
p[x] = t
111+
return p[x]
112+
# 合并a和b所在的两个集合
113+
p[find(a)] = find(b)
114+
d[find(a)] = dinstance
115+
```
116+
117+
对于本题,先遍历所有的边:
118+
119+
- 如果边的两个节点已经属于同个集合,说明两个节点已经相连,不必再将此边加入到集合中,累加 cnt;
120+
- 否则将边加入集合中。
121+
122+
最后判断集合的数量 total 与 cnt 的大小关系。
123+
124+
66125
<!-- tabs:start -->
67126

68127
### **Python3**
69128

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

72131
```python
73-
132+
class Solution:
133+
def makeConnected(self, n: int, connections: List[List[int]]) -> int:
134+
p = [i for i in range(n)]
135+
136+
def find(x):
137+
if p[x] != x:
138+
p[x] = find(p[x])
139+
return p[x]
140+
141+
cnt = 0
142+
for a, b in connections:
143+
if find(a) == find(b):
144+
cnt += 1
145+
else:
146+
p[find(a)] = find(b)
147+
total = sum(i == find(i) for i in range(n))
148+
return -1 if total - 1 > cnt else total - 1
74149
```
75150

76151
### **Java**
77152

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

80155
```java
156+
class Solution {
157+
private int[] p;
158+
159+
public int makeConnected(int n, int[][] connections) {
160+
p = new int[n];
161+
for (int i = 0; i < n; ++i) {
162+
p[i] = i;
163+
}
164+
int cnt = 0;
165+
for (int[] e : connections) {
166+
if (find(e[0]) == find(e[1])) {
167+
++cnt;
168+
} else {
169+
p[find(e[0])] = find(e[1]);
170+
}
171+
}
172+
int total = 0;
173+
for (int i = 0; i < n; ++i) {
174+
if (i == find(i)) {
175+
++total;
176+
}
177+
}
178+
return total - 1 > cnt ? -1 : total - 1;
179+
}
180+
181+
private int find(int x) {
182+
if (p[x] != x) {
183+
p[x] = find(p[x]);
184+
}
185+
return p[x];
186+
}
187+
}
188+
```
189+
190+
### **C++**
191+
192+
```cpp
193+
class Solution {
194+
public:
195+
vector<int> p;
196+
197+
int makeConnected(int n, vector<vector<int>> &connections) {
198+
p.resize(n);
199+
for (int i = 0; i < n; ++i)
200+
{
201+
p[i] = i;
202+
}
203+
int cnt = 0;
204+
for (auto e : connections)
205+
{
206+
if (find(e[0]) == find(e[1]))
207+
{
208+
++cnt;
209+
}
210+
else
211+
{
212+
p[find(e[0])] = find(e[1]);
213+
}
214+
}
215+
int total = 0;
216+
for (int i = 0; i < n; ++i)
217+
{
218+
if (i == find(i))
219+
{
220+
++total;
221+
}
222+
}
223+
return total - 1 > cnt ? -1 : total - 1;
224+
}
225+
226+
int find(int x) {
227+
if (p[x] != x)
228+
p[x] = find(p[x]);
229+
return p[x];
230+
}
231+
};
232+
```
81233

234+
### **Go**
235+
236+
```go
237+
var p []int
238+
239+
func makeConnected(n int, connections [][]int) int {
240+
p = make([]int, n)
241+
for i := 0; i < n; i++ {
242+
p[i] = i
243+
}
244+
cnt := 0
245+
for _, e := range connections {
246+
if find(e[0]) == find(e[1]) {
247+
cnt++
248+
} else {
249+
p[find(e[0])] = find(e[1])
250+
}
251+
}
252+
total := 0
253+
for i := 0; i < n; i++ {
254+
if i == find(i) {
255+
total++
256+
}
257+
}
258+
if total-1 > cnt {
259+
return -1
260+
}
261+
return total - 1
262+
}
263+
264+
func find(x int) int {
265+
if p[x] != x {
266+
p[x] = find(p[x])
267+
}
268+
return p[x]
269+
}
82270
```
83271

84272
### **...**

solution/1300-1399/1319.Number of Operations to Make Network Connected/README_EN.md

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,149 @@
5656
<li>No two computers are connected by more than one cable.</li>
5757
</ul>
5858

59-
6059
## Solutions
6160

6261
<!-- tabs:start -->
6362

6463
### **Python3**
6564

6665
```python
67-
66+
class Solution:
67+
def makeConnected(self, n: int, connections: List[List[int]]) -> int:
68+
p = [i for i in range(n)]
69+
70+
def find(x):
71+
if p[x] != x:
72+
p[x] = find(p[x])
73+
return p[x]
74+
75+
cnt = 0
76+
for a, b in connections:
77+
if find(a) == find(b):
78+
cnt += 1
79+
else:
80+
p[find(a)] = find(b)
81+
total = sum(i == find(i) for i in range(n))
82+
return -1 if total - 1 > cnt else total - 1
6883
```
6984

7085
### **Java**
7186

7287
```java
88+
class Solution {
89+
private int[] p;
90+
91+
public int makeConnected(int n, int[][] connections) {
92+
p = new int[n];
93+
for (int i = 0; i < n; ++i) {
94+
p[i] = i;
95+
}
96+
int cnt = 0;
97+
for (int[] e : connections) {
98+
if (find(e[0]) == find(e[1])) {
99+
++cnt;
100+
} else {
101+
p[find(e[0])] = find(e[1]);
102+
}
103+
}
104+
int total = 0;
105+
for (int i = 0; i < n; ++i) {
106+
if (i == find(i)) {
107+
++total;
108+
}
109+
}
110+
return total - 1 > cnt ? -1 : total - 1;
111+
}
112+
113+
private int find(int x) {
114+
if (p[x] != x) {
115+
p[x] = find(p[x]);
116+
}
117+
return p[x];
118+
}
119+
}
120+
```
121+
122+
### **C++**
123+
124+
```cpp
125+
class Solution {
126+
public:
127+
vector<int> p;
128+
129+
int makeConnected(int n, vector<vector<int>> &connections) {
130+
p.resize(n);
131+
for (int i = 0; i < n; ++i)
132+
{
133+
p[i] = i;
134+
}
135+
int cnt = 0;
136+
for (auto e : connections)
137+
{
138+
if (find(e[0]) == find(e[1]))
139+
{
140+
++cnt;
141+
}
142+
else
143+
{
144+
p[find(e[0])] = find(e[1]);
145+
}
146+
}
147+
int total = 0;
148+
for (int i = 0; i < n; ++i)
149+
{
150+
if (i == find(i))
151+
{
152+
++total;
153+
}
154+
}
155+
return total - 1 > cnt ? -1 : total - 1;
156+
}
157+
158+
int find(int x) {
159+
if (p[x] != x)
160+
p[x] = find(p[x]);
161+
return p[x];
162+
}
163+
};
164+
```
73165

166+
### **Go**
167+
168+
```go
169+
var p []int
170+
171+
func makeConnected(n int, connections [][]int) int {
172+
p = make([]int, n)
173+
for i := 0; i < n; i++ {
174+
p[i] = i
175+
}
176+
cnt := 0
177+
for _, e := range connections {
178+
if find(e[0]) == find(e[1]) {
179+
cnt++
180+
} else {
181+
p[find(e[0])] = find(e[1])
182+
}
183+
}
184+
total := 0
185+
for i := 0; i < n; i++ {
186+
if i == find(i) {
187+
total++
188+
}
189+
}
190+
if total-1 > cnt {
191+
return -1
192+
}
193+
return total - 1
194+
}
195+
196+
func find(x int) int {
197+
if p[x] != x {
198+
p[x] = find(p[x])
199+
}
200+
return p[x]
201+
}
74202
```
75203

76204
### **...**

0 commit comments

Comments
 (0)