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

Автор ritik_patel05, история, 5 лет назад, По-английски

Problem : https://atcoder.jp/contests/abc148/tasks/abc148_f

How did you solved this problem? It would be better if you can give your intuition along with your approach.

Thank you.

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

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

This might help a bit.

»
5 лет назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

Let A be Aoki's initial position and T be Takahashi's initial position.

We root the tree at A. Then for Takahashi, the optimal strategy will be running to some parent of T (we call that parent P, P can be just T), and then from there run to the furthest leaf in the subtree rooted in P.

The three phases are:

  1. Takahashi runs to P and Aoki just runs towards P as well
  2. Takahashi runs to the furthest leaf, Aoki chases (note this doesn't change the decrease the distance between both of them)
  3. Takahashi is now trapped at a leaf. The number of turns needed to end the game is (The distance between them - 1)

We should only consider all P in which Takahashi can reach P before Aoki can reach P.

We can find the distance to the furthest leaf for all nodes using dp on tree. Then for each P, the total time taken is:

(The time needed to travel to P) + (The distance to the furthest leaf) + (The distance between Takahashi and Aoki after Takahashi reaches P - 1).

We just take the maximum value for all valid P.

  • »
    »
    5 лет назад, # ^ |
    Rev. 3   Проголосовать: нравится +7 Проголосовать: не нравится

    You can simplify it by observing that for each leaf k which Takahashi arrives earlier, that formula always reduces to (Distance between v and k) — 1. Since this value always has its maximum at a leaf, we can just write

    Max{ for any node k with dist[u to k] < dist[v to k] } ( dist[v to k] - 1 )

»
5 лет назад, # |
  Проголосовать: нравится +3 Проголосовать: не нравится
»
5 лет назад, # |
  Проголосовать: нравится +14 Проголосовать: не нравится

You can solve this problem with dfs!

First make "Takahashi" as a root and write all vertices distance to root let call that disx[i](for vertex number i) and then make "Aoki" as a root and write all vertices distance to root let call that disy[i](for vertex number i) you should find:

maximum disy[i] such that disy[i] > disx[i] and the answer is (disy[i] — 1)

my solution