Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import math
- # Auto-generated code below aims at helping you parse
- # the standard input according to the problem statement.
- network = dict()
- n = int(input()) # the number of adjacency relations
- for i in range(n):
- # xi: the ID of a person which is adjacent to yi
- # yi: the ID of a person which is adjacent to xi
- xi, yi = [int(j) for j in input().split()]
- if xi not in network.keys():
- network[xi] = [yi]
- else:
- network[xi].append(yi)
- if yi not in network.keys():
- network[yi] = [xi]
- else:
- network[yi].append(xi)
- print(network, file = sys.stderr)
- # Write an action using print
- # To debug: print("Debug messages...", file=sys.stderr)
- # The minimal amount of steps required to completely propagate the advertisement
- def find_path(start):
- """This function determines the number of steps required to completely propagate a message
- across a network from a starting node position"""
- visited = []
- step = 0
- roots = [start]
- # Until all the nodes in the network have been visited
- while len(visited) != len(network):
- children = []
- # Only count nodes that are not in visited
- roots = list(set(roots) - set(visited))
- visited.extend(roots)
- # Find children for every root node
- for root in roots:
- child = network[root]
- children.extend(child)
- # If one child is not in visited => new link => add step
- if len(list(set(children).intersection(set(visited)))) > 0:
- step += 1
- roots.extend(list(set(children) - set(visited)))
- return step
- steps_list = []
- # Go through all keys in network to find the minimum amount of steps
- for key in network.keys():
- steps = find_path(key)
- steps_list.append(steps)
- print(min(steps_list))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement