#C7635. Task Scheduling Problem
Task Scheduling Problem
Task Scheduling Problem
You are given a set of tasks. Each task is represented by a unique integer task_id, a brief description, and a list of prerequisite task IDs that must be completed before the task can be started. Your objective is to determine a valid order in which all tasks can be completed.
Formally, let there be n tasks. For each task, you are provided with the following details:
- task_id: an integer representing the task.
- description: a string (without spaces) describing the task.
- prerequisites: an integer k followed by k integers, each representing a task that must be completed before this task.
The tasks form a directed graph; a valid ordering is one in which for every directed edge \( u \to v \) (i.e. task \( u \) is a prerequisite for task \( v \)), \( u \) appears before \( v \) in the ordering. If there is a cycle in the graph (a circular dependency), no valid order exists.
Note: If multiple valid orderings exist, output the one that is lexicographically smallest (where tasks with smaller IDs are chosen first when possible). Use a topological sort algorithm with a min-heap (or any equivalent mechanism) to guarantee uniqueness.
inputFormat
The input is given from standard input (stdin). The first line contains a single integer n which denotes the number of tasks. Each of the following n lines describes a task in the following format:
task_id description k p1 p2 ... pk
Here, task_id is an integer, description is a string without spaces, k is the number of prerequisites, and p1, p2, ..., pk are the prerequisite task IDs.
outputFormat
Output to standard output (stdout) a valid order of task_ids separated by a single space. If there is a circular dependency and no valid order exists, output a single 0.
## sample4
1 Task1 2 2 3
2 Task2 1 4
3 Task3 0
4 Task4 1 3
3 4 2 1