If I run a recursive (Java) program on my machine, stack overflow usually occures before reaching depth of 10000 (there is no tail recursion since Java does not make it available unfortunately). Yet on Codeforces my recursive programs on trees work up to 100000 or more (one such program I found is https://mirror.codeforces.com/contest/1406/submission/92624199). Does anyone have an idea why is it like this and how can I make it so this would also work on my machine or on other less advanced online judges?
You can increase a stack size with -Xss flag, so you should you run your program like
java -Xss500m Main
. You can find more in this question on StackOverflowI think there's stack overflow in codeforces too. I don't remember exactly which problem was that, but I have faced issues when I try to run a recursive function that can go deep upto 10^6 levels.
Edit : Nevermind, I just rechecked all my MLE submissions and all of them were stupid issues not related to stack-overflow. :P
It is possible to get stack overflow 92174392.
Consider adding something like
which sets the stack size to 1<<28 ($$$2^{28}$$$). Note that this doesn't allow you to do recursion up to $$$2^{28}$$$ levels (you'll get MLE), but it basically overcomes stack overflow. Locally, add the
-Xss
flag when running.-Xss 512m
is usually enough.