Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. def is_ancestor(man, older_man):
  2.     if man == older_man:
  3.         return True
  4.     while man in p_tree:
  5.         man = p_tree[man]
  6.         if man == older_man:
  7.             return True
  8.     return False
  9.      
  10. f = open("input.txt")
  11.  
  12. p_tree = dict()
  13. line = f.readline()
  14. n = int(line)
  15. line = f.readline()
  16. for i in range(n - 1):
  17.     child, parent = line.split()
  18.     p_tree[child] = parent
  19.     line = f.readline()
  20.  
  21. result = ''
  22.  
  23. while line:
  24.     first, second = line.split()
  25.     if is_ancestor(second, first):
  26.         result += ' 1'
  27.     elif is_ancestor(first, second):
  28.         result += ' 2'
  29.     else:
  30.         result += ' 0'
  31.     line = f.readline()
  32.  
  33. res = open("output.txt", 'w')
  34. res.write(result[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement