#P1375H. Set Merging
Set Merging
No submission language available for this problem.
Description
You are given a permutation of numbers from to . Also, you have sets , where . Lastly, you have a variable , representing the current number of sets. Initially, .
We define two kinds of functions on sets:
;
.
You can obtain a new set by merging two sets and , if they satisfy (Notice that the old sets do not disappear).
Formally, you can perform the following sequence of operations:
;
, you are free to choose and for which and which satisfy .
You are required to obtain some specific sets.
There are requirements, each of which contains two integers ,, which means that there must exist a set ( is the ID of the set, you should determine it) which equals , which is, the set consisting of all with indices between and .
In the end you must ensure that . Note that you don't have to minimize . It is guaranteed that a solution under given constraints exists.
The first line contains two integers — the length of the permutation and the number of needed sets correspondently.
The next line consists of integers (, are pairwise distinct) — given permutation.
-th of the next lines contains two integers , describing a requirement of the -th set.
It is guaranteed that a solution under given constraints exists.
The first line should contain one integer , representing the number of sets after all operations.
lines must follow, each line should contain two integers , (, where is the value of before this operation), meaning that you choose , and perform a merging operation. In an operation, must be satisfied.
The last line should contain integers , representing that set is the th required set.
Please notice the large amount of output.
Input
The first line contains two integers — the length of the permutation and the number of needed sets correspondently.
The next line consists of integers (, are pairwise distinct) — given permutation.
-th of the next lines contains two integers , describing a requirement of the -th set.
Output
It is guaranteed that a solution under given constraints exists.
The first line should contain one integer , representing the number of sets after all operations.
lines must follow, each line should contain two integers , (, where is the value of before this operation), meaning that you choose , and perform a merging operation. In an operation, must be satisfied.
The last line should contain integers , representing that set is the th required set.
Please notice the large amount of output.
Samples
Note
In the first sample:
We have initially.
In the first operation, because , we can merge into .
In the second operation, because , we can merge into .
In the third operation, because , we can merge into .
For the first requirement, , satisfies it, thus .
For the second requirement, , satisfies it, thus
Notice that unused sets, identical sets, outputting the same set multiple times, and using sets that are present initially are all allowed.