-
Notifications
You must be signed in to change notification settings - Fork 0
/
topo_sort.py
executable file
·116 lines (93 loc) · 2.9 KB
/
topo_sort.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import sys
class Graf:
def __init__(self, stevk): # konstruktor
# self.graf = defaultdict(list)
self.V = stevk
self.graf = {}
for i in range(self.V):
self.graf[i+1] = []
self.visited = []
self.queue = []
self.developing = []
self.cycle = False
def dodaj(self, od, do): # doda povezavo
self.graf[od].append(do)
def topo_sort(self): # zalaufa algo
for u in self.graf:
if u not in self.visited:
self.razvij(u)
if self.cycle:
print("nemogoce")
return -1
return self.queue
def razvij(self, u):
self.developing.append(u)
if self.cycle:
return
for v in self.graf[u]:
if v not in self.visited and v not in self.developing:
self.razvij(v)
elif v in self.developing:
self.cycle = True
break
self.queue.insert(0, u)
self.developing.remove(u)
self.visited.append(u)
def find_max_digit(i, g, max, queue):
index = queue[i]
if i >= g.V - 1:
return
for e in g.graf[index]:
if max[e-1] >= max[index - 1]:
max[e-1] = max[index - 1] - 1
find_max_digit(i + 1, g, max, queue)
def find_min_digit(i, g, queue, value=1):
tmp = value
for e in g.graf[i]:
new = find_min_digit(e, g, queue, value + 1)
if new > tmp:
tmp = new
return tmp
def find_numbers(q, graf):
max_num = [9] * graf.V
min_num = [1] * graf.V
for i in range(graf.V):
find_max_digit(i, graf, max_num, q)
min_num[q[i] - 1] = find_min_digit(q[i], graf, q)
if all(0 < i < 10 for i in max_num):
return max_num, min_num
else:
return -1, -1
examples = 0
count = 0
conditions = 0
graph = None
for line in sys.stdin:
line = line.rstrip()
chars = line.split(" ")
if examples == 0:
examples = int(line)
continue
if ">" not in chars and "<" not in chars: # nov graf
count = 0
graph = Graf(int(chars[0]))
conditions = int(chars[1])
elif ">" in chars and count < conditions: # dodamo povezavo od do
count += 1
graph.dodaj(int(chars[0]), int(chars[2]))
elif "<" in chars and count < conditions: # dodamo povezavo do od
count += 1
graph.dodaj(int(chars[2]), int(chars[0]))
if count == conditions: # zalaufamo algo
count = 0
queue = graph.topo_sort()
if queue != -1:
max, min = find_numbers(queue, graph)
if max != -1 and min != -1:
max = map(str, max)
min = map(str, min)
# print("Min number: " + ''.join(min))
# print("Max number: " + ''.join(max))
print(''.join(min) + " " + ''.join(max))
else:
print("nemogoce")