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

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

MILITARY PROBLEM

I am trying to solve this problem by running dfs from every node. I am getting TLE verdict. Please help me speed up my solution

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

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

Hint: Dont dfs from every node.

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

If you run dfs from the root, you can save the sequence of accessing nodes and calculate the size of each nodes' subtree. Now if ith node's index is in and ith node's size is k, you can find your answer in your sequence from in to in+k.

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

Did you try to understand the tutorial? If yes, please, tell what part you didn't understand(even if you didn't understand anything).

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

I will describe my approach which i believe is a bit easier than the one described in editorial.
First of all we run dfs from an arbitrary node which we set as the root (eg. node 1) and push each node processed from dfs into a set. We can see that this will generate the set from the sample. Now by definition when we have a query {u, k} we are interested in the k-th element in set counting from the index of u in the set. We are now left with calculating whether the answer is -1. We can do this slightly different than the approach described in editorial. For each node we count the number of nodes in it's subtree and now we node that if #of nodes in u's subtree < k then answer is -1.

My code: 40438205