#K56172. Task Ordering with Prerequisites
Task Ordering with Prerequisites
Task Ordering with Prerequisites
You are given t tasks numbered from 1 to t and p prerequisite pairs. Each prerequisite is given as a pair of integers (a, b), which indicates that task a must be completed before task b can be started.
Your task is to determine a valid order in which all tasks can be performed. Formally, find a sequence \(\pi = [\pi_1, \pi_2, \dots, \pi_t]\) such that for every prerequisite pair \((a, b)\), task a appears before task b in \(\pi\). If no such ordering exists (i.e. the dependency graph contains a cycle), output "IMPOSSIBLE".
inputFormat
The first line contains two integers t and p (\(1 \le t \le 10^5\), \(0 \le p \le 10^5\)), where t is the number of tasks and p is the number of prerequisites.
Each of the next p lines contains two integers a and b (\(1 \le a, b \le t\)), representing a prerequisite: task a must be completed before task b.
outputFormat
If a valid ordering exists, output the task labels in a valid order, separated by a single space.
If it is impossible to complete all tasks due to a cycle in the prerequisites, output "IMPOSSIBLE".
## sample3 0
1 2 3
</p>