Editorial for DMOPC '17 Contest 2 P3 - Bad Bunnies


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: r3mark

For the first subtask, brute force to find the distance from each node to each rabbit. Then, check along the path from X to Y to find the answer.

Time Complexity: \mathcal O(RN)

For the second subtask, compute the path from X to Y. Partition the tree by deleting the edges of the path. Several subtrees rooted from nodes on the path are created. Compute the minimum distance from each node of the path to a rabbit in its subtree using simple tree DP. The final answer is the minimum of these values.

Time Complexity: \mathcal O(R+N)

Additional Challenge: How would you solve the problem if you were given Q queries, the i^\text{th} of which has X_i and Y_i as the start and endpoints, respectively, of the path?


Comments


  • 0
    RKS_The_Great  commented on Nov. 23, 2017, 11:50 a.m. edited

    Can someone give a hint on how to solve the additional challenge. I have somewhere read that using 2 dfs, this can be achieved but HOW??


    • 3
      Evan_Yu123  commented on Nov. 23, 2017, 1:56 p.m.

      Idk if this is an overkill, but you can precompute each node's distance to the closest rabbit, then use a modified lca to answer the queries, kinda like distribution channel.


      • 1
        Kirito  commented on Nov. 23, 2017, 3:53 p.m.

        That is indeed the correct solution.