Skip to content

Commit 437ee3d

Browse files
committed
feat: add solutions to lc problem: No.0684. Redundant Connection
1 parent b04d7fe commit 437ee3d

File tree

7 files changed

+333
-26
lines changed

7 files changed

+333
-26
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454

5555
**方法一:深度优先搜索**
5656

57-
判断学生与学生之间是否属于同一个连通分量,最后连通分量的总数即为结果。
57+
判断城市之间是否属于同一个连通分量,最后连通分量的总数即为结果。
5858

5959
**方法二:并查集**
6060

solution/0600-0699/0684.Redundant Connection/README.md

+152-2
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,177 @@
4444
<p><strong>更新(2017-09-26):</strong><br>
4545
我们已经重新检查了问题描述及测试用例,明确图是<em><strong>无向&nbsp;</strong></em>图。对于有向图详见<strong><a href="https://leetcodechina.com/problems/redundant-connection-ii/description/">冗余连接II</a>。</strong>对于造成任何不便,我们深感歉意。</p>
4646

47-
4847
## 解法
4948

5049
<!-- 这里可写通用的实现逻辑 -->
5150

51+
并查集。
52+
53+
模板 1——朴素并查集:
54+
55+
```python
56+
# 初始化,p存储每个点的祖宗节点
57+
p = [i for i in range(n)]
58+
# 返回x的祖宗节点
59+
def find(x):
60+
if p[x] != x:
61+
# 路径压缩
62+
p[x] = find(p[x])
63+
return p[x]
64+
# 合并a和b所在的两个集合
65+
p[find(a)] = find(b)
66+
```
67+
68+
模板 2——维护 size 的并查集:
69+
70+
```python
71+
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
72+
p = [i for i in range(n)]
73+
size = [1] * n
74+
# 返回x的祖宗节点
75+
def find(x):
76+
if p[x] != x:
77+
# 路径压缩
78+
p[x] = find(p[x])
79+
return p[x]
80+
# 合并a和b所在的两个集合
81+
size[find(b)] += size[find(a)]
82+
p[find(a)] = find(b)
83+
```
84+
85+
模板 3——维护到祖宗节点距离的并查集:
86+
87+
```python
88+
# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离
89+
p = [i for i in range(n)]
90+
d = [0] * n
91+
# 返回x的祖宗节点
92+
def find(x):
93+
if p[x] != x:
94+
t = find(p[x])
95+
d[x] += d[p[x]]
96+
p[x] = t
97+
return p[x]
98+
# 合并a和b所在的两个集合
99+
p[find(a)] = find(b)
100+
d[find(a)] = dinstance
101+
```
102+
103+
对于本题,先遍历所有的边,如果边的两个节点已经属于同个集合,说明两个节点已经相连,若再将这条件加入集合中,就会出现环,因此可以直接返回这条边。
104+
52105
<!-- tabs:start -->
53106

54107
### **Python3**
55108

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

58111
```python
59-
112+
class Solution:
113+
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
114+
p = [i for i in range(1010)]
115+
116+
def find(x):
117+
if p[x] != x:
118+
p[x] = find(p[x])
119+
return p[x]
120+
121+
for a, b in edges:
122+
if find(a) == find(b):
123+
return [a, b]
124+
p[find(a)] = find(b)
125+
return []
60126
```
61127

62128
### **Java**
63129

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

66132
```java
133+
class Solution {
134+
private int[] p;
135+
136+
public int[] findRedundantConnection(int[][] edges) {
137+
p = new int[1010];
138+
for (int i = 0; i < p.length; ++i) {
139+
p[i] = i;
140+
}
141+
for (int[] e : edges) {
142+
if (find(e[0]) == find(e[1])) {
143+
return e;
144+
}
145+
p[find(e[0])] = find(e[1]);
146+
}
147+
return null;
148+
}
149+
150+
private int find(int x) {
151+
if (p[x] != x) {
152+
p[x] = find(p[x]);
153+
}
154+
return p[x];
155+
}
156+
}
157+
```
158+
159+
### **C++**
160+
161+
```cpp
162+
class Solution {
163+
public:
164+
vector<int> p;
165+
166+
vector<int> findRedundantConnection(vector<vector<int>> &edges) {
167+
p.resize(1010);
168+
for (int i = 0; i < p.size(); ++i)
169+
{
170+
p[i] = i;
171+
}
172+
for (auto e : edges)
173+
{
174+
if (find(e[0]) == find(e[1]))
175+
{
176+
return e;
177+
}
178+
p[find(e[0])] = find(e[1]);
179+
}
180+
return {};
181+
}
182+
183+
int find(int x) {
184+
if (p[x] != x)
185+
{
186+
p[x] = find(p[x]);
187+
}
188+
return p[x];
189+
}
190+
};
191+
```
67192

193+
### **Go**
194+
195+
```go
196+
var p []int
197+
198+
func findRedundantConnection(edges [][]int) []int {
199+
p = make([]int, 1010)
200+
for i := 0; i < len(p); i++ {
201+
p[i] = i
202+
}
203+
for _, e := range edges {
204+
if find(e[0]) == find(e[1]) {
205+
return e
206+
}
207+
p[find(e[0])] = find(e[1])
208+
}
209+
return nil
210+
}
211+
212+
func find(x int) int {
213+
if p[x] != x {
214+
p[x] = find(p[x])
215+
}
216+
return p[x]
217+
}
68218
```
69219

70220
### **...**

solution/0600-0699/0684.Redundant Connection/README_EN.md

+101-8
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ In this problem, a tree is an <b>undirected</b> graph that is connected and has
1010

1111
</p><p>
1212

13-
The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N), with one additional edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed.
13+
The given input is a graph that started as a tree with N nodes (with distinct values 1, 2, ..., N), with one additional edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed.
1414

1515
</p><p>
1616

17-
The resulting graph is given as a 2D-array of <code>edges</code>. Each element of <code>edges</code> is a pair <code>[u, v]</code> with <code>u < v</code>, that represents an <b>undirected</b> edge connecting nodes <code>u</code> and <code>v</code>.
17+
The resulting graph is given as a 2D-array of <code>edges</code>. Each element of <code>edges</code> is a pair <code>[u, v]</code> with <code>u < v</code>, that represents an <b>undirected</b> edge connecting nodes <code>u</code> and <code>v</code>.
1818

1919
</p><p>
2020

21-
Return an edge that can be removed so that the resulting graph is a tree of N nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array. The answer edge <code>[u, v]</code> should be in the same format, with <code>u < v</code>.
21+
Return an edge that can be removed so that the resulting graph is a tree of N nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array. The answer edge <code>[u, v]</code> should be in the same format, with <code>u < v</code>.
2222

2323
</p><p><b>Example 1:</b><br />
2424

@@ -68,12 +68,8 @@ Return an edge that can be removed so that the resulting graph is a tree of N no
6868

6969
</p>
7070

71-
72-
7371
<br />
7472

75-
76-
7773
<p>
7874

7975
<b><font color="red">Update (2017-09-26):</font></b><br>
@@ -89,13 +85,110 @@ We have overhauled the problem description + test cases and specified clearly th
8985
### **Python3**
9086

9187
```python
92-
88+
class Solution:
89+
def findRedundantConnection(self, edges: List[List[int]]) -> List[int]:
90+
p = [i for i in range(1010)]
91+
92+
def find(x):
93+
if p[x] != x:
94+
p[x] = find(p[x])
95+
return p[x]
96+
97+
for a, b in edges:
98+
if find(a) == find(b):
99+
return [a, b]
100+
p[find(a)] = find(b)
101+
return []
93102
```
94103

95104
### **Java**
96105

97106
```java
107+
class Solution {
108+
private int[] p;
109+
110+
public int[] findRedundantConnection(int[][] edges) {
111+
p = new int[1010];
112+
for (int i = 0; i < p.length; ++i) {
113+
p[i] = i;
114+
}
115+
for (int[] e : edges) {
116+
if (find(e[0]) == find(e[1])) {
117+
return e;
118+
}
119+
p[find(e[0])] = find(e[1]);
120+
}
121+
return null;
122+
}
123+
124+
private int find(int x) {
125+
if (p[x] != x) {
126+
p[x] = find(p[x]);
127+
}
128+
return p[x];
129+
}
130+
}
131+
```
132+
133+
### **C++**
134+
135+
```cpp
136+
class Solution {
137+
public:
138+
vector<int> p;
139+
140+
vector<int> findRedundantConnection(vector<vector<int>> &edges) {
141+
p.resize(1010);
142+
for (int i = 0; i < p.size(); ++i)
143+
{
144+
p[i] = i;
145+
}
146+
for (auto e : edges)
147+
{
148+
if (find(e[0]) == find(e[1]))
149+
{
150+
return e;
151+
}
152+
p[find(e[0])] = find(e[1]);
153+
}
154+
return {};
155+
}
156+
157+
int find(int x) {
158+
if (p[x] != x)
159+
{
160+
p[x] = find(p[x]);
161+
}
162+
return p[x];
163+
}
164+
};
165+
```
98166

167+
### **Go**
168+
169+
```go
170+
var p []int
171+
172+
func findRedundantConnection(edges [][]int) []int {
173+
p = make([]int, 1010)
174+
for i := 0; i < len(p); i++ {
175+
p[i] = i
176+
}
177+
for _, e := range edges {
178+
if find(e[0]) == find(e[1]) {
179+
return e
180+
}
181+
p[find(e[0])] = find(e[1])
182+
}
183+
return nil
184+
}
185+
186+
func find(x int) int {
187+
if p[x] != x {
188+
p[x] = find(p[x])
189+
}
190+
return p[x]
191+
}
99192
```
100193

101194
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
vector<int> p;
4+
5+
vector<int> findRedundantConnection(vector<vector<int>> &edges) {
6+
p.resize(1010);
7+
for (int i = 0; i < p.size(); ++i)
8+
{
9+
p[i] = i;
10+
}
11+
for (auto e : edges)
12+
{
13+
if (find(e[0]) == find(e[1]))
14+
{
15+
return e;
16+
}
17+
p[find(e[0])] = find(e[1]);
18+
}
19+
return {};
20+
}
21+
22+
int find(int x) {
23+
if (p[x] != x)
24+
{
25+
p[x] = find(p[x]);
26+
}
27+
return p[x];
28+
}
29+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var p []int
2+
3+
func findRedundantConnection(edges [][]int) []int {
4+
p = make([]int, 1010)
5+
for i := 0; i < len(p); i++ {
6+
p[i] = i
7+
}
8+
for _, e := range edges {
9+
if find(e[0]) == find(e[1]) {
10+
return e
11+
}
12+
p[find(e[0])] = find(e[1])
13+
}
14+
return nil
15+
}
16+
17+
func find(x int) int {
18+
if p[x] != x {
19+
p[x] = find(p[x])
20+
}
21+
return p[x]
22+
}

0 commit comments

Comments
 (0)