cswork / OAOpen practice workspace

Applied IntuitionOA #1

Compilation Order with Topological Sort

Estimated MediumJudge-ready

Topics (AI-labelled)Topo SortStacks & Heaps

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.

Input

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

Output

Output the compilation order or IMPOSSIBLE.

Example 1

Input:

3
app core util
2
app core
core util

Output:

util core app

Example 2

Input:

3
b a c
0

Output:

a b c

Example 3

Input:

2
a b
2
a b
b a

Output:

IMPOSSIBLE

Editorial

Loading solution…