Return immediately if edge starts and finishes in the same node

This commit is contained in:
Eryk Miszczuk 2019-12-13 15:01:42 +01:00
parent 2efbb9182d
commit 6ae119a40b

View File

@ -26,6 +26,9 @@ def dfs(graph, start):
while stack: while stack:
edge = stack.pop() edge = stack.pop()
#print('Current edge: {edge}'.format(edge = edge)) #print('Current edge: {edge}'.format(edge = edge))
#return immediately if cycle is detected
if edge[0] == edge[2]:
return [-1]
if edge not in visited: if edge not in visited:
visited.add(edge) visited.add(edge)
children = set() children = set()
@ -37,7 +40,7 @@ def dfs(graph, start):
return [-1] return [-1]
return visited return visited
# load atand format from stdin
for line in sys.stdin: for line in sys.stdin:
line = line.replace('\n', '') line = line.replace('\n', '')
move = atandt_desc_pattern.match(line) move = atandt_desc_pattern.match(line)