depressed_ontop's blog

By depressed_ontop, history, 6 weeks ago, In English

I am getting run-time error most of the time in python in problems involving trees. I have increased recursion limit then also I am getting run-time error .And same logic works in C++.

Some of my recent submissions 119637795,119420809 and there are many more .

I want help, Is there any way of doing DFS in python without getting run-time error.

 
 
 
 
  • Vote: I like it
  • 0
  • Vote: I do not like it

»
6 weeks ago, # |
  Vote: I like it 0 Vote: I do not like it

short answer: google "python recursion site:codeforces.com"

slightly longer answer: https://codeforces.com/blog/entry/91765 which is about the second problem you asked about, and also how sys.setrecursionlimit is only a part of the issue

further context: c++ programs get compiled/run with generous stack space parameters on this site while other languages are probably using default settings which tend to be smaller. There are hack-y ways to set parameters within a program and/or hope they affect a new thread you create, buuut that's probably too much 'off' in trade-off...

Especially for bfs/dfs, it's pretty straightforward to recognize what state you're keeping when you make a recursive call. If you 'do it iteratively' you can be explicit about what you're holding on to, which may come out lighter than python's function call logic piled up in depth.

»
6 weeks ago, # |
  Vote: I like it -8 Vote: I do not like it

I don't think there is any solution for this "problem". instead one should get comfortable in c++ programming.

  • »
    »
    6 weeks ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    The solution is wrapped into a function. This function is run in a new thread. Before that, the stack limit for new threads is updated.

    The trick of starting a new thread to control the stack size in the contests is old. I first saw it used for Java some time around 2010. 119420809 Now it is accepted. I got the solution thanks to Gassa