-
Notifications
You must be signed in to change notification settings - Fork 17
/
redundant-connection-ii.py
67 lines (49 loc) · 2.1 KB
/
redundant-connection-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
67
from typing import List, DefaultDict, Set, Tuple, Dict
from enum import Enum
from collections import defaultdict
class Color(Enum):
WHITE = 0
GRAY = 1
BLACK = 2
class Solution:
def findRedundantDirectedConnection(self, edges: List[List[int]]) -> List[int]:
def create_adj_list(edges: List[List[int]]) -> DefaultDict[int, List[int]]:
adj_list: DefaultDict[int, List[int]] = defaultdict(list)
for src, dst in edges:
adj_list[src].append(dst)
adj_list[dst]
return adj_list
def dfs(vertex: int) -> None:
colors[vertex] = Color.GRAY
for adj_vertex in adj_list[vertex]:
if colors[adj_vertex] == Color.GRAY:
back_edges.add((vertex, adj_vertex))
elif colors[adj_vertex] == Color.BLACK:
cross_edges.add((vertex, adj_vertex))
else:
dfs(adj_vertex)
colors[vertex] = Color.BLACK
possible_edges: Set[Tuple[int, int]] = set()
back_edges: Set[Tuple[int, int]] = set()
cross_edges: Set[Tuple[int, int]] = set()
colors: Dict[int, Color] = {}
adj_list = create_adj_list(edges)
for vertex in adj_list.keys():
colors = {key: Color.WHITE for key in adj_list.keys()}
back_edges = set()
cross_edges = set()
dfs(vertex)
if all(c == Color.BLACK for c in colors.values()):
possible_edges = possible_edges | back_edges | cross_edges
adj_list = create_adj_list(list(reversed(edges)))
for vertex in adj_list.keys():
back_edges = set()
cross_edges = set()
colors = {key: Color.WHITE for key in adj_list.keys()}
dfs(vertex)
if all(c == Color.BLACK for c in colors.values()):
possible_edges = possible_edges | back_edges | cross_edges
for src, dst in reversed(edges):
if (src, dst) in possible_edges:
return [src, dst]
raise RuntimeError("No edges to remove")