atcoder#ABC289B. [ABC289B] レ
[ABC289B] レ
Score : points
Problem Statement
Studying Kanbun
, Takahashi is having trouble figuring out the order to read words. Help him out!
There are integers from through arranged in a line in ascending order.
Between them are "レ" marks. The -th "レ" mark is between the integer and integer .
You read each of the integers once by the following procedure.
- Consider an undirected graph with vertices numbered through and edges. The -th edge connects vertex and vertex .
- Repeat the following operation until there is no unread integer:- let be the minimum unread integer. Choose the connected component containing vertex , and read all the numbers of the vertices contained in in descending order.
- let be the minimum unread integer. Choose the connected component containing vertex , and read all the numbers of the vertices contained in in descending order.
For example, suppose that integers and "レ" marks are arranged in the following order:
(In this case, , and .) Then, the order to read the integers is determined to be , and , as follows:
- At first, the minimum unread integer is , and the connected component of containing vertex has vertices , so and are read in this order.
- Then, the minimum unread integer is , and the connected component of containing vertex has vertices , so , , and are read in this order.
- Now that all integers are read, terminate the procedure.
Given , and , print the order to read the integers.
What is a connected component?
A subgraph of a graph is a graph obtained by choosing some vertices and edges from the original graph.
A graph is said to be connected if and only if one can travel between any two vertices in the graph via edges.
A connected component is a connected subgraph that is not contained in any larger connected subgraph.
Constraints
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer in the following format, where is the -th integers to read.
5 3
1 3 4
2 1 5 4 3
As described in the Problem Statement, if integers and "レ" marks are arranged in the following order:
then the integers are read in the following order: , and .
5 0
1 2 3 4 5
There may be no "レ" mark.
10 6
1 2 3 7 8 9
4 3 2 1 5 6 10 9 8 7