Getting segmentation fault

q link:


soln:

Its just a simple question. You’ve made it complicated to understand. Do a dfs and find the size of component, each time you find the size just multiply it with total no. of nodes minus the nodes of this component. Sum them up and you’ll get an AC

@amankumarkeshu that’s exactly what I have done, how have I complicated anything?

I cannot understand the use of other things in main function other than the dfs function. Why are you doing it. I’m not sure if it is going segmentation fault there.

@amankumarkeshu as I explained in the chat, the other code is from hackerrank itself, look at the journeyToMoon() function only. I attached the question link too the function arguments have been explained in that.

Yup I’ll look into this.

@Ishitagambhir
the segmentation fault is because in DFS_helper function, for recursive call from DFS_Helper(src, vis) you are making recursive call to same DFS_Helper(src, vis) instead of DFS_Helper(nbr, vis)
It will resolve the segmenation fault.

@Ishitagambhir
Also use long long int to avoid Wrong answer.

@Ishitagambhir
it will be alright after using long long int. Have a look if needed https://ide.codingblocks.com/s/259802

1 Like

@sanyamsinghalmnnit Thank you it is working now. One thing I did not understand is, why was the code giving error when the return type of main() was int? I understand it’d be changed to long long int instead, but why’d that produce an error?

Yes, main() must return int . The return value is passed back to the operating system, to indicate whether the program ran successfully: zero means success.
so using signed is just a way to work it out

1 Like

@Ishitagambhir
please mark the doubt as resolved.

1 Like