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. HTML <area><map>标签及在实际开发中的应用

    之前,我一直以为HTML <area>是一个鸡肋HTML,估计到了HTML5时代会被废弃的命.但是,最近一查资料,乖乖了个咚,不仅没被废弃,反而发展了,新增了一些标签属性,例如rel,me ...

  2. 创建DNS子域及view

    author:JevonWei 版权声明:原创作品 子域 子域同父域在同一个服务器上 新建子域jevon.danran.com vim /etc/named.rfc1912.zones zone &q ...

  3. 详解HTTPS加速原理

    HTTPS是什么? http叫超文本传输协议,使用TCP端口80,默认情况下数据是明文传送的,数据可以通过抓包工具捕获到,因此在interner上,有些比较重要的站点的http服务器需要使用PKI(公 ...

  4. linux bash 和 sh的区别

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt189 Linux 中的 shell 有很多类型,其中最常用的几种是: Bou ...

  5. 【小白成长撸】--多项式求圆周率PI

    /*程序的版权和版本声明部分: *Copyright(c) 2016,电子科技大学本科生 *All rights reserved. *文件名:多项式求PI *程序作用:计算圆周率PI *作者:Amo ...

  6. Charles 抓包

    声明:本文为依依Love博主原创文章,未经博主允许不得转载   1. 简介: 2. 安装包下载: 3. 安装并替换破解版的jar包 4.设置mac代理 5.  安装证书: 6.  设置手机抓包     ...

  7. 交换机的Ethernet Channel

    端口聚合也叫做以太通道(ethernet channel),主要用于交换机之间连接.由于两个交换机之间有多条冗余链路的时候,STP会将其中的几条链路关闭,只保留一条,这样可以避免二层的环 路产生.但是 ...

  8. 【★】Web精彩实战之

    JS精彩实战之<智能迷宫>      ---宝贵编程经验分享会--- hello大家好,这里是Web云课堂,之前的一年里我们经历了Html和CSS的系统攻城,此时的你们已经是做静态(动静结 ...

  9. 201521123072《java程序设计》第五周学习总结

    201521123072<java程序设计>第五周学习总结 标签(空格分隔): java学习 1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 2. 书面作业 代码 ...

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

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 参考资料: 百度脑图 XMind 2. 书面作业 作业参考文件下载 1.代码阅读:Child压缩包内源代码 1.1 com.p ...