must use python

Community Answer

【General guidance】The answer provided below has been developed in a clear step by step manner.Step1/2Answer:-To detect a cycle in a directed graph, we can use the Depth-First Search (DFS) algorithm. We will perform a DFS on each unvisited node of the graph and while exploring the graph, if we encounter a node that is already visited, then there exists a cycle in the graph.Algorithm:Create a graph using the given inputs.Create a visited set to keep track of the visited nodes.For each unvisited node in the graph, perform a DFS.While performing the DFS, if a node is already visited, then there exists a cycle in the graph. Return "YES".If we have explored all the nodes and not found any cycle, then return "NO".Explanation:Please refer to solution in this step.Step2/2The complete python code along with comments is given below:# Define a function to check if a given node in a graph has a cycledef has_cycle(graph, node, visited, rec_stack): # Add the node to the visited set and the recursion stack visited.add(node) rec_stack.add(node) # Check each neighbor of the node for neighbor in graph[node]: # If the neighbor hasn't been visited yet, check if it has a cycle if neighbor not in visited: if has_cycle(graph, neighbor, visited, rec_stack): return True # If the neighbor is already in the recursion stack, there is a cycle elif neighbor in rec_stack: return True # Remove the node from the recursion stack and return False rec_stack.remove(node) return Falseprint("Enter the input: ")# Read in the number of cities (n) and roads (m)n, m = map(int, input().split())# Check that n and m are valid (i.e., between 1 and 100, incl ... See the full answer