I solving this problem Link to problem. I could not solve it hence checked other people's code. I saw this AC Code. AC Solution. I changed it just a little bit and it got runtime error. Runtime Error Code. As you can see the only difference is in the for loop in the 'dfs' function. Can anyone tell me the reason for the code to receive a runtime error.
.
Checking whether the set is empty or not also did not help.
You should never delete element in a range-based for loop. In the AC code, the element are deleted later after the for loop. If you still wanna delete element while iterating:
I still prefer to delete outside the loop than delete inside the loop. From my experience,
set
is a magic data structure in C++, sometime you couldn't find where your code cause RTE without printing every possible variables, debugger can't help. So be careful, otherwise you will have a great time debugging hopelessly.Thanks for the info. Learnt something new.
But still after iterating through iterators it's still giving runtime error. (This time on test 1). Here is the code that I wrote:
Simply because
unv
is modified outside the loop. Assume currently we are in nodeu
then Then we call dfs go tov
and delete something inunv
. When we go back tou
,unv
was modified.Thank you!
i have removed verdict RTE by first checking whether that element that you are iterating in loop is present or not, we are doing this because we have modified this set many times. 136610663
Thanks a lot man