「CF403C」Strictly Positive Matrix

2015年1月24日4,3620

You have matrix a of size n × n. Let’s number the rows of the matrix from 1 to n from top to bottom, let’s number the columns from 1 to n from left to right. Let’s use aij to represent the element on the intersection of the i-th row and the j-th column.

Matrix a meets the following two conditions:

  • for any numbers i, j (1 ≤ i, j ≤ n) the following inequality holds: aij ≥ 0;
  • .

Matrix b is strictly positive, if for any numbers i, j (1 ≤ i, j ≤ n) the inequality bij > 0 holds. You task is to determine if there is such integer k ≥ 1, that matrix ak is strictly positive.

Input

The first line contains integer n (2 ≤ n ≤ 2000) — the number of rows and columns in matrix a.

The next n lines contain the description of the rows of matrix a. The i-th line contains n non-negative integersai1, ai2, …, ain (0 ≤ aij ≤ 50). It is guaranteed that .

Output

If there is a positive integer k ≥ 1, such that matrix ak is strictly positive, print “YES” (without the quotes). Otherwise, print “NO” (without the quotes).

Sample test(s)
input

output

input

output

来自zld神犇的题解

学过矩阵的人都知道对于本题来说整个矩阵里的元素只需要分为两种,一种是为0,一种是不为0。

这样我们就得到了一个01矩阵= =

我们看看这题的标签= =

卧槽。。这TM跟图论有什么关系。。

对邻接矩阵比较熟悉的的人没看到这个标签也可能一下子就能想到怎么回事,该矩阵表示对应的有向图,第i行第j列表示第i个点能否直接走到第j个点,而这个矩阵的k次幂中,第i行第j列表示第i个点走恰好k步能否走到第j个点。

于是我们就把这道题转到图论上去了。。由邻接矩阵幂的性质我们猜想当且仅当这个矩阵代表的有向图为强连通图的时候答案为YES。

证明:

当这个矩阵(01矩阵,边权为1)代表的有向图为强连通图的时候,设有自环的那个点为mid,取(U,V)两点使得U–>mid–>V最短路径的长度最大,记为k。

那么我们考虑任意一对(u,v),我们构造一条u–>mid–>v的最短路径,如果这条路径的长度<k,那么我们走mid的自环直到长度补齐为止,而>k显然是不可能的,这个时候我们就找到了这样的正整数k,使得该矩阵的k次幂中每一项全部为正数,此时答案为YES。

(利用上面一段证明我们可以如果k存在那么其上界为2n,因此50分做法只需要取一个矩阵的2n次幂判断即可。)

当这个矩阵代表的不是强连通图,取(u,v)使u始终不能到达v,无论取几次幂,该矩阵中第u行第v列始终为0,此时答案为NO。

直接BFS的时间复杂度为n^3,而用Tarjan算法的话时间复杂度为n^2。

 

avatar
  Subscribe  
提醒