1697. Checking Existence of Edge Length Limited Paths
On This Page
class Solution:
def distanceLimitedPathsExist(
self, n: int, edgeList: List[List[int]], queries: List[List[int]]
) -> List[bool]:
def find(x):
if p[x] != x:
p[x] = find(p[x])
return p[x]
p = list(range(n))
edgeList.sort(key=lambda x: x[2])
j = 0
res = [False] * len(queries)
for i, (a, b, limit) in sorted(enumerate(queries), key=lambda x: x[1][2]):
while j < len(edgeList) and edgeList[j][2] < limit:
u, v, _ = edgeList[j]
p[find(u)] = find(v)
j += 1
res[i] = find(a) == find(b)
return res