Olympiad in Mathematics - Category Programming
Problem Set of the First Round
2001/2002


P-I-1

Consider a square matrix A with dimensions n times n, containing only zeros and ones. By row and column exchanges we will mean an exchange of two arbitrary rows and two arbitrary columns of the matrix, respectively. In this task, we focus on the following question: Can one transform a matrix to a matrix containing only ones on the main diagonal, using only the row and column exchanges? The main diagonal of a matrix A consists of elements A[1,1], A[2,2], ..., A[n,n].

In many applications (e.g. when solving systems of linear equations), it can be advantageous to transform a given matrix to an equivalent matrix containing only "large" elements on the main diagonal. In our task, "small" elements are represented by zeros, and "large" elements are represented by ones.

Task

Design an efficient algorithm, which will transform a given matrix to a matrix containing only ones on its diagonal, by using only the row and column exchanges. Otherwise, the algorithm should determine that it is not possible.

Example:
Matrix A:
0 1 0 1
1 1 0 1
1 1 0 0
0 0 1 0
Result:
1 1 0 0
1 1 0 1
0 0 1 0
0 1 0 1

The matrix can be transformed. Exchange the third and the fourth row and then the first and the fourth column.

Example:
Matrix A:
0 1 0
1 1 0
1 1 0
Result:
The matrix can not be transformed.


P-I-2

Researchers from the Piping Research Institute developed a new type of robot. Its purpose is to clean heat-conducting pipelines. The robot moves through the system of pipes according to a pre-programmed fixed schedule. The robot must pass each pipe of the system twice: while a chemical cleaning is performed in the first pass, a mechanical finalization is performed in the second pass. It is not important in which direction a pipe is cleaned, and the robot can enter it from both ends. However, once a pipe is entered, the robot must continue until its end.

A system of pipes contains n nodes, which are numbered from 1 to n. There are m heat-conducting pipes numbered from 1 to m in the system. Each pipe connects two nodes, doesn't branch or cross with other pipes. Each pair of nodes is connected with at most one pipe. You can assume that the pipe system is connected, i.e. the robot can reach all locations in the system. The robot starts in the node number 1, where it should return when it's done with the cleaning.

Task

Write a program, which will help to plan the path of the cleaning robot. The program will read a description of the pipe system, and determine whether there exists a path starting and ending in the node 1 that is passing each pipe exactly twice. If such a path exists, the program will print it out.

Input specifications. The first line of the input file pipes.in contains two space-separated positive integers n and m (n<=100). Each of the following m lines describes a pipe. It contains two space-separated integers - the ending nodes of the pipe.

Output specifications. The output file pipes.out will consist of a single line with 2m+1 integers: space-separated node numbers in the order of robot's visits. The first and the last integer on the line must be 1. If a path doesn't exist, the output file should contain a single line with a word NO. If multiple solutions exist, print an arbitrary one.

Example:
File pipes.in
5 6
1 3
1 4
1 5
2 4
3 5
4 5
File pipes.out
1 3 5 4 1 5 4 2 4 1 5 3 1


P-I-3

Given are 2n points in a plane. n of them are white and n of them are black. A fair line is a line that:

Task

Write a program, which will read the coordinates of the white and black points from a file line.in. The program will then write a fair line to the output file line.out.

You can assume that no 3 input points lie on the same line and that the point [0,0] doesn't lie on any line connecting any two of the input points. All points have integer coordinates.

Input specifications. The file line.in contains an integer n on the first line. Each of the following 2n lines contains space-separated coordinates of one of the points. The first n points are white, the remaining are black.

Output specifications The file line.out will contain an arbitrary point of the fair line different from [0,0]. If there is no fair line for a given set of points, the file will contain a single word NO.

Example:

File line.in
2
0 1
2 -1
-1 -1
-1 2

File line.out
2 1

Another correct solution:
-1.0 2.9

a graphical representation of the input and output


Hint: Let A1=[x1,y1], A2=[x2,y2] and A3=[x3,y3] are points in a plane. If the value (x2-x1)(y3-y1)-(x3-x1)(y2-y1) is positive, the point A3 lies left to the directed line A1A2. If the value is negative, it lies right to the line, and if the value is 0, the point A3 lies on the line A1A2.


P-I-4

Comparator nets

Comparator nets are used in the design of parallel algorithms. It is also possible to implement them in electronic circuits. A comparator is a simple part which receives two numbers on the input, compares them and returns the lower of them on its upper output and the greater of them on its bottom output. Comparators can be combined into more complex circuits, here called comparator nets.

A comparator net consists of n horizontal wires, which are connected by comparators at arbitrary locations. The comparators are arranged into layers, representing the steps of the computation. In the beginning of the computation (step 0), the net takes n input numbers - each wire takes one. After the computation step k-1 is finished, the outputs from the step k-1 are sent to the inputs of the comparators in the layer k. A comparator in the layer k connects two wires (not necessarily neighboring ones). If the bottom wire contains a value lower than the upper wire, the comparator exchanges the values. Otherwise the values are not modified and they are simply copied further. One layer may contain more than one comparator (and the computation is performed in parallel). However, each wire can enter only one comparator in each layer. When the computation is finished, the outputs contain the same values as inputs, but their order can be different.

The wires are represented graphically as horizontal lines, the comparators as vertical connections of their input wires. The comparators from the same layer are horizontally aligned, or appear in several neighboring columns. The layers are divided by a dashed line. The computation flows from left to right.

When designing nets, we are trying to construct them in such a way that the computational time would be the shortest possible, i.e. the net should have the least number of layers. Another important criterium is the number of comparators (this has a direct influence on the cost of the circuit production).

Example.
a picture of a comparator net

Consider the leftmost net on the figure. This net receives four inputs and returns them sorted in an ascending order. After the first two steps of computation, the smallest input will be on the first or the second wire, and the largest input on the third or the fourth. The following two steps place the smallest and the largest elements to their place and the last step will finish sorting. Note that the first and the second comparators (and the third and the fourth one) can be merged into a single layer. The resulting and faster net is in the middle of the figure. A computation flow for input 4, 1, 2, 3 is shown on the right.

Example. Construct a net, which takes n numbers on the input and outputs the smallest of them on the wire 1 (the order of the remaining numbers does not matter). Suppose n is a power of two.

Solution. The net will be constructed recursively. Label S(n) a net, which solves the task for n inputs. If n=1, S(n) does not contain any separator, since there's only one input. Let thus n>1 and let the inputs be divided into upper and lower halves. We apply the net S(n/2) to both - they can work in parallel. When finished, the wire 1 contains the smallest value of the upper half and the wire n/2 + 1 contains the smallest value of the lower half. Adding a single comparator between wires 1 and n/2 + 1 will result in the global minimum on wire 1. The net S(n) thus consists of two nets S(n/2) and one comparator. The following figure shows the construction of the net S(n) on the left, an example for n=8 is shown on the right.

a picture of a comparator net

Notice that the depth of the recursion is log2 n, because the size of the input decreases to half in each recursive step. Each recursive level contributes to the resulting net by one layer and thus the the net S(n) has log2 n layers. The number of used comparators is 1 in the last layer, and this number doubles in each layer in backward direction. Let the number of inputs is n=2k. Then the number of comparators used is 1+2+4+...+2k-1=2k-1=n-1 (the sum of a geometric sequence). Our net thus contains O(log n) layers and used O(n) comparators.

Tasks

a)
Write a program, which simulates comparator networks. The program will receive a comparator network and the values on the input. Design and describe your own input format for saving comparator nets into a file. Your program should compute the result of the computation of the comparator net and it should be able to display the computation flow in graphics or semigraphics. Include examples of inputs and outputs for your program.
b)
The input contains n-1 numbers, ordered in an ascending order. (the first n-1 inputs). The last input contains an arbitrary number. Design a comparator net, which will insert the last number to this sequence (i.e. the output will contain all n numbers sorted. Suppose that n is a power of 2. Try to make your comparator net to compute as fast as possible.