cswork / OA打开练习工作台

Applied IntuitionOA #1

Compilation Order with Topological Sort

估计中等可在线判题

考点(AI 标注)拓扑排序单调栈与堆

A dependency [module, prerequisite] means prerequisite is compiled first; each time, choose the lexicographically smallest among the modules that can currently be compiled. If a cycle exists, return IMPOSSIBLE.

See "Input" for the standard I/O conventions and constraints added by this site.

输入格式

Module names are distinct and contain no spaces; n,m≤200000, and dependencies are given as pairs.

输出格式

Output the compilation order or IMPOSSIBLE.

样例 1

输入:

3
app core util
2
app core
core util

输出:

util core app

样例 2

输入:

3
b a c
0

输出:

a b c

样例 3

输入:

2
a b
2
a b
b a

输出:

IMPOSSIBLE