I took the BFS function from here .
from collections import defaultdict
from queue import Queue
def read_nodes(pairs_number):
for _ in range(pairs_number):
yield map(int, input().split())
def parse_input(tree):
nodes_count, pairs_number, capital = map(int, input().split())
for node1, node2 in read_nodes(pairs_number):
tree[node1].append(node2)
tree[node2].append(node1)
return tree, capital, nodes_count
def bfstraverse_path(graphfromNode, starttoNode, endnodes):
q =def QueuegetNeighbours(current, nodes):
path = [start] return nodes[current] if current in nodes else []
q.putdef make_path(pathtoNode, graph):
visited result = set([start])
[]
while not'Root' q!= toNode:
result.emptyappend(toNode):
path toNode = qgraph[toNode]
result.getreverse()
last_nodereturn result
q = path[-1]Queue()
q.put(fromNode)
if last_nodegraph === end{fromNode: 'Root'}
while not q.empty():
return path current = q.get()
for nodeneighbor in graph[last_node]getNeighbours(current, nodes):
if nodeneighbor not in visitedgraph:
visited.add(node)graph[neighbor] = current
q.put(pathneighbor)
+ [node] if current == toNode:
return make_path(toNode, graph)
return []
def distant_sites(graph_dic, capital, nodes_count):
distance_with_connections = defaultdict(list)
node_distance = {}
for towns in range(nodes_count):
towns_distance = len(bfstraverse_path(graph_dic, capital, towns, graph_dic)) - 1
node_distance[towns] = (towns_distance)
for town1, neighbours in graph_dic.items():
for town2 in neighbours:
if town1 > town2:
for neighbour_of_town2 in graph_dic[town2]:
if town2 > neighbour_of_town2:
if neighbour_of_town2 in neighbours:
town_connection = (town1, town2, neighbour_of_town2)
distance = (node_distance[town1]) + (node_distance[town2]) + (node_distance[neighbour_of_town2])
distance_with_connections[distance].append(town_connection)
max_distance = max(distance_with_connections)
return max_distance, len(distance_with_connections[max_distance])
if __name__ == '__main__':
graph_dic, capital, nodes_count = parse_input(defaultdict(list))
max_distance, number_of_connections = distance_sites(graph_dic, capital, nodes_count)
print(max_distance, number_of_connections)
from collections import defaultdict
from queue import Queue
def read_nodes(pairs_number):
for _ in range(pairs_number):
yield map(int, input().split())
def parse_input(tree):
nodes_count, pairs_number, capital = map(int, input().split())
for node1, node2 in read_nodes(pairs_number):
tree[node1].append(node2)
tree[node2].append(node1)
return tree, capital, nodes_count
def bfs(graph, start, end):
q = Queue()
path = [start]
q.put(path)
visited = set([start])
while not q.empty():
path = q.get()
last_node = path[-1]
if last_node == end:
return path
for node in graph[last_node]:
if node not in visited:
visited.add(node)
q.put(path + [node])
def distant_sites(graph_dic, capital, nodes_count):
distance_with_connections = defaultdict(list)
node_distance = {}
for towns in range(nodes_count):
towns_distance = len(bfs(graph_dic, capital, towns)) - 1
node_distance[towns] = (towns_distance)
for town1, neighbours in graph_dic.items():
for town2 in neighbours:
if town1 > town2:
for neighbour_of_town2 in graph_dic[town2]:
if town2 > neighbour_of_town2:
if neighbour_of_town2 in neighbours:
town_connection = (town1, town2, neighbour_of_town2)
distance = (node_distance[town1]) + (node_distance[town2]) + (node_distance[neighbour_of_town2])
distance_with_connections[distance].append(town_connection)
max_distance = max(distance_with_connections)
return max_distance, len(distance_with_connections[max_distance])
if __name__ == '__main__':
graph_dic, capital, nodes_count = parse_input(defaultdict(list))
max_distance, number_of_connections = distance_sites(graph_dic, capital, nodes_count)
print(max_distance, number_of_connections)
I took the BFS function from here .
from collections import defaultdict
from queue import Queue
def read_nodes(pairs_number):
for _ in range(pairs_number):
yield map(int, input().split())
def parse_input(tree):
nodes_count, pairs_number, capital = map(int, input().split())
for node1, node2 in read_nodes(pairs_number):
tree[node1].append(node2)
tree[node2].append(node1)
return tree, capital, nodes_count
def traverse_path(fromNode, toNode, nodes):
def getNeighbours(current, nodes):
return nodes[current] if current in nodes else []
def make_path(toNode, graph):
result = []
while 'Root' != toNode:
result.append(toNode)
toNode = graph[toNode]
result.reverse()
return result
q = Queue()
q.put(fromNode)
graph = {fromNode: 'Root'}
while not q.empty():
current = q.get()
for neighbor in getNeighbours(current, nodes):
if neighbor not in graph:
graph[neighbor] = current
q.put(neighbor)
if current == toNode:
return make_path(toNode, graph)
return []
def distant_sites(graph_dic, capital, nodes_count):
distance_with_connections = defaultdict(list)
node_distance = {}
for towns in range(nodes_count):
towns_distance = len(traverse_path(capital, towns, graph_dic)) - 1
node_distance[towns] = (towns_distance)
for town1, neighbours in graph_dic.items():
for town2 in neighbours:
if town1 > town2:
for neighbour_of_town2 in graph_dic[town2]:
if town2 > neighbour_of_town2:
if neighbour_of_town2 in neighbours:
town_connection = (town1, town2, neighbour_of_town2)
distance = (node_distance[town1]) + (node_distance[town2]) + (node_distance[neighbour_of_town2])
distance_with_connections[distance].append(town_connection)
max_distance = max(distance_with_connections)
return max_distance, len(distance_with_connections[max_distance])
if __name__ == '__main__':
graph_dic, capital, nodes_count = parse_input(defaultdict(list))
max_distance, number_of_connections = distance_sites(graph_dic, capital, nodes_count)
print(max_distance, number_of_connections)
lang-py