Please subscribe to the official Codeforces channel in Telegram via the link: ×

FutureIntelli's blog

By FutureIntelli, history, 3 months ago, In English,

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

Thanks in advance!


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