Everyone loves flowers, but flowers on graphs are even better!
Let's define the DFS tree of the graph and the beauty of the tree.
\operatorname{DFS}_{\operatorname{path}}(G) — the DFS tree of the graph (the edges traversed by DFS).
used = array of size |G| initialized to false
edges = empty list
procedure DFS(node):
mark used[node] as true
for each neighbor v in G[node]: # iterate over all edges (node, v) in the original graph
if not used[v]:
add (node, v) to edges
DFS(v)
procedure DFS_path(G):
DFS(1)
return edgesThe beauty of the tree is defined this way:
where \operatorname{deg}_i is the degree of vertex in the tree .
Your task is as follows: Construct a simple connected graph with vertices and edges in such a way that maximizes \operatorname{beauty}(\operatorname{DFS}_{\operatorname{path}}(G)), where is the graph.
The problem has types of subtasks:
K = 1: Output only the maximum value of \operatorname{beauty}(\operatorname{DFS}_{\operatorname{path}}(G)).
K = 2: Output the maximum value of \operatorname{beauty}(\operatorname{DFS}_{\operatorname{path}}(G)) and the graph that achieves this value.
A graph is simple if each edge connects two distinct vertices and no two edges connect the same pair of vertices.
A graph is connected if there is a path between every pair of vertices.
The line contains three integers , , (1 \le n \le 3 \cdot 10^5, n - 1 \le m \le \min(3 \cdot 10^5, \frac{n \cdot (n - 1)}{2}), 1 \le K \le 2) — the number of vertices, number of edges, and type of subtask.
On the first line, output the maximum value of \operatorname{beauty}(\operatorname{DFS}_{\operatorname{path}}(G)).
If K = 2, then also output lines, each containing two integers , (1 \le u, v \le n), representing an edge in the constructed graph .
Note that the order in which you print the edges affects the DFS traversal and the value of the beauty function.
Example 1
Example 2
Example 3
In the third sample, the following graph could be constructed:
Here numbers on edges mean the order of edges
Our DFS tree will be:
The beauty of it is 1^2 + 2^2 + 3^2 + 1^2 + 1^2 = 16.
It can be proven that this is the best answer.
( points): n \le 4, K = 1;
( points): n \le 4, K = 2;
( points): n \le 20, m \le 20, K = 1;
( points): n \le 20, m \le 20, K = 2;
( points): n \le 100, m \le 100, K = 1;
( points): n \le 100, m \le 100, K = 2;
( points): K = 1;
( points): K = 2;