#C6389. Task Ordering with Dependencies

    ID: 50143 Type: Default 1000ms 256MiB

Task Ordering with Dependencies

Task Ordering with Dependencies

You are given N employees numbered from 1 to N and M dependency relations. Each dependency is given as a pair of integers \(a\) and \(b\), which means that employee \(b\) must finish their task before employee \(a\> can begin theirs. Your task is to determine a valid ordering of the employees that satisfies all the dependencies.

If it is impossible to complete all tasks due to cyclic dependencies, output an empty list.

Note: The dependency \((a, b)\) implies a directed edge from \(b\) to \(a\) in the dependency graph. The ordering given should list the employees in the order in which their tasks can be performed.

inputFormat

The first line contains two integers \(N\) and \(M\) separated by a space, where \(N\) is the number of employees and \(M\) is the number of dependency relations.

The next \(M\) lines each contain two integers \(a\) and \(b\) separated by a space, representing a dependency relation where employee \(b\) must precede employee \(a\>.

Input Example:

3 2
2 1
3 2

outputFormat

If a valid ordering exists, output the ordering of employees as space-separated integers in a single line. If no valid ordering exists (i.e. there is a cycle in the dependencies), output an empty list represented as [].

Output Example:

1 2 3
## sample
3 2
2 1
3 2
1 2 3

</p>