-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path1724-checking-existence-of-edge-length-limited-paths-ii.py
66 lines (58 loc) · 2.25 KB
/
1724-checking-existence-of-edge-length-limited-paths-ii.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class UnionFind:
def __init__(self, n):
self.parent = [i for i in range(n)]
self.rank = [0 for _ in range(n)]
def find(self, p):
while p != self.parent[p]:
self.parent[p] = self.parent[self.parent[p]]
p = self.parent[p]
return p
def union(self, p, q):
root_p = self.find(p)
root_q = self.find(q)
if root_p == root_q:
return
if self.rank[root_p] > self.rank[root_q]:
self.parent[root_q] = root_p
elif self.rank[root_p] < self.rank[root_q]:
self.parent[root_p] = root_q
else:
self.parent[root_p] = root_q
self.rank[root_q] += 1
def is_connected(self, p, q):
return self.find(p) == self.find(q)
from collections import defaultdict
class DistanceLimitedPathsExist:
def __init__(self, n: int, edgeList: List[List[int]]):
self.edges = sorted(edgeList, key = lambda x: x[2])
self.limit_parent = defaultdict(list)
self.uf = UnionFind(n)
for u, v, w in self.edges:
if w not in self.limit_parent:
self.limit_parent[w] = self.uf.parent[:]
if not self.uf.is_connected(u, v):
self.uf.union(u, v)
self.limit_parent[float('inf')] = self.uf.parent[:]
self.limits = list(self.limit_parent.keys())
def query(self, p: int, q: int, limit: int) -> bool:
left, right, boundary = 0, len(self.limits) - 1, - 1
while left <= right:
m = (left + right) // 2
if limit == self.limits[m]:
boundary = m
break
elif limit > self.limits[m]:
boundary = m + 1
left = m + 2
else:
right = m - 1
if boundary == - 1:
return False
self.uf.parent = self.limit_parent[self.limits[boundary]]
return self.uf.is_connected(p, q)
# Your DistanceLimitedPathsExist object will be instantiated and called as such:
# obj = DistanceLimitedPathsExist(n, edgeList)
# param_1 = obj.query(p,q,limit)
# time O(ElogE + EV) for init, O(logE) for query()
# space O(EV) for init, O(1) for query()
# using graph and kruskal and mst and union find and binary search and hashmap