DP on Trees: Tree Distances II

Поделиться
HTML-код
  • Опубликовано: 8 ноя 2024

Комментарии • 32

  • @himanshumidha537
    @himanshumidha537 3 года назад +5

    Best explanation I found on the topic today. How the formula of re-rooting was derived was major problem and major part.

  • @mayankchaudhary5515
    @mayankchaudhary5515 4 года назад +12

    the rerooting explanation was amazing, I was very happy when I find out the recurrence for SubAns[I] myself , , , , Thanks :)

    • @AlgosWithKartik
      @AlgosWithKartik  4 года назад +1

      That's amazing and I am happy to see that you are benefitting from the videos :)

  • @159_mdamirulislam8
    @159_mdamirulislam8 3 года назад +2

    This man deserves more likes and subscriptions

  • @ronaktaleja8517
    @ronaktaleja8517 3 года назад +2

    Bhai mazaa aa gaya yaar solve karke. Kya baat hai!

  • @akshatchaudhary9393
    @akshatchaudhary9393 Год назад +1

    I also have a simpler looking approach:
    1. Calculate the number of nodes in the subtree for each node i
    2. For calculating the answer for root, also calculate the depths of each node w.r.t root and sum them up for the answer to the root
    3. Now, let res be our answer array and subt be our arrays holding number of nodes in subtree of node i
    4. Res[i]=res[par]+n-2*subt[i]
    5. Lets suppose we know the answer for our parent, now to find the distance to its child i, we see that all nodes lying outside the subtree of i will have +1 in distance to i when compared to its par because it is 1 distance further away. So we add n-subt[i] to the answer, also we see that the result of our parent has all the nodes in subtree of i at 1 greater distance when compared to i, so we add -subt[i] to transform the result of i's parent to the result of i
    6. Res[i] has final answer of i, also calculate res[i] only when i!=root ( we already calculated before at start)

  • @KCOMohammadRehmaan
    @KCOMohammadRehmaan 3 года назад +1

    amazing videos bhaiya i was able to solve this problem on my own thank you for making these videos

  • @hymnish_you
    @hymnish_you 3 года назад +1

    Such a beautiful explanation as ususal

  • @yourbestie4138
    @yourbestie4138 3 года назад

    we need more tutorials on cses sets amazing explanation

  • @shubhamgond2884
    @shubhamgond2884 Год назад +1

    Nice explanation

  • @mayankchaudhary5515
    @mayankchaudhary5515 4 года назад +2

    Do you thinking of making video explanations on Range queries also? It might be more helpful, although code forces blog is nice too

    • @AlgosWithKartik
      @AlgosWithKartik  4 года назад +10

      maybe not for CSES problems but I am definitely considering a series on range query DS like sparse tables segtrees sqrt decomp etc.

  • @stayInTheGreen_
    @stayInTheGreen_ 4 года назад +1

    Thank you for such a nice explanations...

  • @ankoor
    @ankoor 3 года назад +1

    Great explanation!

  • @komalbisht1979
    @komalbisht1979 Год назад

    clear explanation.thnx

  • @ankithreddyadudodla7673
    @ankithreddyadudodla7673 3 года назад +1

    Helpful and Great!

  • @ishaankaustav727
    @ishaankaustav727 2 года назад

    beautiful sir

  • @ZealKapadiya
    @ZealKapadiya 6 месяцев назад

    Why does he need count of nodes present in subtree? Can somebody explain

  • @angshumansarma2836
    @angshumansarma2836 4 года назад

    Amazing Bhaiya :)

  • @DebojyotiMandal
    @DebojyotiMandal 2 года назад

    Damn explanation.🔥

  • @falseee4445
    @falseee4445 4 года назад +2

    8:06 why isnt 1+1+2+2 instead ?

  • @chaitanyagujarathi4318
    @chaitanyagujarathi4318 3 года назад +1

    Awesome video

    • @chaitanyagujarathi4318
      @chaitanyagujarathi4318 3 года назад

      how can I solve these problem
      www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/parwal-problem/

    • @AlgosWithKartik
      @AlgosWithKartik  3 года назад +2

      looks like the problem can be solved with the rerooting technique. try thinking more in that direction.
      root the tree at some arbitrary node. for the root answer should be easy to evaluate using top down dp.
      for other nodes
      if I start from vertex i, i will either go downwards or upwards. downwards ans is easy. upward ans you will have to re-use the answer of parent.
      i might be wrong as I only had a glimpse of the problem.

    • @chaitanyagujarathi4318
      @chaitanyagujarathi4318 3 года назад

      @@AlgosWithKartik Yeah these problem uses the rerooting technique , I am struggling with these problem , from last two days, if you are not busy plz guide me

  • @ZealKapadiya
    @ZealKapadiya 6 месяцев назад

    Please write what is partial, sb and other terms..The explaination is not that clear because of that

  • @architsinha4272
    @architsinha4272 4 года назад +4

    Please be more clear with what you want to say

    • @AlgosWithKartik
      @AlgosWithKartik  4 года назад +7

      Which part do you find is not clear and I'll try to help you with that.

    • @karthiksharma8720
      @karthiksharma8720 Год назад +1

      @@AlgosWithKartik last part