From 6ae119a40b7ee9021e29e37903aec904ced3fa49 Mon Sep 17 00:00:00 2001 From: Eryk Miszczuk Date: Fri, 13 Dec 2019 15:01:42 +0100 Subject: [PATCH] Return immediately if edge starts and finishes in the same node --- TaskB05/run.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/TaskB05/run.py b/TaskB05/run.py index 440c8d2..a13bcae 100644 --- a/TaskB05/run.py +++ b/TaskB05/run.py @@ -26,6 +26,9 @@ def dfs(graph, start): while stack: edge = stack.pop() #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: visited.add(edge) children = set() @@ -37,7 +40,7 @@ def dfs(graph, start): return [-1] return visited - +# load atand format from stdin for line in sys.stdin: line = line.replace('\n', '') move = atandt_desc_pattern.match(line)