Skip to content

Commit e8a46dc

Browse files
committed
feat: update solutions to lc problem: No.1697
No.1697.Checking Existence of Edge Length Limited Paths
1 parent f8cdf2c commit e8a46dc

File tree

4 files changed

+50
-52
lines changed

4 files changed

+50
-52
lines changed

solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/README.md

+20-18
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@
5252

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

55-
并查集。
55+
这是一道**离线思维**的题目。
56+
57+
**离线**的意思是,一道题目会给出若干 query,而这些 query 会全部提前给出。也就是说,我们可以不必按照 query 的顺序依次对它们进行处理,而是可以按照另外某种顺序进行处理。与**离线**相对应的是**在线**,即所有 query 会依次给出,在返回第 k 个 query 的答案之前,不会获得第 k+1 个 query。
58+
59+
对于本题,可以转换为:将小于 limit 的所有边加入图中,判断此时 pj, qj 是否连通。可以用并查集来实现。
60+
61+
以下是并查集的几个常用模板。
5662

5763
模板 1——朴素并查集:
5864

@@ -111,8 +117,6 @@ p[find(a)] = find(b)
111117
d[find(a)] = distance
112118
```
113119

114-
对于本题,可以转换为:将小于 limit 的所有边加入图中,判断此时是否存在从 pj 到 qj 的一条路径。
115-
116120
<!-- tabs:start -->
117121

118122
### **Python3**
@@ -122,23 +126,23 @@ d[find(a)] = distance
122126
```python
123127
class Solution:
124128
def distanceLimitedPathsExist(self, n: int, edgeList: List[List[int]], queries: List[List[int]]) -> List[bool]:
125-
p = list(range(n))
126-
127129
def find(x):
128130
if p[x] != x:
129131
p[x] = find(p[x])
130132
return p[x]
131133

134+
p = list(range(n))
132135
edgeList.sort(key=lambda x: x[2])
136+
133137
m = len(queries)
134138
indexes = list(range(m))
135-
indexes.sort(key=lambda x: queries[x][2])
139+
indexes.sort(key=lambda i: queries[i][2])
136140
ans = [False] * m
137141
i = 0
138142
for j in indexes:
139-
pj, qj, limit = queries[j][0], queries[j][1], queries[j][2]
143+
pj, qj, limit = queries[j]
140144
while i < len(edgeList) and edgeList[i][2] < limit:
141-
u, v = edgeList[i][0], edgeList[i][1]
145+
u, v, _ = edgeList[i]
142146
p[find(u)] = find(v)
143147
i += 1
144148
ans[j] = find(pj) == find(qj)
@@ -234,13 +238,18 @@ public:
234238
### **Go**
235239
236240
```go
237-
var p []int
238-
239241
func distanceLimitedPathsExist(n int, edgeList [][]int, queries [][]int) []bool {
240-
p = make([]int, n)
242+
p := make([]int, n)
241243
for i := 0; i < n; i++ {
242244
p[i] = i
243245
}
246+
var find func(x int) int
247+
find = func(x int) int {
248+
if p[x] != x {
249+
p[x] = find(p[x])
250+
}
251+
return p[x]
252+
}
244253
sort.Slice(edgeList, func(i, j int) bool {
245254
return edgeList[i][2] < edgeList[j][2]
246255
})
@@ -265,13 +274,6 @@ func distanceLimitedPathsExist(n int, edgeList [][]int, queries [][]int) []bool
265274
}
266275
return ans
267276
}
268-
269-
func find(x int) int {
270-
if p[x] != x {
271-
p[x] = find(p[x])
272-
}
273-
return p[x]
274-
}
275277
```
276278

277279
### **...**

solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/README_EN.md

+16-16
Original file line numberDiff line numberDiff line change
@@ -46,30 +46,32 @@ For the second query, there is a path (0 -&gt; 1 -&gt; 2) of two edges with dist
4646

4747
## Solutions
4848

49+
Union find.
50+
4951
<!-- tabs:start -->
5052

5153
### **Python3**
5254

5355
```python
5456
class Solution:
5557
def distanceLimitedPathsExist(self, n: int, edgeList: List[List[int]], queries: List[List[int]]) -> List[bool]:
56-
p = list(range(n))
57-
5858
def find(x):
5959
if p[x] != x:
6060
p[x] = find(p[x])
6161
return p[x]
62-
62+
63+
p = list(range(n))
6364
edgeList.sort(key=lambda x: x[2])
65+
6466
m = len(queries)
6567
indexes = list(range(m))
66-
indexes.sort(key=lambda x: queries[x][2])
68+
indexes.sort(key=lambda i: queries[i][2])
6769
ans = [False] * m
6870
i = 0
6971
for j in indexes:
70-
pj, qj, limit = queries[j][0], queries[j][1], queries[j][2]
72+
pj, qj, limit = queries[j]
7173
while i < len(edgeList) and edgeList[i][2] < limit:
72-
u, v = edgeList[i][0], edgeList[i][1]
74+
u, v, _ = edgeList[i]
7375
p[find(u)] = find(v)
7476
i += 1
7577
ans[j] = find(pj) == find(qj)
@@ -163,13 +165,18 @@ public:
163165
### **Go**
164166
165167
```go
166-
var p []int
167-
168168
func distanceLimitedPathsExist(n int, edgeList [][]int, queries [][]int) []bool {
169-
p = make([]int, n)
169+
p := make([]int, n)
170170
for i := 0; i < n; i++ {
171171
p[i] = i
172172
}
173+
var find func(x int) int
174+
find = func(x int) int {
175+
if p[x] != x {
176+
p[x] = find(p[x])
177+
}
178+
return p[x]
179+
}
173180
sort.Slice(edgeList, func(i, j int) bool {
174181
return edgeList[i][2] < edgeList[j][2]
175182
})
@@ -194,13 +201,6 @@ func distanceLimitedPathsExist(n int, edgeList [][]int, queries [][]int) []bool
194201
}
195202
return ans
196203
}
197-
198-
func find(x int) int {
199-
if p[x] != x {
200-
p[x] = find(p[x])
201-
}
202-
return p[x]
203-
}
204204
```
205205

206206
### **...**

solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/Solution.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
var p []int
2-
31
func distanceLimitedPathsExist(n int, edgeList [][]int, queries [][]int) []bool {
4-
p = make([]int, n)
2+
p := make([]int, n)
53
for i := 0; i < n; i++ {
64
p[i] = i
75
}
6+
var find func(x int) int
7+
find = func(x int) int {
8+
if p[x] != x {
9+
p[x] = find(p[x])
10+
}
11+
return p[x]
12+
}
813
sort.Slice(edgeList, func(i, j int) bool {
914
return edgeList[i][2] < edgeList[j][2]
1015
})
@@ -28,11 +33,4 @@ func distanceLimitedPathsExist(n int, edgeList [][]int, queries [][]int) []bool
2833
ans[j] = find(pj) == find(qj)
2934
}
3035
return ans
31-
}
32-
33-
func find(x int) int {
34-
if p[x] != x {
35-
p[x] = find(p[x])
36-
}
37-
return p[x]
3836
}

solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/Solution.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
class Solution:
2-
def distanceLimitedPathsExist(
3-
self, n: int, edgeList: List[List[int]], queries: List[List[int]]
4-
) -> List[bool]:
5-
p = list(range(n))
6-
2+
def distanceLimitedPathsExist(self, n: int, edgeList: List[List[int]], queries: List[List[int]]) -> List[bool]:
73
def find(x):
84
if p[x] != x:
95
p[x] = find(p[x])
106
return p[x]
117

8+
p = list(range(n))
129
edgeList.sort(key=lambda x: x[2])
10+
1311
m = len(queries)
1412
indexes = list(range(m))
15-
indexes.sort(key=lambda x: queries[x][2])
13+
indexes.sort(key=lambda i: queries[i][2])
1614
ans = [False] * m
1715
i = 0
1816
for j in indexes:
19-
pj, qj, limit = queries[j][0], queries[j][1], queries[j][2]
17+
pj, qj, limit = queries[j]
2018
while i < len(edgeList) and edgeList[i][2] < limit:
21-
u, v = edgeList[i][0], edgeList[i][1]
19+
u, v, _ = edgeList[i]
2220
p[find(u)] = find(v)
2321
i += 1
2422
ans[j] = find(pj) == find(qj)

0 commit comments

Comments
 (0)