Two Stage Router

Given N, a connection cap, and a power cap, construct a two-stage router graph with collectors and distributors that satisfies all correctness conditions.

Medium5GraphImplementationMathNo attempts yetTime limit2sMemory limit512 MB

Problem

A router is built from nodes and one way connections between pairs of distinct nodes:

  • NN input nodes, numbered 11 to NN;
  • NN output nodes, numbered N+1N+1 to 2N2N;
  • KK internal nodes, numbered 2N+12N+1 to 2N+K2N+K;
  • MM one way connections.

Node XX can send data to node YY if X=YX = Y, or if there is a node ZZ such that XX can send data to ZZ and a connection runs from ZZ to YY. When XYX \ne Y and XX can send data to YY, a data path from XX to YY is a set of connections {(A1,A2),(A2,A3),,(AL1,AL)}\{(A_1, A_2), (A_2, A_3), \dots, (A_{L-1}, A_L)\} with L2L \ge 2, A1=XA_1 = X and AL=YA_L = Y.

The router works properly when all of these hold:

  • every input node can send data to every output node;
  • an input node receives data only from itself;
  • an output node sends data only to itself;
  • if XYX \ne Y and XX can send data to YY, then YY cannot send data to XX;
  • if XYX \ne Y and XX can send data to YY, then the data path from XX to YY is unique. In particular at most one connection joins any two nodes.

The power of node XX is PX=INX×OUTXP_X = \mathrm{IN}_X \times \mathrm{OUT}_X, where INX\mathrm{IN}_X is the number of input nodes that can send data to XX and OUTX\mathrm{OUT}_X is the number of output nodes that can receive data from XX. The maximum power of the router is Pmax=max(P1,P2,,P2N+K)P_{max} = \max(P_1, P_2, \dots, P_{2N+K}).

You are given NN, MlimM_{lim} and PlimP_{lim}. Build the router described in the output section. That router works properly, uses at most MlimM_{lim} connections, keeps PmaxP_{max} at most PlimP_{lim}, and uses at most 500000 nodes in total.

Input

One line with three integers NN, MlimM_{lim} and PlimP_{lim}, separated by spaces (1N50001 \le N \le 5000, NPlim109N \le P_{lim} \le 10^9, 1Mlim5000001 \le M_{lim} \le 500000).

The router described in the output section uses at most MlimM_{lim} connections.

Output

Let s=min(N,Plim/N)s = \min(N, \lfloor P_{lim} / N \rfloor) and g=N/sg = \lceil N / s \rceil. Build the router this way.

  • Split the input nodes into gg groups of ss consecutive nodes: input node ii belongs to group i/s\lceil i / s \rceil. The last group can hold fewer than ss nodes.
  • Split the output nodes the same way: output node N+jN + j belongs to group j/s\lceil j / s \rceil.
  • Add gg collector nodes. The collector of group cc is node 2N+c2N + c.
  • Add gg distributor nodes. The distributor of group dd is node 2N+g+d2N + g + d.
  • Connect every input node to the collector of its own group.
  • Connect every collector to every distributor.
  • For every group, connect its distributor to each output node of that group.

On the first line print the total number of nodes Ntot=2N+2gN_{tot} = 2N + 2g and the number of connections M=2N+g2M = 2N + g^2, separated by a space. On each of the next MM lines print two integers XX and YY, meaning a connection from node XX to node YY. Sort the connections by XX in increasing order, and by YY in increasing order when XX is equal.