Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def is_ancestor(man, older_man):
- if man == older_man:
- return True
- while man in p_tree:
- man = p_tree[man]
- if man == older_man:
- return True
- return False
- f = open("input.txt")
- p_tree = dict()
- line = f.readline()
- n = int(line)
- line = f.readline()
- for i in range(n - 1):
- child, parent = line.split()
- p_tree[child] = parent
- line = f.readline()
- result = ''
- while line:
- first, second = line.split()
- if is_ancestor(second, first):
- result += ' 1'
- elif is_ancestor(first, second):
- result += ' 2'
- else:
- result += ' 0'
- line = f.readline()
- res = open("output.txt", 'w')
- res.write(result[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement