Would you please explain how to solve this question ABC119 Task C with dfs.
I think reading the correct code from others will be useful to you.
Google translate of editorial. Although text is a bit unaligned. But still its readable and sufficient.
P.S. Dfs is just a fancy name here. Nothing related to graphs here.