Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from collections import defaultdict
- # values: id, key, child labels (left and right), color
- # parsing input
- nc, root = map(int, input().split())
- tr = {}
- for n in range(nc):
- n = list(map(int, input().split()))
- tr[n[0]] = n
- # building depths
- def bd(id, d):
- global nbd
- n = tr[id]
- nbd[d].append(n)
- if n[2]:
- bd(n[2], d + 1)
- if n[3]:
- bd(n[3], d + 1)
- nbd = defaultdict(list)
- bd(root, 0)
- # calculating
- res = 0
- for nodes in nbd.values():
- lb = None
- lw = None
- for n in nodes:
- if n[4] == 1:
- ln, lb = lb, n
- else:
- ln, lw = lw, n
- if ln != None and ln[1] == n[1]:
- res += 1
- # output result
- print(res)
Advertisement
Add Comment
Please, Sign In to add comment