#P15915. [TOPC 2024] Kingdom' s Development Plan

[TOPC 2024] Kingdom' s Development Plan

Description

The Kingdom of Topcaria is planning a series of developmental projects to enhance its infrastructure. Each project has specific prerequisites that must be completed before the project can start. The Ministry of Development has asked you to help determine a feasible order in which all the projects can be completed.

You are given:

  • nn, the number of projects numbered from 11 to nn.
  • mm, the number of prerequisite relationships between these projects.
  • A list of mm pairs, where each pair (a,b)(a, b) indicates that project aa must be completed before project bb can start.

Your task is to determine an order in which all the projects can be completed. If it is impossible to complete all projects due to a cyclic dependency, output “IMPOSSIBLE”. If there are multiple valid orders, please output any the lexicographically smallest one.

Input Format

The first line contains two integers nn and mm — the number of projects and the number of prerequisite relationships. The next mm lines each contain two integers aa and bb — a prerequisite pair indicating that project aa must be completed before project bb.

Output Format

If it is not possible, output “IMPOSSIBLE”. If it is possible to complete all projects, output a single line with nn integers — a valid order of project completions. If there are multiple possible orders, output the lexicographically smallest one. An order is lexicographically smaller than another order if at the first position where they differ, the project number on the first order is smaller than the number on the second order.

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

Hint

  • 1n1051 \le n \le 10^5
  • 0m2×1050 \le m \le 2 \times 10^5
  • a,b{1,2,,n}a, b \in \{1, 2, \dots, n\}
  • aba \ne b
  • No duplicate pairs are given.