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

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

Hi guys, Why my implementation to problem https://mirror.codeforces.com/contest/1278/problem/D gives MLE, First I though it is because of recursive DFS. But even after changing DFS to iterative it is still giving MLE. Can anyone please help me with it. My solution links

https://pastebin.com/T5eqyXYS (Recursive)

https://pastebin.com/KczMMcud (Iterative)

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

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

It might be because of too large amount of edges in graph. Tree with n vertexes has n-1 edges, so you can check that excession easily.

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

In the worst case, you might end up inserting $$$O(n^2)$$$ edges. So, one idea is to terminate immediately once you find that the no. of edges have become $$$\ge n$$$, since a tree has only $$$(n-1)$$$ edges.