monsij's blog

By monsij, history, 6 years ago, In English

Any approach to solve this problem. As expected making dfs calls from every vertex results in TLE

Thanks in advance!

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
6 years ago, # |
  Vote: I like it +1 Vote: I do not like it

calculate k[i]th ancestor of each node and subtract r[i`] from that node and store in arr[], simultaneously add r[i] to the current node. Now population of each country can be calculated using another dfs by that additional array arr[]