#C4034. Module Loading Order

    ID: 47528 Type: Default 1000ms 256MiB

Module Loading Order

Module Loading Order

You are given ( n ) modules and ( m ) dependency relations between them. Each dependency is given as a pair of integers ( (a, b) ) indicating that module ( a ) depends on module ( b ). Your task is to determine an order to load these modules such that every module is loaded only after all the modules it depends on have been loaded. In other words, if ( a ) depends on ( b ), then ( b ) must appear before ( a ) in the order. If no valid loading order exists (i.e. if there is a cycle in the dependency graph), output 0.

Note: If there are multiple valid orders, output the one obtained by the natural order of processing the modules using a standard topological sort algorithm (i.e., process nodes with zero in-degree in the order they appear).

inputFormat

The first line contains an integer ( n ), the number of modules.\nThe second line contains an integer ( m ), the number of dependency relations.\nThe following ( m ) lines each contain two integers ( a ) and ( b ), representing that module ( a ) depends on module ( b ).

outputFormat

If a valid loading order exists, output the order as a sequence of space-separated integers representing the modules in the order they should be loaded. If no valid order exists (i.e., the dependency graph has a cycle), output 0.## sample

5
4
1 2
2 3
3 4
4 5
5 4 3 2 1