E. Strictly Positive Matrix
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 nfrom 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).

Examples
input
2
1 0
0 1
output
NO
input
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
YES
题意:给你一个矩阵然后问你他的任意K次幂之后他是否全部大于0,一开始看上去很难,其实就是一个n个点的图是否全联通,就是从每一个点出发能到达所以其他点(学过离散数学应该一下就想到了)。
题解:用bitset<N>F[N]对矩阵进行预处理,1表示能到,0表示不能到。两重循环找到每一个点能到的点,这里采用位运算|=按位或,能省掉一重循环。这样之后如果图是全联通的,f[N][N]应该全为);
下面代码
#include<iostream>
#include<cstdio>
#include<bitset>
const int N=;
using namespace std;
bitset<N>f[N];
int n;
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
int t;
scanf("%d",&t);
f[i][j]=t>; //大于0就用1表示联通小与0则为0
}
}
for(int j=;j<=n;j++)
{
for(int i=;i<=n;i++)
{
if(f[i][j])f[i]|=f[j]; //如果i到j联通,则i与j按位或j能到的点i也能到
}
}
for(int i=;i<=n;i++)
{
if(f[i].count()!=n)
{
puts("NO");return ;
}
}
puts("YES");
return ;
}

有个我也不太理解的地方,为什么那个进行位运算的两重循环不能反过来,路过的大佬们求教一下


http://codeforces.com/contest/402/problem/E的更多相关文章

  1. codeforces.com/contest/325/problem/B

    http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...

  2. [E. Ehab's REAL Number Theory Problem](https://codeforces.com/contest/1325/problem/E) 数论+图论 求最小环

    E. Ehab's REAL Number Theory Problem 数论+图论 求最小环 题目大意: 给你一个n大小的数列,数列里的每一个元素满足以下要求: 数据范围是:\(1<=a_i& ...

  3. http://codeforces.com/contest/555/problem/B

    比赛时虽然贪了心,不过后面没想到怎么处理和set的排序方法忘了- -,其实是和优先队列的仿函数一样的... 比赛后用set pair过了... #include <bits/stdc++.h&g ...

  4. http://codeforces.com/contest/610/problem/D

    D. Vika and Segments time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. http://codeforces.com/contest/612/problem/D

    D. The Union of k-Segments time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  6. http://codeforces.com/contest/536/problem/B

    B. Tavas and Malekas time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. http://codeforces.com/contest/535/problem/C

    C. Tavas and Karafs time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. http://codeforces.com/contest/838/problem/A

    A. Binary Blocks time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. codeforces.com/contest/251/problem/C

    C. Number Transformation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

随机推荐

  1. Freemarker的使用方法

    1. Freemarker概念 1.1 什么是freemarker FreeMarker是一个用Java语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与Web容器无关,即在Web运 ...

  2. input长度随输入内容动态变化 input光标定位在最右侧

    <input type="text" onkeydown="this.onkeyup();" onkeyup="this.size=(this. ...

  3. Jquery 绑定标签事件

    为子元素绑定: $('#foreachResult').delegate('td', 'click', function () {            alert($(this).text());  ...

  4. JS嵌套循环的典型练习题

    1.斐波那契数列 ①分数 <script type="text/javascript">    var a = 1   var b = 1   var c    for ...

  5. web程序员标准环境之DreamWeaver【…

    Adobe Dreamweaver,简称"DW",中文名称 "梦想编织者",是美国MACROMEDIA公司开发的集网页制作和管理网站于一身的所见即所得网页编辑器 ...

  6. 团队作业4---第一次项目冲刺(AIpha版本)第二天

    一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 1.界面 完成了主页及登录页面 2.功能 完成了后端数据处理的全部基本功能:a.数据结构设计及数据交互操作 b.博客页面数据采 ...

  7. 201521123066《Java程序设计》第七周学习总结

    第7周-集合 本周学习总结 其他注意点: (1)List主要特征: 其元素以线性方式存储 集合中允许存放重复对象 (2)Set中的对象: 不按特定方式排序 没有重复对象 Set最多有一个null元素 ...

  8. 201521123067 《Java程序设计》第13周学习总结

    201521123067 <Java程序设计>第13周学习总结 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 书面作业 Q1. 网络基 ...

  9. 201521123075 《Java程序设计》第12周学习总结

    1. 本周学习总结 2. 书面作业 将Student对象(属性:int id, String name,int age,double grade)写入文件student.data.从文件读出显示. 1 ...

  10. 201521123113《Java程序设计》第10周学习总结

    1. 本周学习总结 2. 书面作业 本次PTA作业题集异常.多线程 Q1.finally 题目4-2 1.1 截图你的提交结果(出现学号) 1.2 4-2中finally中捕获异常需要注意什么? fi ...