-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
45 lines (37 loc) · 1.26 KB
/
bot.py
File metadata and controls
45 lines (37 loc) · 1.26 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
#!/usr/bin/env python3
def get_forced_edge(graph, closed, vertex):
print(graph)
for other_vertex in graph[abs(vertex)]:
if other_vertex < 0 and other_vertex not in closed:
return other_vertex
return None
def number_of_dead_ends(graph, start, opened, closed):
glitch = 0
end = 0
while len(opened) > 0:
current = abs(opened.pop())
if glitch == 0:
for glitch_edge in filter(lambda e: e > 0, graph[current]):
opened.append(glitch_edge)
number_of_dead_ends(graph, current, opened, closed)
glitch += 1
elif get_forced_edge(graph, closed, current) is None:
end += 1
else:
for forced_edge in filter(lambda e: e < 0, graph[current]):
opened.append(forced_edge)
closed.append(current)
return end
case_amt = int(input())
for _ in range(case_amt):
[vertex_amt, edge_amt] = map(int, input().split(' '))
graph = {}
for i in range(edge_amt):
graph[i] = []
for i in range(edge_amt):
[a, b] = map(int, input().split(' '))
if a < 0:
graph[-a].append(-b)
else:
graph[a].append(b)
print(number_of_dead_ends(graph, 1, [1], []))