Telephone Network
Time limit2sMemory limit128 MB
Route m disjoint input-output requests through a recursive Clos-like network of binary switches, choosing at each layer the lexicographically smallest routing bit string.
- Level
Hard8 of 10
- Topics
- Graph, Greedy, Recursion, Bit manipulation
- Solved
- No attempts yet
Problem
A telephone company wants to build a new telephone network in a city. Every person in the city should be able to call every other person. Building a direct connection between every pair of persons is impossible, so the company uses a network made of several layers.
The network switch in layer is written . A switch consists of one input, one output and a cable that connects the input to the output. A switch with consists of inputs, outputs and two switches . Input of () is connected by a cable to input of each of the two switches . Output of is connected the same way to output of each of the two switches .
Consider a network whose outermost layer is a single switch . From any input and any output of there is exactly one path to each of the switches, so any input of can be connected to any of its outputs, and naming the switch that carries the connection fixes the whole path.
The switches inside are numbered to . Switch number is defined as follows. Write in binary as . These bits describe a path from an input of down to switch number : for each , means the path leaves into the first of the two switches it consists of, and means it leaves into the second one. The path ends at the same switch no matter which input of it starts from, and that switch is the one numbered .
Several connections are sometimes needed at the same time. To avoid interference, every input and every output of every switch () may be used by at most one connection. Given a set of connection requests, route every request so that no two connection paths share an input or an output of any switch.
Input
The first line contains a positive integer, the number of test cases, at most . Each test case follows in this form.
- One line with two integers () and (): the layer of the outermost switch and the number of connection requests.
- lines, the -th with two integers and (), a request to connect input of to output . The values are pairwise distinct, and the values are pairwise distinct as well.
Output
For each test case, print one line with integers , where is the number of the switch that carries the connection from input to output . The connection paths must be pairwise disjoint, and at least one such routing always exists.
Several routings are usually valid, so print the canonical one. It is defined layer by layer, from the outermost layer inward. In layer every request enters one of the two switches that the switch holding it consists of. Write the choices of that layer as the bit string , where is for the first of the two switches and for the second, so is bit of . Start from all valid routings and keep the ones whose bit string for layer is smallest in lexicographic order. Among those keep the ones whose bit string for layer is smallest, and continue down to layer . Exactly one routing survives.
Hint
The number of an switch spells out its path. For , switch number has bits , so the path takes the second inside , then the first inside that , then the second inside that .