Construct a layered routing network of collectors, hubs, and distributors connecting N inputs to N outputs within given connection and power limits.
Medium6GraphImplementationMathNo attempts yetTime limit2sMemory limit512 MBHenry and Hetty were hired by a networking company, and their first project is a router, the Connect Ethernet Operating Interface 2016. A router is made of:
A node X can send data to a node Y, which is the same as saying Y can receive data from X, when X=Y, or when some node Z exists such that X can send data to Z and there is a direct connection from Z to Y.
When X can send data to Y and X=Y, a data path from X to Y is a set of direct connections {(A1,A2),(A2,A3),…,(AL−1,AL)} with L≥2, A1=X and AL=Y.
The router works properly when all five conditions hold:
The power needed to operate node X is PX=INX×OUTX, where INX is the number of input nodes that can send data to X, and OUTX is the number of output nodes that can receive data from X. The maximum power used by the router is Pmax=max(P1,P2,…,P2N+K).
Build a router that works properly, has exactly N input nodes and exactly N output nodes, uses at most Mlim direct connections, has Pmax≤Plim, and uses at most 500000 nodes in total, so Ntot=2N+K≤500000.
Many routers stay inside those limits, so the output section fixes one of them. Print that router.
One line holds three integers N, Mlim and Plim: the number of input nodes, which equals the number of output nodes, the largest number of direct connections allowed, and the largest power allowed.
1≤N≤10000, 1≤Mlim≤500000, N≤Plim≤1000000. Every input node X has INX=1 and OUTX=N, so an answer exists only when Plim≥N.
In every test case the router described in the output section uses at most Mlim direct connections.
Print the router built by the rules below. It works properly and its maximum power is at most Plim.
Let
g=min(N,⌊NPlim⌋),c=⌊Plim⌋,b=⌊gc⌋.
Cut the input nodes into S=⌈N/g⌉ groups of g consecutive nodes. Input group j holds the input nodes (j−1)g+1 to min(jg,N), so the last group can be smaller than the others. Cut the output nodes the same way: output group j holds the output nodes N+(j−1)g+1 to N+min(jg,N).
Cut the group indices 1 to S into B=⌈S/b⌉ blocks of b consecutive indices. Block k holds the group indices (k−1)b+1 to min(kb,S), so group j lies in block ⌊(j−1)/b⌋+1. The input side and the output side use the same blocks.
There are K=2S+B2 internal nodes, numbered in this order:
The direct connections, in the order they must be printed:
That router has Ntot=2N+2S+B2 and M=2N+2SB.
On the first line print Ntot and M, separated by a space. On each of the next M lines print two integers X and Y, separated by a space, meaning a direct connection runs from node X to node Y. Keep the order given above.