DFS (graphs)

The problem link is

Can someone help me to optimize my solution?
solution link:-
https://www.codechef.com/viewsolution/31782888