Linear Algebra and Applications
Time limit2sMemory limit256 MB
For a sparse n-by-n matrix A, find the least k with A plus A^2 plus ... plus A^k nonzero in every entry, or output 0 if none exists.
- Level
Medium7 of 10
- Topics
- Graph, BFS, Matrix, Shortest path
- Solved
- No attempts yet
Problem
To celebrate taking a linear algebra course, Cheongung gives Hyowon an matrix as a gift. To make it easy to work with, every entry is a nonnegative integer, and the matrix has at most nonzero entries. In the spirit of taking linear algebra, Hyowon computes and finds that has no more zero entries than . Likewise, has no more zero entries than . Hyowon falls into this train of thought.
"Could there be some for which has no zero entries at all? If such a exists, what is the smallest one?"
Hyowon asks Cheongung about this and receives the answer "If exists, then by the Cayley-Hamilton theorem it is at most ..." For Hyowon, who is not satisfied with this answer, write a program that determines whether such a exists and, if it does, prints its smallest value.
Input
The first line gives the size of the matrix, , where .
The matrix is given over the second line through the -th line. Each line contains integers separated by spaces, and the -th number on the -th line is . Each entry satisfies , and at most of the are greater than .
Output
If a satisfying the condition exists, print the smallest such . If no such exists, print 0.