Skip to content

Commit e6550f0

Browse files
committed
feat: add solutions to lc problem: No.1697.Checking Exsitence of Edge Length Limited Paths
1 parent 93e518b commit e6550f0

File tree

6 files changed

+483
-3
lines changed

6 files changed

+483
-3
lines changed

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

+204-1
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,230 @@
4848
<li>两个点之间可能有 <strong>多条</strong> 边。</li>
4949
</ul>
5050

51-
5251
## 解法
5352

5453
<!-- 这里可写通用的实现逻辑 -->
5554

55+
并查集。
56+
57+
模板 1——朴素并查集:
58+
59+
```python
60+
# 初始化,p存储每个点的父节点
61+
p = list(range(n))
62+
63+
# 返回x的祖宗节点
64+
def find(x):
65+
if p[x] != x:
66+
# 路径压缩
67+
p[x] = find(p[x])
68+
return p[x]
69+
70+
# 合并a和b所在的两个集合
71+
p[find(a)] = find(b)
72+
```
73+
74+
模板 2——维护 size 的并查集:
75+
76+
```python
77+
# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
78+
p = list(range(n))
79+
size = [1] * n
80+
81+
# 返回x的祖宗节点
82+
def find(x):
83+
if p[x] != x:
84+
# 路径压缩
85+
p[x] = find(p[x])
86+
return p[x]
87+
88+
# 合并a和b所在的两个集合
89+
if find(a) != find(b):
90+
size[find(b)] += size[find(a)]
91+
p[find(a)] = find(b)
92+
```
93+
94+
模板 3——维护到祖宗节点距离的并查集:
95+
96+
```python
97+
# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离
98+
p = list(range(n))
99+
d = [0] * n
100+
101+
# 返回x的祖宗节点
102+
def find(x):
103+
if p[x] != x:
104+
t = find(p[x])
105+
d[x] += d[p[x]]
106+
p[x] = t
107+
return p[x]
108+
109+
# 合并a和b所在的两个集合
110+
p[find(a)] = find(b)
111+
d[find(a)] = distance
112+
```
113+
114+
对于本题,可以转换为:将小于 limit 的所有边加入图中,判断此时是否存在从 pj 到 qj 的一条路径。
115+
56116
<!-- tabs:start -->
57117

58118
### **Python3**
59119

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

62122
```python
123+
class Solution:
124+
def distanceLimitedPathsExist(self, n: int, edgeList: List[List[int]], queries: List[List[int]]) -> List[bool]:
125+
p = list(range(n))
126+
127+
def find(x):
128+
if p[x] != x:
129+
p[x] = find(p[x])
130+
return p[x]
63131

132+
edgeList.sort(key=lambda x: x[2])
133+
m = len(queries)
134+
indexes = list(range(m))
135+
indexes.sort(key=lambda x: queries[x][2])
136+
ans = [False] * m
137+
i = 0
138+
for j in indexes:
139+
pj, qj, limit = queries[j][0], queries[j][1], queries[j][2]
140+
while i < len(edgeList) and edgeList[i][2] < limit:
141+
u, v = edgeList[i][0], edgeList[i][1]
142+
p[find(u)] = find(v)
143+
i += 1
144+
ans[j] = find(pj) == find(qj)
145+
return ans
64146
```
65147

66148
### **Java**
67149

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

70152
```java
153+
class Solution {
154+
private int[] p;
155+
156+
public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {
157+
p = new int[n];
158+
for (int i = 0; i < n; ++i) {
159+
p[i] = i;
160+
}
161+
int m = queries.length;
162+
Integer[] indexes = new Integer[m];
163+
for (int i = 0; i < m; ++i) {
164+
indexes[i] = i;
165+
}
166+
Arrays.sort(indexes, Comparator.comparingInt(i -> queries[i][2]));
167+
Arrays.sort(edgeList, Comparator.comparingInt(a -> a[2]));
168+
boolean[] ans = new boolean[m];
169+
int i = 0;
170+
for (int j : indexes) {
171+
int pj = queries[j][0], qj = queries[j][1], limit = queries[j][2];
172+
while (i < edgeList.length && edgeList[i][2] < limit) {
173+
int u = edgeList[i][0], v = edgeList[i][1];
174+
p[find(u)] = find(v);
175+
++i;
176+
}
177+
ans[j] = find(pj) == find(qj);
178+
}
179+
return ans;
180+
}
181+
182+
private int find(int x) {
183+
if (p[x] != x) {
184+
p[x] = find(p[x]);
185+
}
186+
return p[x];
187+
}
188+
}
189+
```
190+
191+
### **C++**
192+
193+
```cpp
194+
class Solution {
195+
public:
196+
vector<int> p;
197+
198+
vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edgeList, vector<vector<int>>& queries) {
199+
p.resize(n);
200+
for (int i = 0; i < n; ++i) p[i] = i;
201+
sort(edgeList.begin(), edgeList.end(), [](const auto& e1, const auto& e2) {
202+
return e1[2] < e2[2];
203+
});
204+
int m = queries.size();
205+
vector<int> indexes(m);
206+
for (int i = 0; i < m; ++i) indexes[i] = i;
207+
sort(indexes.begin(), indexes.end(), [&](int i, int j) {
208+
return queries[i][2] < queries[j][2];
209+
});
210+
211+
vector<bool> ans(m, false);
212+
int i = 0;
213+
for (int j : indexes)
214+
{
215+
int pj = queries[j][0], qj = queries[j][1], limit = queries[j][2];
216+
while (i < edgeList.size() && edgeList[i][2] < limit)
217+
{
218+
int u = edgeList[i][0], v = edgeList[i][1];
219+
p[find(u)] = find(v);
220+
++i;
221+
}
222+
ans[j] = find(pj) == find(qj);
223+
}
224+
return ans;
225+
}
226+
227+
int find(int x) {
228+
if (p[x] != x) p[x] = find(p[x]);
229+
return p[x];
230+
}
231+
};
232+
```
233+
234+
### **Go**
235+
236+
```go
237+
var p []int
238+
239+
func distanceLimitedPathsExist(n int, edgeList [][]int, queries [][]int) []bool {
240+
p = make([]int, n)
241+
for i := 0; i < n; i++ {
242+
p[i] = i
243+
}
244+
sort.Slice(edgeList, func(i, j int) bool {
245+
return edgeList[i][2] < edgeList[j][2]
246+
})
247+
m := len(queries)
248+
indexes := make([]int, m)
249+
for i := 0; i < m; i++ {
250+
indexes[i] = i
251+
}
252+
sort.Slice(indexes, func(i, j int) bool {
253+
return queries[indexes[i]][2] < queries[indexes[j]][2]
254+
})
255+
ans := make([]bool, m)
256+
i := 0
257+
for _, j := range indexes {
258+
pj, qj, limit := queries[j][0], queries[j][1], queries[j][2]
259+
for i < len(edgeList) && edgeList[i][2] < limit {
260+
u, v := edgeList[i][0], edgeList[i][1]
261+
p[find(u)] = find(v)
262+
i++
263+
}
264+
ans[j] = find(pj) == find(qj)
265+
}
266+
return ans
267+
}
71268
269+
func find(x int) int {
270+
if p[x] != x {
271+
p[x] = find(p[x])
272+
}
273+
return p[x]
274+
}
72275
```
73276

74277
### **...**

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

+144-2
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,163 @@ For the second query, there is a path (0 -&gt; 1 -&gt; 2) of two edges with dist
4444
<li>There may be <strong>multiple</strong> edges between two nodes.</li>
4545
</ul>
4646

47-
4847
## Solutions
4948

5049
<!-- tabs:start -->
5150

5251
### **Python3**
5352

5453
```python
55-
54+
class Solution:
55+
def distanceLimitedPathsExist(self, n: int, edgeList: List[List[int]], queries: List[List[int]]) -> List[bool]:
56+
p = list(range(n))
57+
58+
def find(x):
59+
if p[x] != x:
60+
p[x] = find(p[x])
61+
return p[x]
62+
63+
edgeList.sort(key=lambda x: x[2])
64+
m = len(queries)
65+
indexes = list(range(m))
66+
indexes.sort(key=lambda x: queries[x][2])
67+
ans = [False] * m
68+
i = 0
69+
for j in indexes:
70+
pj, qj, limit = queries[j][0], queries[j][1], queries[j][2]
71+
while i < len(edgeList) and edgeList[i][2] < limit:
72+
u, v = edgeList[i][0], edgeList[i][1]
73+
p[find(u)] = find(v)
74+
i += 1
75+
ans[j] = find(pj) == find(qj)
76+
return ans
5677
```
5778

5879
### **Java**
5980

6081
```java
82+
class Solution {
83+
private int[] p;
84+
85+
public boolean[] distanceLimitedPathsExist(int n, int[][] edgeList, int[][] queries) {
86+
p = new int[n];
87+
for (int i = 0; i < n; ++i) {
88+
p[i] = i;
89+
}
90+
int m = queries.length;
91+
Integer[] indexes = new Integer[m];
92+
for (int i = 0; i < m; ++i) {
93+
indexes[i] = i;
94+
}
95+
Arrays.sort(indexes, Comparator.comparingInt(i -> queries[i][2]));
96+
Arrays.sort(edgeList, Comparator.comparingInt(a -> a[2]));
97+
boolean[] ans = new boolean[m];
98+
int i = 0;
99+
for (int j : indexes) {
100+
int pj = queries[j][0], qj = queries[j][1], limit = queries[j][2];
101+
while (i < edgeList.length && edgeList[i][2] < limit) {
102+
int u = edgeList[i][0], v = edgeList[i][1];
103+
p[find(u)] = find(v);
104+
++i;
105+
}
106+
ans[j] = find(pj) == find(qj);
107+
}
108+
return ans;
109+
}
110+
111+
private int find(int x) {
112+
if (p[x] != x) {
113+
p[x] = find(p[x]);
114+
}
115+
return p[x];
116+
}
117+
}
118+
```
119+
120+
### **C++**
121+
122+
```cpp
123+
class Solution {
124+
public:
125+
vector<int> p;
126+
127+
vector<bool> distanceLimitedPathsExist(int n, vector<vector<int>>& edgeList, vector<vector<int>>& queries) {
128+
p.resize(n);
129+
for (int i = 0; i < n; ++i) p[i] = i;
130+
sort(edgeList.begin(), edgeList.end(), [](const auto& e1, const auto& e2) {
131+
return e1[2] < e2[2];
132+
});
133+
int m = queries.size();
134+
vector<int> indexes(m);
135+
for (int i = 0; i < m; ++i) indexes[i] = i;
136+
sort(indexes.begin(), indexes.end(), [&](int i, int j) {
137+
return queries[i][2] < queries[j][2];
138+
});
139+
140+
vector<bool> ans(m, false);
141+
int i = 0;
142+
for (int j : indexes)
143+
{
144+
int pj = queries[j][0], qj = queries[j][1], limit = queries[j][2];
145+
while (i < edgeList.size() && edgeList[i][2] < limit)
146+
{
147+
int u = edgeList[i][0], v = edgeList[i][1];
148+
p[find(u)] = find(v);
149+
++i;
150+
}
151+
ans[j] = find(pj) == find(qj);
152+
}
153+
return ans;
154+
}
155+
156+
int find(int x) {
157+
if (p[x] != x) p[x] = find(p[x]);
158+
return p[x];
159+
}
160+
};
161+
```
61162
163+
### **Go**
164+
165+
```go
166+
var p []int
167+
168+
func distanceLimitedPathsExist(n int, edgeList [][]int, queries [][]int) []bool {
169+
p = make([]int, n)
170+
for i := 0; i < n; i++ {
171+
p[i] = i
172+
}
173+
sort.Slice(edgeList, func(i, j int) bool {
174+
return edgeList[i][2] < edgeList[j][2]
175+
})
176+
m := len(queries)
177+
indexes := make([]int, m)
178+
for i := 0; i < m; i++ {
179+
indexes[i] = i
180+
}
181+
sort.Slice(indexes, func(i, j int) bool {
182+
return queries[indexes[i]][2] < queries[indexes[j]][2]
183+
})
184+
ans := make([]bool, m)
185+
i := 0
186+
for _, j := range indexes {
187+
pj, qj, limit := queries[j][0], queries[j][1], queries[j][2]
188+
for i < len(edgeList) && edgeList[i][2] < limit {
189+
u, v := edgeList[i][0], edgeList[i][1]
190+
p[find(u)] = find(v)
191+
i++
192+
}
193+
ans[j] = find(pj) == find(qj)
194+
}
195+
return ans
196+
}
197+
198+
func find(x int) int {
199+
if p[x] != x {
200+
p[x] = find(p[x])
201+
}
202+
return p[x]
203+
}
62204
```
63205

64206
### **...**

0 commit comments

Comments
 (0)