TOPOSORT - Topological Sorting

Sandro is a well organised person. Every day he makes a list of things which need to be done and enumerates them from 1 to n. However, some things need to be done before others. In this task you have to find out whether Sandro can solve all his duties and if so, print the correct order.

Input

In the first line you are given an integer n and m (1 <= n <= 10000, 1 <= m <= 1000000). On the next m lines there are two distinct integers x and y, (1 <= x, y <= 10000) describing that job x needs to be done before job y.

Output

Print "Sandro fails." if Sandro cannot complete all his duties on the list. If there is a solution print the correct ordering, the jobs to be done separated by a space. 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.

Example 1

Input:
8 9
1 4
1 2
4 2
4 3
3 2
5 2
3 5
8 2
8 6

Output:
1 4 3 5 7 8 2 6 

Example 2

Input:
2 2
1 2
2 1

Output:
Sandro fails.

Added by:Josef Ziegler
Date:2011-10-23
Time limit:0.5s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All

hide comments
2016-08-23 20:05:45
use printf/scanf in c++
2016-06-20 20:11:02 Shrish Lal Bhatnagar
i felt time limit to be too strict
I had to do some unnecessary optimization
2016-06-17 04:14:01
does the pair(x,y) is unique
ex: no input that
2 2
1 2
1 2
2016-06-17 01:14:49 deerishi
C++14 STL <3. Priority Queue \m/
2016-06-15 12:18:17 CHANDAN KUMAR
In love with STL!!!!!!!!!!
2016-05-15 15:35:01 shikhar0037
must use FAST I/O
2016-04-14 22:25:07 Akshat Mathur
Refer Kahn's Algorithm.
2016-02-26 16:48:45 SANDEEP KUMAR
Those getting WA probably have the bad habit of not reading the problem statement properly just like me :D
..So read this "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. "
2016-02-23 20:23:24 Vivek Mangal
nice problem.learnt a lot.
2016-02-13 07:20:42
What is the correct ouput for this test case (Something feels wrong about this test case)
8 6
1 2
2 3
5 7
8 9
6 7
2 5
SPOJTOOLKIT says Sandra fails.
My code says 1 2 3 4 5 6 7 8

Last edit: 2016-02-13 07:58:56
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.