## TOPOSORT - Topological Sorting

no tags

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 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.

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.
```

hide comments
 < Previous 1 2 3 4 5 Next > priyankarking: 2018-02-18 12:35:30 nice one:) shubham_001: 2018-02-02 11:17:32 WAs due to '.' in "Sandro fails." chetan4060: 2018-01-28 14:18:52 good question:-) Josef Ziegler: 2018-01-27 01:29:21 Bumped up the time limit from 0.1s to 0.5s (per test case) as many Java users were noticing TLE. Last edit: 2018-01-27 01:29:38 sharif ullah: 2017-11-22 03:32:09 Print "Sandro fails." if Sandro cannot complete all his duties on the list. !!!!!!!!!!!!!!!!!!! all duties~ madhur4127: 2017-11-03 11:23:52 use synced cin & cout, for TLE! rvatna: 2017-09-11 01:15:47 It seems that using exit(1) function in C will result in a NZEC. Take care and use exit(0)... arikpamnani: 2017-06-24 21:14:49 Please see "." (full stop) in "Sandro fails.", this caused me 10 WAs :p. Last edit: 2017-06-24 21:15:04 razor123: 2017-05-17 08:17:18 O(nlogn+m) passes :) v_ns: 2017-03-23 11:52:57 don't use cin/cout, got one TLE.

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