#K35712. Task Build Ordering

    ID: 25593 Type: Default 1000ms 256MiB

Task Build Ordering

Task Build Ordering

You are given T tasks, numbered from 1 to T, and a list of dependencies for each task. Each task is described by a line in the following format:

task_number num_dependencies dependency₁ dependency₂ ...

A task can only be executed after all its dependencies are completed. Formally, if task u appears as a dependency of task v, then in a valid ordering, it must hold that $$order(u) < order(v).$$

Your goal is to determine an ordering of tasks that satisfies all these dependencies. If such an ordering is impossible (i.e. a cyclic dependency exists), output an empty list: [].

inputFormat

The input is given from stdin in the following format:

The first line contains an integer T (the number of tasks). Each of the following T lines contains a description of a single task. The description starts with the task number, followed by an integer representing the number of dependencies, then the list of dependencies separated by spaces.

For example:

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

outputFormat

If a valid ordering exists, output the task numbers in a valid order separated by a space on a single line to stdout. If it is impossible to complete all tasks due to cyclic dependencies, output an empty list: []## sample

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

</p>