Блог пользователя NeverSayNever

Автор NeverSayNever, 12 лет назад, По-английски

Hello all,

I am trying to solve this problem

https://www.hackerrank.com/contests/101hack19/challenges/journey-scheduling

Editorial for this problem mentioned that the query for the longest distance for any given node in a tree can be performed with standard DP approach ... I am not familiar with the approach and also unable to find content regarding this on internet so please can anyone help me understanding this .. or provide me link for good tutorial for this standard approach with reference to some more problem ...

Thanx in advance ..

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
12 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Hi, with second time distance is diametr of tree.

  • »
    »
    12 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Yes you are absolutely correct but actually i need to query first for the longest distance that i can traversed from current position / current node . I want to know approach of finding that .. Please help if you can ..

    • »
      »
      »
      12 лет назад, скрыть # ^ |
       
      Проголосовать: нравится 0 Проголосовать: не нравится

      the longest distance from any node is always to travel to one of diameter ends ,if there are more than one diameter choose any one of them

      • »
        »
        »
        »
        12 лет назад, скрыть # ^ |
         
        Проголосовать: нравится 0 Проголосовать: не нравится

        @kingofnumbers

        Can you please elaborate this a bit more.. I am getting whatever you are saying .. but still need more clarification ..

        • »
          »
          »
          »
          »
          12 лет назад, скрыть # ^ |
           
          Проголосовать: нравится 0 Проголосовать: не нравится

          This is what i have understood that we can first find the diameter of the tree which can be calculated using a single call to DFS easily (also maintain the start point and end point right)then for each node i will find the distance of this to both of the end point and end point which will give me maximum answer is the answer to the query right.. For this purpose i can use DP LCA ...

          Each query taking log(n) time so the total complexity is Q(log(N))..

          I understood this ... But i read in the editorial that this can be done V+E. Is the idea first find the two end points and then do DFS two time one for each point..