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

Автор Top12, история, 9 месяцев назад, По-английски

When I was solving the problem D from the last contest Codeforces Round 1070 (Div. 2), in the contest i wrote the dp arguments like (vertex v, parent of vertex v), it gave TLE see my submission, and using some optimizations i reduced the dp arguments to just 1 and it was the index of the edge, and same it gave TLE :-(

in the contest I was able to optimize this solution using segment tree and binary search and finally it gave AC!

but after the contest I just changed a small thing in my first submission instead of having arguments like (vertex v, parent of vertex v), I changed it to (vertex v, the value of the parent of vertex v) and it was passed!!, see my submission

Can anyone tell me how that happened?

Thanks!

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

»
9 месяцев назад, скрыть # |
 
Проголосовать: нравится -10 Проголосовать: не нравится

sometimes it happens

»
9 месяцев назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

The reason is that you started depending on the value of the previous number, not on the node index itself, and that’s why the solution got AC.

Why?

Because in the test cases, they are forced to use a small number of values. The Fibonacci sequence up to (10^{18}) has only about 88 numbers. So if they want to make you go deep in recursion, they must follow the Fibonacci sequence correctly.

They cannot force you to walk through something like 1000 different steps, because that’s impossible with Fibonacci values limited to (10^{18}).

That’s why when the DP depends on the value of the node instead of the node number, the number of states becomes very small, and the solution becomes much more efficient, which leads to AC.

»
9 месяцев назад, скрыть # |
Rev. 2  
Проголосовать: нравится +7 Проголосовать: не нравится

Lets say you have this graph:

(In the picture: a(b) means node with index a has value b)

Then your TLE code stores {node 2, parent 1}, {node 2, parent 4}. But your AC code stores {node 2, value 1} which just reduces the time & space!