by AskAI

Solutions:

Check these solutions from our community and artificial intelligence:
The candidate's solution is correct and demonstrates a good understanding of depth first search. However, the code could be more concise. For example, the has_cycle function could be written as follows: def has_cycle(g): visited = set() for node in g: if node not in visited: if dfs(node, visited, None): return 1 return 0 def dfs(node, visited, parent): visited.add(node) for neighbor in node: if neighbor not in visited: if dfs(neighbor, visited, node): return True elif neighbor != parent: return True return False