Fourty_Seven's blog

By Fourty_Seven, 3 years ago, In English

Until today I thought that recursion in general is very bad for competitive programming and it always fails, but now it looks like that it was python hogging memory all this time in my previous recursion solutions. I was solving this problem, C of the last contest, and my solution was getting runtime error due to memory so I tried to optimize it the best I can but it was still not working so I tried what every python programmer here have to do sometime or other, I copied my solution gave it to chatGPT to convert to c++ (which it surprisingly did in the first try). AND IT WORKS. Even my previous less optimized versions work in c++.

< sobbing >

Now I just want to know if it was me or python because at this point I have no trust in either of them. Someone please look into my solution and tell me if I am doing something wrong or recursion is just impossible with python here. My approach is basically the same as mentioned in the editorial, I am just using recursion for dfs. In worst case my program should have a recursion stack of $$$10^5$$$ with each item of constant size so I am very confused why is it using so much memory. Considering 512MB limit there should be more than 5KB per stack element, how are 10 integers taking 5 KiloBytes of space.

One last thing, is there a difference between these two errors:

Memory limit exceeded

exit code: -1073741571 (STATUS_STACK_OVERFLOW)

  • Vote: I like it
  • +8
  • Vote: I do not like it

| Write comment?
»
3 years ago, hide # |
← Rev. 3  
Vote: I like it +10 Vote: I do not like it

Just never use recursion with big depth in python. Unfortunately it's just too terrible

If you need a recursion there is an easy way to make it on stack almost without editing the code https://codeforces.me/blog/entry/80158

With adding it the memory problem goes away, though it still gets TL 7 https://codeforces.me/contest/1946/submission/252972888

But after changing binary search to use only one function call instead of 2 it passes https://codeforces.me/contest/1946/submission/252973095

Also adding faster input input = sys.stdin.readline gives extra 600ms time, not adding children into separate list — 300ms time https://codeforces.me/contest/1946/submission/252974970

About getting RE — I am not sure why it is here. Just python recursion moment xd

  • »
    »
    3 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    May the light illumine you my friend. Thank you very much.

    Sometimes it feels that we python guys play the game on hard mode XD

»
3 years ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

Avoid python

  • »
    »
    3 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    yeah by switching to C++(or C that I use)

    • »
      »
      »
      3 years ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      You walk a dangerous path my friend, be very careful. Even if you survive those semicolons, there are those...

      those curly braces, don't be fooled by their innocent looks. You won't even realize when they curl around your code and strangle it.

      May the light protect you.

  • »
    »
    3 years ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Can't do that brother. Me and those semicolons, old enemies, very old enemies.

    Our enmity goes back generations. 46, 45 and even 44 fought against that evil and I shall continue their fight till the day I draw my last breath.

    Peace be upon you my brother, but I have to fight this war, alone if I must.