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] ) 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 @lru_cache def shortestPathDFS(path, end): current_node = path[-1] # first check if done if current_node == end: return path best_path = None best_length = math.inf # a big number # next check actions for next_node in current_node.links: # up, down, right, left # make sure it is a valid move if next_node not in path: # use path[-3:] instead of 'path' to speed things up if you know that there are no infiite loops. full_path = shortestPathDFS( path + (next_node, ), end ) if full_path is not None and full_path[-1] == end and len( full_path ) < best_length: best_path = full_path best_length = len( full_path ) return best_path def shortestPathDFSNoRecursion( start, end ): path = [] stack = [] stack.append( start ) best_path = None best_length = math.inf # a big number while stack: current_node = stack.pop() if current_node == end: if len(path) < best_length: print(path, file=sys.stderr, flush=True) print(len(path), file=sys.stderr, flush=True) print(best_length, file=sys.stderr, flush=True) best_length = len(path) best_path = path[:] if current_node in path: continue path.append( current_node ) for next_node in current_node.links: stack.append( next_node ) 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) for node in nodes: if node.is_exit_gateway: path = shortestPathDFSNoRecursion( node, bobnet_node ) path[0].links.remove( path[1] ) path[1].links.remove( path[0] ) print( str( path[0] ) + ' ' + str( path[1] ) ) ''' paths = [] for node in nodes: if node.is_exit_gateway: path = shortestPathDFS( node, bobnet_node ) paths.append(path) print(len(paths), file=sys.stderr, flush=True) for path in paths: print( str( path[0] ) + ' ' + str( path[1] ) ) break ''' #for node in nodes[ bobnet_agent_node_index ].links: # if nodes[ node.index ].is_exit_gateway: # target_node = nodes[ node.index ] # Example: 0 1 are the indices of the nodes you wish to sever the link between #print( str( nodes[ bobnet_agent_node_index ] ) + " " + str( target_node ) )