-
Notifications
You must be signed in to change notification settings - Fork 481
/
1129.py
23 lines (20 loc) · 832 Bytes
/
1129.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution:
def shortestAlternatingPaths(self, n: int, red_edges: List[List[int]], blue_edges: List[List[int]]) -> List[int]:
g0, g1 = collections.defaultdict(list), collections.defaultdict(list)
for i, j in red_edges:
g0[i].append(j)
for i, j in blue_edges:
g1[i].append(j)
g = [g0, g1]
res = [float("inf")] * n
q = [(0, 0, 0), (0, 1, 0)]
vis = {(0, 0), (0, 1)}
while len(q):
node, color, level = q.pop(0)
res[node] = min(res[node], level)
nc = not color
for nd in g[nc][node]:
if (nd, nc) not in vis:
q.append((nd, nc, level + 1))
vis.add((nd, nc))
return [-1 if i == float("inf") else i for i in res]