Under construction

Still being built. Things may change

Kingdom's Development Plan

Interview

Time limit2sMemory limit1024 MB

Summary
Given n projects and prerequisite pairs, output the lexicographically smallest valid completion order, or IMPOSSIBLE if the dependencies contain a cycle.
Level

Easy3 of 10

Topics
Topological sort, Graph, Heap
Solved
No attempts yet

Problem

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

You are given:

  • nn, the number of projects, numbered from 1 to nn.
  • mm, the number of prerequisite relationships.
  • mm pairs (a,b)(a, b), each meaning project aa must be completed before project bb can start.

If all projects cannot be completed because of a cyclic dependency, output IMPOSSIBLE. If several valid orders exist, output the lexicographically smallest one.

Input

The first line contains two integers nn and mm. Each of the next mm lines contains two integers aa and bb, a prerequisite pair meaning project aa must be completed before project bb.

Output

If it is impossible, output IMPOSSIBLE. Otherwise, output one line with nn integers, a valid completion order. If several orders are valid, output the lexicographically smallest one. An order is smaller than another if, at the first position where they differ, its project number is smaller.

Constraints

  • 1≤n≤1051 \le n \le 10^5
  • 0≤m≤2×1050 \le m \le 2 \times 10^5
  • a,b∈{1,2,…,n}a, b \in \{1, 2, \dots, n\}
  • a≠ba \ne b
  • No duplicate pairs are given.

Examples2

  1. Example 1

    Input
    5 5
    1 2
    2 3
    2 4
    2 5
    3 4
    
    Expected output
    1 2 3 4 5
    
  2. Example 2

    Input
    5 4
    1 2
    2 3
    3 1
    5 4
    
    Expected output
    IMPOSSIBLE