Comments
+10

Pass adjList as well by reference. Passing it as value creates memory each time the function is called. https://stackoverflow.com/questions/26647152/passing-vectors-to-a-function-value-vs-reference-

But still it gives you TLE 54543465. Try to optimise your dfs.

On Kavi1234 → Runtime Error ..!!!!, 7 years ago
+6

Thanks for the help. Btw,Changed the local arrays to global and it worked too. 54515438. Found this article :https://stackoverflow.com/questions/22945647/why-does-a-large-local-array-crash-my-program-but-a-global-one-doesnt

You are assigning the value of c to val[i] while running dfs, c being the number of connected components, this doesnot ensure that the parent of all these components is 'i' and therefore the answer is coming out to be less than than the actual number of components. Just a little bit of change and there you go..!!! 54225792

PS-- This might give you TLE in system tests as you have not used weighted dsu.