monsij's blog

By monsij, history, 7 months ago, In English,

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

Thanks in advance!


7 months 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[]