Code Submission Evaluation System Login

NOI 2019 Open


Tasks | Scoreboard | Statistics

CSES - NOI 2019 Open - Graph OrderingCSES - Graph Ordering

Graph Ordering

Time limit:1.00 s
Memory limit:512 MB

You are given an undirected connected graph that has $n$ nodes. The nodes are numbered $1,2,\dots,n$.

Let us consider an ordering of the nodes. The first node in the ordering is called the source, and the last node is called the sink. In addition, a path is called valid if always when we move from node $a$ to node $b$, node $a$ is before node $b$ in the ordering.

Your task is to find an ordering such that (1) there is a valid path from the source to every node, and (2) there is a valid path from every node to the sink, or determine that it is not possible to create such an ordering.


The first line has two integers $n$ and $m$: the number of nodes and edges.

After this, there are $m$ lines that describe the edges. Each line has two integers $a$ and $b$: there is an edge between nodes $a$ and $b$.

It is guaranteed that the graph is connected, contains no self-loops and there is at most one edge between every pair of nodes.


Print any valid ordering of the nodes. If there are no solutions, print "IMPOSSIBLE".

Example 1

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

4 2 3 1 5

Example 2

4 3
1 2
3 2
4 2


Subtask 1 (7 points)
Subtask 2 (29 points)
Subtask 3 (18 points)
Subtask 4 (21 points)
Subtask 5 (25 points)