Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- graph = {}
- visited = set()
- # cycles = set()
- while True:
- line = input().split('-')
- if "End" == line[0]:
- break
- parent, child = line[0], line[1]
- if parent not in graph:
- graph[parent] = []
- if child not in graph:
- graph[child] = []
- graph[parent].append(child)
- def dfs(node, graph, visited, cycles):
- if node in cycles:
- raise Exception
- if node in visited:
- return
- visited.add(node)
- cycles.add(node)
- for child in graph[node]:
- dfs(child, graph, visited, cycles)
- cycles.remove(node)
- try:
- for node in graph:
- dfs(node, graph, visited, set())
- print('Acyclic: Yes')
- except Exception:
- print('Acyclic: No')
Advertisement
Add Comment
Please, Sign In to add comment