This kind of problem might become NP Hard if the graph is not acyclic.
Possible solution that comes in mind is Topological sorting and Hamiltonian Path solution but after in depth thought could not see a possible solution.
Similar question has been asked in stack exchange 2 years back but no correct answer.
Hope the codechef community will be of help.