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
  1. 2
    1 0
    0 1
output
  1. NO
input
  1. 5
    4 5 6 1 2
    1 2 3 4 5
    6 4 1 2 4
    1 1 1 1 1
    4 4 4 4 4
output
  1. 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[];
  4. vector<int>G2[];
  5. int c1, c2;
  6. bool vis[];
  7. void dfs1(int r)
  8. {
  9. c1++;
  10. vis[r] = true;
  11. for (int i = ; 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 = ; 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 = ; i <= n; i++)
  36. {
  37. G1[i].clear();
  38. G2[i].clear();
  39. }
  40. for (int i = ; i < n; i++)
  41. {
  42. for (int j = ; j < n; j++)
  43. {
  44. int x;
  45. scanf ("%d", &x);
  46. if (i != j && x)
  47. {
  48. G1[i+].push_back(j+);
  49. G2[j+].push_back(i+);
  50. }
  51. }
  52. }
  53. c1 = c2 = ;
  54. memset(vis, false, sizeof(vis));
  55. dfs1();
  56. memset(vis, false, sizeof(vis));
  57. dfs2();
  58. printf("%s\n", (c1 == n && c2 == n) ? "YES" : "NO");
  59. }
  60. return ;
  61. }

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

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

    题目:http://codeforces.com/contest/402/problem/E 题意:给你一个矩阵a,判断是否存在k,使得a^k这个矩阵全部元素都大于0 分析:把矩阵当作01矩阵,超过1 ...

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

    题目地址 /* 题意:要你构造一个有2n+p条边的图,使得,每一个含k个结点子图中,最多有2*k+p条边 水得可以啊,每个点向另外的点连通,只要不和自己连,不重边就可以,正好2*n+p就结束:) */ ...

  3. 贪心 Codeforces Round #236 (Div. 2) A. Nuts

    题目传送门 /* 贪心:每一次选取最多的线段,最大能放置nuts,直到放完为止,很贪婪! 题目读不懂多读几遍:) */ #include <cstdio> #include <alg ...

  4. Codeforces Round #236 (Div. 2)

    A. Nuts time limit per test:1 secondmemory limit per test:256 megabytesinput:standard inputoutput:st ...

  5. Codeforces Round #542(Div. 2) A.Be Positive

    链接:https://codeforces.com/contest/1130/problem/A 题意: 给n个数,找出一个非0整数d,使所有n个数除以整数d后,数组中正数的数量>= n/2. ...

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

    题目大意 我们说一个无向图是 p-interesting 当且仅当这个无向图满足如下条件: 1. 该图恰有 2 * n + p 条边 2. 该图没有自环和重边 3. 该图的任意一个包含 k 个节点的子 ...

  7. Codeforces Round #277 (Div. 2) B. OR in Matrix 贪心

    B. OR in Matrix Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/486/probl ...

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

    http://codeforces.com/contest/1004/problem/D 题意: 在n×m的方格中,选定一个点(x,y)作为中心点,该点的值为0,其余点的值为点到中心点的曼哈顿距离. ...

  9. Codeforces Round #524 (Div. 2) E. Sonya and Matrix Beauty(字符串哈希,马拉车)

    https://codeforces.com/contest/1080/problem/E 题意 有一个n*m(<=250)的字符矩阵,对于每个子矩阵的每一行可以任意交换字符的顺序,使得每一行每 ...

随机推荐

  1. Robotium -- 针对apk包的测试

    在使用Robotium测试的时候,有时候,测试人员并没有代码权限,而Robotium也可以在只有apk文件进行测试,下面就介绍一下这个过程. 1.设置环境变量 安装jdk环境和sdk环境 2.安装签名 ...

  2. android在广播接收器BroadcastReceiver里面再进行发送广播,造成当前广播接收器不断循环执行问题

    最近在公司处理项目时,用到锁屏状态弹出activity进行提示,类似QQ消息弹屏提示的功能.当中用到了,假如该弹出activity已经位于锁屏界面外时,将不进行再次弹窗,而是发送广播进行通知数据更新, ...

  3. Eclipse中Java文件图标由实心J变成空心J的问题

    在eclipse中空心J的java文件,表示不被包含在项目中进行编译,而是当做资源存在项目中.例如 当是单个文件为空心J的时候 1.右击该文件 -- >BuildPath -->Inclu ...

  4. 数据类型转换中的一些特殊情况(JY06-JavaScript)

    1.字符串的不可变性 字符串定义了后,会一直占据内存空间,企鹅该处内存空间(栈)不可被重新赋值. 2.短路运算 ||.&& 二元运算符,返回参与运算的操作数的原值(原数据类型和原数据) ...

  5. python面对对象编程----2:__init__

    面对对象编程估计我们最早接触到的就是__init__了,也就是实例的初始化处理过程: 1:来看看最基础的__init__ class Card(object): #抽象类Card,并不用于实例化 de ...

  6. 精通Django或Rails框架

    Django是一个开放源代码的Web应用框架,由Python写成. Rubyon Rails 是一个用于开发数据库驱动的网络应用程序的完整框架.

  7. Eclipse 运行ant build.xml

    在命令行cmd运行mvn clean install,ant compiler,提示上述信息,是因为 maven的这个插件要求jdk1.6,但是本地电脑环境变量jdk版本为1.7.将JAVA_HOME ...

  8. 汇编笔记之 ret 、retf和call

    作用: ret  将 栈顶数据出栈到IP retf 将 栈顶数据出栈到IP ,然后再次将栈顶数据出栈到CS 这样一来,可以使程序跳转到已经定义好了的代码段去执行. call 语法 call s0 (如 ...

  9. git add相关

    git add -A stages All git add . stages new and modified, without deleted git add -u stages modified ...

  10. startActivityForResult

    Activity提供了startActivityForResult(Intent intent, int requestCode)方法打开新的Activity,新的Activity关闭后会向前面的Ac ...