Codeforces Round #236 (Div. 2)E. Strictly Positive Matrix(402E)

                          E. Strictly Positive Matrix

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 ton 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 integers ai1, 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

21 00 1

output

NO

input

54 5 6 1 21 2 3 4 56 4 1 2 41 1 1 1 14 4 4 4 4

output

YES

题意: 矩阵matrix[n][n], 对角线上元素不全为0, 其他元素的值大于等于0. 问是否存在k 使得 矩阵的k次幂之后 元素的值全部大于0.

设 A = B2  , B 为一邻接矩阵, 则A[i][j] 的实际意义为 从i到j 经过一个点(不包含i, j)的路径的个数。。。对于k次幂就是经过k-1个点的路径的个数了。

要使 A[i][j] 大于0 ,(有向图) i 到j必须连通。。 A[j][i] > 0 && A[i][j] < 0 ,则i, j之间必能形成回路。

可以理解为 从任意点开始都可以遍历整个有向图。

建两个图(正向 反向),分别跑dfs。。判断一下即可。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 vector<int>G1[2015];
 4 vector<int>G2[2015];
 5 int  c1, c2;
 6 bool vis[2015];
 7 void dfs1(int r)
 8 {
 9     c1++;
10     vis[r] = true;
11     for (int i = 0; i < G1[r].size(); i++)
12     {
13         if (!vis[G1[r][i]])
14             dfs1(G1[r][i]);
15     }
16 }
17 void dfs2(int r)
18 {
19     c2++;
20     vis[r] = true;
21     for (int i = 0; i < G2[r].size(); i++)
22     {
23         if (!vis[G2[r][i]])
24             dfs2(G2[r][i]);
25     }
26 }
27 int main()
28 {
29     #ifndef ONLINE_JUDGE
30         freopen("in.txt","r",stdin);
31     #endif
32     int n;
33     while (~scanf ("%d", &n))
34     {
35         for (int i = 0; i <= n; i++)
36         {
37             G1[i].clear();
38             G2[i].clear();
39         }
40         for (int i = 0; i < n; i++)
41         {
42             for (int j = 0; j < n; j++)
43             {
44                 int x;
45                 scanf ("%d", &x);
46                 if (i != j && x)
47                 {
48                     G1[i+1].push_back(j+1);
49                     G2[j+1].push_back(i+1);
50                 }
51             }
52         }
53         c1 = c2 = 0;
54         memset(vis, false, sizeof(vis));
55         dfs1(1);
56         memset(vis, false, sizeof(vis));
57         dfs2(1);
58         printf("%s\n", (c1 == n && c2 == n) ? "YES" : "NO");
59     }
60     return 0;
61 }
时间: 2024-12-17 00:29:19

Codeforces Round #236 (Div. 2)E. Strictly Positive Matrix(402E)的相关文章

[CF #236 (Div. 2) E] Strictly Positive Matrix(强联通分量)

题目:http://codeforces.com/contest/402/problem/E 题意:给你一个矩阵a,判断是否存在k,使得a^k这个矩阵全部元素都大于0 分析:把矩阵当作01矩阵,超过1的都当作1,那么a矩阵可表示一个有向图的走一次的连通性,则a^k表示有向图走K次的连通性.既然要求最后都没0,即走了K次后,每个点都能互通,这也说明这个图必然是只有一个强联通分量.于是判断k的存在有无,也就是判断a矩阵表示的有向图是不是只有一个强联通分量.

构造图 Codeforces Round #236 (Div. 2) C. Searching for Graph

题目地址 1 /* 2 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 3 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结束:) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <string> 10 #includ

Codeforces Round #495 (Div. 2) D. Sonya and Matrix

链接:http://codeforces.com/contest/1004/problem/D 题意:给你t个数字(a1,a2......at),要你组成一个n x m的矩阵,这个矩阵满足这样的条件 ①矩阵里面的元素到"0"这个元素的曼哈顿距离为元素值大小.曼哈顿距离:两个点坐标差的绝对值之和. 现在问你的是输出n,m以及元素"0"的坐标(x,y);如果不存在这样的矩阵,输出"-1". 注意:起点坐标为(1,1). 分析:首先我们假设"

Codeforces Round #277 (Div. 2) B.OR in Matrix 模拟

B. OR in Matrix Let's define logical OR as an operation on two logical values (i. e. values that belong to the set {0, 1}) that is equal to 1 if either or both of the logical values is set to 1, otherwise it is 0. We can define logical OR of three or

Codeforces Round #315 (Div. 1)

A. Primes or Palindromes? time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and un

Codeforces Round #275 (Div. 1)A. Diverse Permutation 构造

Codeforces Round #275 (Div. 1)A. Diverse Permutation Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/482/problem/A Description Permutation p is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct posi

Codeforces Round #245 (Div. 1)——Tricky Function

l and dished out an assist in the Blackhawks' 5-3 win over the Nashville Predators.Shaw said just playing with the Blackhawks was enough motivation for him."Positive, I'm playing in the NHL," Shaw said after Sunday's win. "What can't you be

Codeforces Round #279 (Div. 2) ABCD

Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name     A Team Olympiad standard input/output 1 s, 256 MB  x2377 B Queue standard input/output 2 s, 256 MB  x1250 C Hacking Cypher standard input/output 1 s, 256 MB  x740 D Chocolate standard input/

Codeforces Round #362 (Div. 2) C. Lorenzo Von Matterhorn LCA(最近公共祖先)

C. Lorenzo Von Matterhorn time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Barney lives in NYC. NYC has infinite number of intersections numbered with positive integers starting from 1. Ther