-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem1192.py
More file actions
45 lines (38 loc) · 1 KB
/
problem1192.py
File metadata and controls
45 lines (38 loc) · 1 KB
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
class Solution(object):
def criticalConnections(self, n, connections):
"""
:type n: int
:type connections: List[List[int]]
:rtype: List[List[int]]
"""
import collections
self.cur = 0
low = [None for i in range(n)]
dfn = [None for i in range(n)]
graph = collections.defaultdict(list)
for i,k in connections:
graph[i].append(k)
graph[k].append(i)
def dfs(node, parent):
if dfn[node] == None:
dfn[node] = self.cur
low[node] = self.cur
self.cur += 1
for nextNode in graph[node]:
if dfn[nextNode] == None:
dfs(nextNode, node)
if parent != None:
lowValue = min([low[i] for i in graph[node] if i != parent] + [low[node]])
else:
lowValue = min([low[i] for i in graph[node]] + [low[node]])
low[node] = lowValue
dfs(0, None)
res = []
for v in connections:
if low[v[0]]>dfn[v[1]] or low[v[1]]>dfn[v[0]]:
res.append(v)
return res
s = Solution()
n = 4
connections = [[0,1],[1,2],[2,0],[1,3]]
print s.criticalConnections(n, connections)