Error Report
Time limit2sMemory limit512 MB
Given a sequence of function numbers from concatenated stack traces, build a call graph of minimum edge count where all traces come from errors in at most two functions.
- Level
Medium7 of 10
- Topics
- Graph, Greedy, String, Implementation
- Solved
- No attempts yet
Problem
A function call stack trace printed when an error occurs is a powerful tool for debugging programs. Consider a mathematical model of how functions in a program interact, called a call graph.
Suppose a program has functions that can call one another. Number all functions in the program from 1 to . Let be the set of pairs , where and are the numbers of functions such that calls . The size of this set is called the complexity of the call graph.
For example, consider a program in a primitive programming language with three functions.
function f(x)
if x > 0 then
return g(x)
else
return h(x)
function g(x)
return x
function h(x)
if x == 0 then
return 1 / x
else
return h(x + 1) + 1
Number the functions so that is 1, is 2, and is 3. Then the set is because function calls functions and , function calls no other functions, and function calls itself. The complexity of the call graph of this program is 3.
The call stack trace printed when an error occurs works as follows. Suppose some error occurs while the program is running. First, the number of the function in which the error occurred is printed, then the number of the function from which this function was directly called, then the number of the function from which function was called, and so on.
For example, suppose the call is made in the program above. Then is called, which calls , which in turn calls and , and division by 0 occurs in the last call. Then the call stack trace looks like this:
3
3
3
3
1
Yura asked Lyosha to find an error in his program and sent him the call stack traces printed after errors. Unfortunately, the file Yura sent to Lyosha contains several call stack traces from different errors, and these traces are printed one after another with no separators between them. Yura claims that errors can occur in only two functions, but he does not remember which ones.
Lyosha realized that the call stack traces alone would not be enough and that he would have to look at the program. Before doing so, he wants to find out what minimum complexity the call graph of this program can have if all of Yura's claims are true.
Help him find the minimum possible complexity of the program's call graph such that the file sent to him could contain one or more call stack traces written one after another without separators, and the direct errors occurred in at most two different functions.
Input
The first line contains two integers and (): the number of functions in Yura's program and the number of lines in the call stack trace file that Yura sent to Lyosha. Each of the following lines contains a single integer (): the number of the function on the -th line of the file.
Output
On the first line, print a single integer : the minimum possible complexity of the call graph of Yura's program. On each of the following lines, print two integers and . Such a pair means that the function numbered can call the function numbered . If there are several possible call graphs, print any one of them.
Hint
In the example, it is possible that errors occur only in functions 1 and 3, and that the given file contains five call stack traces written one after another. Below are the same traces separated by empty lines.
1
3
3
2
3
2
1
In this case only function 2 calls function 3, so the complexity of the call graph is 1.