How to solve Spoj TOPOSORT problem?

Please tell me - How to solve Spoj TOPOSORT problem? I have tried to solve this but getting wrong at 12 number case.

My code.

1 Like

I have just solved this problem and was getting the same problem. The reason is that the question states -

If there is a solution print the correct ordering, the jobs to be done separated by a whitespace. If there are multiple solutions print the one, whose first number is smallest, if there are still multiple solutions, print the one whose second number is smallest, and so on.

To achieve this, you should use std::set instead of std::queue because then the vertices will always be taken in sorted order for processing and the goal of question will be achieved.

Also use scanf/printf as cin/cout will give you a TLE (just as it has given me).

The link to my solution (if you want to refer) is -

Hope this helps.


I have used same method as @prj18 but still getting TLE after test case #12
Link to code ->

1 Like