-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreakingbad.py
76 lines (60 loc) · 1.51 KB
/
breakingbad.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
68
69
70
71
72
73
74
75
76
# bipartite graph partitioning, 2-coloring, breadth first search, bfs
# For BFS
from queue import Queue
n = int(input())
# Names to ints and vice-versa
names = {}
names_rev = []
# Edge list int->[int]
edges = {}
vertices = set()
# Initialize forward, backward list of names
for i in range(n):
current = input()
names[current] = i
names_rev.append(current)
# Initialize colors
color = [float('inf')] * len(names)
m = int(input())
start = ""
# Initialize adjacency list (both directions)
for i in range(m):
(fst, snd) = input().split()
fst = names[fst]
snd = names[snd]
if not fst in edges:
edges[fst] = []
if not snd in edges:
edges[snd] = []
edges[fst].append(snd)
edges[snd].append(fst)
vertices.add(fst)
# BFS starting with every fst in (fst, snd)
for e in edges:
# color is assigned
if color[e] != float('inf'):
continue
q = Queue()
q.put(e)
color[e] = 0
# perform BFS
while not q.empty():
fst = q.get()
for snd in edges[fst]:
if color[snd] == float('inf'):
color[snd] = 1 - color[fst]
q.put(snd)
elif color[fst] == color[snd]:
# failure
print("impossible")
exit()
# Print Walter's list
for i in range(len(color)):
if color[i] == 0:
print(names_rev[i], end=' ')
print()
# Jesse gets everything else
for i in range(len(color)):
if color[i] != 0:
print(names_rev[i], end=' ')
print()