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. 最近完成的AndroidStudio项目实现思路及应用技术

    主要内容: Android Studio的介绍 AS中个Gradle及Groovy介绍 AS中的依赖管理 Maven以及Nexus私库管理依赖 Gradle对变种代码的管理以及多渠道打包 eclips ...

  2. java代码块的理解

    最近在复习java基础,在看到java代码块的时候,忽然发现自己貌似对于java代码块一无所知,于是赶紧对着一些资料实战演练了一把. 对于java代码块,不难根据名称看出其实就是一些java语句的集合 ...

  3. 主从及转发DNS搭建

    author:JevonWei 版权声明:原创作品 主DNS 安装bind软件包 yum -y install bind systemctl start named systemctl enable ...

  4. 通过官网找到spring的jar包

    1.官网为:https://spring.io/ 2.打开之后,点击:PROJECTS,如图所示: 3.点击第三个:SPRING FRAMEWORK,如图所示: 4.进入之后,找到features,点 ...

  5. UICollectionView中Cell左对齐 居中 右对齐 等间距------你想要的,这里都有

    支持靠左,居中,靠右,等间距对齐. 靠左等间距.png 居中等间距.png 靠右等间距.png #import <UIKit/UIKit.h> typedef NS_ENUM(NSInte ...

  6. 所谓编码--泛谈ASCII、Unicode、UTF8、UTF16、UCS-2等编码格式

    最近在看nodejs的源码,看到stream的实现里面满地都是encoding,不由想起以前看过的一篇文章--在前面的随笔里面有提到过--阮一峰老师的<字符编码笔记:ASCII,Unicode和 ...

  7. Mac系统的终端显示git当前分支

    当我第一次在mac系统下使用git的时候,发现一个问题,git默认是不显示当前所在的分支名称,然后网上查找资料,找到了解决办法,终于可以显示本地当前分支,现在分享如下. 1 进入你的home目录 cd ...

  8. [BT5]信息收集1-1 Dnsenum

    0.工具介绍 The purpose of Dnsenum is to gather as much information as possible about a domain. The progr ...

  9. Java-错误处理机制学习(一)异常处理

    注意:本文介绍Java中的异常处理理论知识及相关语法结构,对于实际应用来说是万万不够的.关于如何高效地使用异常,请查看Java-高效地使用Exception-实践. 异常处理的思想是,当应用程序处于异 ...

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

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多数据库相关内容. 2. 书面作业 1. MySQL数据库基本操作 建立数据库,将自己的姓名.学号作为一条记录插入.(截图,需出现自 ...