Laplacian matrices (2) X59053


Statement
 

pdf   zip

A square matrix MM of size n×nn\times{n} that contains only zeros and ones, and only zeros in the diagonal, is called a binary matrix.

The Laplacian of a binary matrix MM is another n×nn\times{n} square matrix LL with the following content:

  • All cells LiiL_{ii} (i.e. the diagonal of LL), are equal to the number of ones in row ii of MM.

  • Any other cell in LL contains the same value than the corresponding cell in MM but with opposite sign (since MM contains only 0 and 1, these LL cells will contain 0 or -1 accordingly).

For example, the following binary matrix 5×55\times5:

 0  1  1  0  0
 1  0  0  1  1
 0  1  0  0  1 
 1  1  1  0  1
 0  0  0  0  0

has as Laplacian the following Matrix:

 2 -1 -1  0  0
-1  3  0 -1 -1
 0 -1  2  0 -1 
-1 -1 -1  4 -1
 0  0  0  0  0
  • Write a function count_row(a,i) that receives a binary matrix aa and a row number ii, and returns how many 1 there are in the ii-th row of aa.

  • Write a program that reads a sequence of binary matrices and prints its Laplacian following the format shown in the examples. The program must use the function count_row.

Input

Input is a sequence of cases. A case is a number n>0n > 0, the dimension of the coming binary matrix, followed by n×nn\times{n} integers describing the matrix: all of them either 0 or 1, where all the diagonal entries are zero.

Output

The output must contain the Laplacian transform of each of the matrices in the input in the same order. One empty line should appear after each case.

Public test cases
  • Input

    2
    0 1
    1 0
    3
    0 0 0
    0 0 0
    0 0 0
    3
    0 1 0
    0 0 1
    1 1 0
    3
    0 1 1
    0 0 1
    1 1 0
    4
    0 1 1 0
    1 0 0 1
    1 1 0 1
    0 1 1 0
    

    Output

    1 -1 
    -1 1 
    
    0 0 0 
    0 0 0 
    0 0 0 
    
    1 -1 0 
    0 1 -1 
    -1 -1 2 
    
    2 -1 -1 
    0 1 -1 
    -1 -1 2 
    
    2 -1 -1 0 
    -1 2 0 -1 
    -1 -1 3 -1 
    0 -1 -1 2 
    
    
  • Input

    3
    0 0 1
    0 0 1
    0 0 0
    4
    0 0 0 0
    1 0 0 0
    1 1 0 0
    1 1 1 0
    

    Output

    1 0 -1 
    0 1 -1 
    0 0 0 
    
    0 0 0 0 
    -1 1 0 0 
    -1 -1 2 0 
    -1 -1 -1 3 
    
    
  • Information
    Author
    José Luis Balcázar
    Language
    English
    Official solutions
    Python
    User solutions
    Python