So when I was trying to do TWOFL question I don’t know I was getting RunTimeError for some of the cases and only in subtasks 2 and 3. So I wonder whether this is because of big values. Then somehow in my program I made all value as 1 and traversed it for n=1000 and m=1000. Clearly on my PC I got an error of StackOverFlow. I thought may be my program is going in an infinite recursion so I tried more and couldn’t find why is this happening. Then I thought of looking at one of the accepted codes in CPP and changing it into Java. Still it was giving RunTimeError on same cases. Then I looked at others code of Java and many were getting the same error and hence I was sure that this is only with Java who is doing in recursive fashion.
only 2 solution in java accepted during the contest it is very much validating your point,
but it should’t happen in the contest it is like having an edge over java if you are using cpp
and we should not be language specific. @vjvjain0 ask the community to look into the matter if you feel it was unfair as i can not do that bcoz i don’t have any knowledge of java .
I have dealt with the same problem before.
One way is to increase stack size by creating a new thread for your program. It is same as that mentioned on the cf posts you linked.
Second way is to create your own stack and then do iterative dfs( https://www.geeksforgeeks.org/iterative-depth-first-traversal/ )
As we know dfs stack size becomes O(n) in worst case. So if n is upto 10^6 its better if you extend stack size before otherwise you may have to deal with stackoverflow error. This bound also depends on what parameters you are passing through dfs calls. Try to reduce them too.