-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathdistance-to-a-cycle-in-undirected-graph.py
54 lines (50 loc) · 1.48 KB
/
distance-to-a-cycle-in-undirected-graph.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
# Time: O(|V| + |E|)
# Space: O(|V| + |E|)
# graph, dfs, bfs
class Solution(object):
def distanceToCycle(self, n, edges):
"""
:type n: int
:type edges: List[List[int]]
:rtype: List[int]
"""
def cycle(parent, v, u):
result = [parent[v], v]
while u != parent[v]:
result.append(u)
u = parent[u]
return result
def iter_dfs(adj):
stk = [0]
parent = [-2]*len(adj)
parent[0] = -1
while stk:
u = stk.pop()
for v in reversed(adj[u]):
if parent[v] != -2:
if v == parent[u]:
continue
return cycle(parent, v, u)
parent[v] = u
stk.append(v)
def bfs(adj, q):
result = [-1]*n
for x in q:
result[x] = 0
d = 1
while q:
new_q = []
for u in q:
for v in adj[u]:
if result[v] != -1:
continue
result[v] = d
new_q.append(v)
q = new_q
d += 1
return result
adj = [[] for _ in xrange(n)]
for u, v in edges:
adj[u].append(v)
adj[v].append(u)
return bfs(adj, iter_dfs(adj))