gmo61520

Untitled

Apr 28th, 2016
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. # values: id, key, child labels (left and right), color
  2.  
  3. # parsing input
  4. nc, root = map(int, input().split())
  5. tr = {}
  6. for n in range(nc):
  7. n = list(map(int, input().split()))
  8. tr[n[0]] = n
  9.  
  10. # building depths
  11. def bd(id, d = 0):
  12. global nbd
  13.  
  14. n = tr[id]
  15. nbd[d].append(n)
  16.  
  17. if n[2]:
  18. bd(n[2], d + 1)
  19. if n[3]:
  20. bd(n[3], d + 1)
  21.  
  22. nbd = {}
  23. bd(root, nbd)
  24.  
  25. # calculating
  26. res = 0
  27. for nodes in nbd.values():
  28. lb = None
  29. lw = None
  30. for n in nodes:
  31. if n[4] == 1:
  32. ln, lb = lb, n
  33. else:
  34. ln, lw = lw, n
  35. if ln != None and ln[1] == n[1]:
  36. res += 1
  37.  
  38. # output result
  39. print(res)
Advertisement
Add Comment
Please, Sign In to add comment