Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.85 KB | None | 0 0
  1. import sys
  2. import math
  3.  
  4. # Auto-generated code below aims at helping you parse
  5. # the standard input according to the problem statement.
  6. network = dict()
  7.  
  8. n = int(input())  # the number of adjacency relations
  9. for i in range(n):
  10.     # xi: the ID of a person which is adjacent to yi
  11.     # yi: the ID of a person which is adjacent to xi
  12.     xi, yi = [int(j) for j in input().split()]
  13.     if xi not in network.keys():
  14.         network[xi] = [yi]
  15.     else:
  16.         network[xi].append(yi)
  17.     if yi not in network.keys():
  18.         network[yi] = [xi]
  19.     else:
  20.         network[yi].append(xi)
  21.        
  22. print(network, file = sys.stderr)
  23.  
  24. # Write an action using print
  25. # To debug: print("Debug messages...", file=sys.stderr)
  26. # The minimal amount of steps required to completely propagate the advertisement
  27.  
  28. def find_path(start):
  29.     """This function determines the number of steps required to completely propagate a message
  30.    across a network from a starting node position"""
  31.  
  32.     visited = []
  33.     step = 0
  34.     roots = [start]
  35.  
  36.     # Until all the nodes in the network have been visited
  37.     while len(visited) != len(network):
  38.         children = []
  39.  
  40.         # Only count nodes that are not in visited
  41.         roots = list(set(roots) - set(visited))
  42.         visited.extend(roots)
  43.  
  44.         # Find children for every root node
  45.         for root in roots:
  46.             child = network[root]
  47.             children.extend(child)
  48.  
  49.         # If one child is not in visited => new link => add step
  50.         if len(list(set(children).intersection(set(visited)))) > 0:
  51.             step += 1
  52.  
  53.         roots.extend(list(set(children) - set(visited)))
  54.  
  55.     return step
  56.  
  57.  
  58. steps_list = []
  59. # Go through all keys in network to find the minimum amount of steps
  60. for key in network.keys():
  61.     steps = find_path(key)
  62.     steps_list.append(steps)
  63.  
  64. print(min(steps_list))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement