import sys import math from functools import lru_cache # Auto-generated code below aims at helping you parse # the standard input according to the problem statement. class Node(): def __init__(self, index): self.index = index self.links = [] self.is_exit_gateway = False def __repr__(self): return str( self.index ) # number_of_nodes: the total number of nodes in the level, including the gateways # number_of_links: the number of links # number_of_exit_gateways: the number of exit gateways number_of_nodes, number_of_links, number_of_exit_gateways = [int(i) for i in input().split()] nodes = [ Node(index) for index in range(number_of_nodes) ] for _ in range( number_of_links ): # n1: node1 and node2 defines a link between these nodes node1, node2 = [int(j) for j in input().split()] if nodes[node2] not in nodes[node1].links: nodes[node1].links.append( nodes[node2] ) if nodes[node1] not in nodes[node2].links: nodes[node2].links.append( nodes[node1] ) exit_gateways = [] for _ in range( number_of_exit_gateways ): exit_gateway_index = int(input()) # the index of a gateway node nodes[ exit_gateway_index ].is_exit_gateway = True exit_gateways.append( nodes[ exit_gateway_index ] ) @lru_cache def shortestPath( start, end ): stack = [ ( (start,), 0) ] best_path = None best_length = math.inf while stack: current_path, current_direction = stack.pop(0) current_position = current_path[-1] if current_position == end: if len(current_path) < best_length: best_length = len(current_path) best_path = current_path if current_position in current_path[:-1]: continue next_direction = current_direction + 1 if next_direction > 0 and next_direction < len(current_position.links): stack.append( (current_path, next_direction) ) direction = current_position.links[ current_direction ] stack.append( ( current_path + ( direction,), 0) ) return best_path # game loop while True: bobnet_agent_node_index = int(input()) # The index of the node on which the Bobnet agent is positioned this turn bobnet_node = nodes[bobnet_agent_node_index] # Write an action using print # To debug: print("Debug messages...", file=sys.stderr, flush=True) shortest_exit_path = None shortest_exit_distance = math.inf for node in exit_gateways: path = shortestPath( node, bobnet_node ) if len(path) < shortest_exit_distance: shortest_exit_path = path shortest_exit_distance = len(path) shortestPath.cache_clear() if shortest_exit_path: shortest_exit_path[0].links.remove( shortest_exit_path[1] ) shortest_exit_path[1].links.remove( shortest_exit_path[0] ) print( str( shortest_exit_path[0] ) + ' ' + str( shortest_exit_path[1] ) )