I'm getting wa31 for this problem ( my submission ).
First, I find all vertices that can be reached from the capital. Then, I look at all vertices which have indegree equal to 0. These vertices must have an edge directly connecting them to the capital, so I do that. Now, if unvisited vertices remain, they must form a cycle. So, I visit these vertices.
Please help