一个矩阵,自乘无限次后能否全为正数?

如果n比较小,可以二分一下,但是这里n很大,乘一次都无法接受

可以考虑实际含义:矩阵看成邻接矩阵,那么0就是没有边,其余就是有边。

我们知道邻接矩阵自乘k次就相当于在原图走k步,无限次后是否有0?也就是图能否强连通。

判断就好,整个是环的情况题目限制不存在。

#include<iostream>
#include<cstdio> using namespace std; const int MAXN=; struct Edge{
int next,to;
}e[MAXN*MAXN*];
int ecnt,head[MAXN];
inline void add(int x,int y){
e[++ecnt].to = y;
e[ecnt].next = head[x];
head[x] = ecnt;
} inline int rd(){
int ret=,f=;char c;
while(c=getchar(),!isdigit(c))f=c=='-'?-:;
while(isdigit(c))ret=ret*+c-'',c=getchar();
return ret*f;
} int n; int bl[MAXN],cnt;
int ins[MAXN],sta[MAXN],top;
int dfn[MAXN],low[MAXN],tot;
void tarjan(int x){
dfn[x]=low[x]=++tot;ins[x]=;sta[++top]=x;
for(int i=head[x];i;i=e[i].next){
int v=e[i].to;
// cout<<x<<" "<<v<<endl<<endl;
if(!dfn[v]){
tarjan(v);
low[x]=min(low[x],low[v]);
}else if(ins[v]){
low[x]=min(low[x],dfn[v]);
}
}
if(dfn[x]==low[x]){
int elm;cnt++;
do{
elm = sta[top--];
ins[elm] = false;
bl[elm] = cnt;
}while(elm != x);
}
} int main(){
n=rd();int x;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if(rd()) add(i,j);
for(int i=;i<=n;i++) if(!dfn[i]) tarjan(i);
if(cnt!=) return puts("NO"),;
puts("YES");
return ;
}

[CF] 402 E. Strictly Positive Matrix的更多相关文章

  1. CodeForces 402 E Strictly Positive Matrix

    Strictly Positive Matrix 题解: 如果原来的 a[i][j] = 0, 现要 a[i][j] = 1, 那么等于 sum{a[i][k] + a[k][j]} > 1. ...

  2. 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 f ...

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

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

  4. CF402E Strictly Positive Matrix 传递闭包用强连通分量判断

    题目链接:http://codeforces.com/problemset/problem/402/E /**算法分析: 这道题考察了图论基本知识,就是传递闭包,可以构图用强联通分量来判断 */ #i ...

  5. codeforces 402E - Strictly Positive Matrix【tarjan】

    首先认识一下01邻接矩阵k次幂的意义:经过k条边(x,y)之间的路径条数 所以可以把矩阵当成邻接矩阵,全是>0的话意味着两两之间都能相连,也就是整个都要在一个强连通分量里,所以直接tarjan染 ...

  6. CF402E Strictly Positive Matrix(矩阵,强联通分量)

    题意 给定一个 n∗n 的矩阵 A,每个元素都非负判断是否存在一个整数 k 使得 A^k 的所有元素 >0 n≤2000(矩阵中[i][i]保证为1) 题解 考虑矩阵$A*A$的意义 ,设得到的 ...

  7. http://codeforces.com/contest/402/problem/E

    E. Strictly Positive Matrix time limit per test 1 second memory limit per test 256 megabytes input s ...

  8. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.10

    Every $k\times k$ positive matrix $A=(a_{ij})$ can be realised as a Gram matrix, i.e., vectors $x_j$ ...

  9. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]Contents

    I find it may cost me so much time in doing such solutions to exercises and problems....I am sorry t ...

随机推荐

  1. 洛谷 - P1012 - 拼数 - 排序

    https://www.luogu.org/problemnew/show/P1012 这道水题居然翻车了,还发现不了bug,服气了.并不是空字符一定比不空要好,要取决于替代它的字符的大小.所以还是直 ...

  2. hihoCoder 1032

    最长回文子串的O(1)算法Manacher算法 #include <iostream> #include <stdio.h> #include <string.h> ...

  3. 【CodeForces - 651C 】Watchmen(map)

    Watchmen 直接上中文 Descriptions: 钟表匠们的好基友马医生和蛋蛋现在要执行拯救表匠们的任务.在平面内一共有n个表匠,第i个表匠的位置为(xi, yi). 他们需要安排一个任务计划 ...

  4. JQuery动态添加表格,然后动态删除不成功问题

    背景: 自己做了一个测试网页,想动态添加表格,然后删除,按照网上的教程写完,发现点击"删除参数"按钮没用 源码: function addtr() { var trinfo = & ...

  5. first-child和last-child选择器 nth-child(n)第几个元素 nth-last-child(n)倒数第几个元素

    :first-child 和  :last-child 分别表示父元素中第一个 或者  最后一个 子元素设置样式,如上图

  6. 浅谈Java中static作用--转

    static表示“全局”或者“静态”的意思,用来修饰成员变量和成员方法,也可以形成静态static代码块,但是Java语言中没有全局变量的概念. 被static修饰的成员变量和成员方法独立于该类的任何 ...

  7. 在 CentOS 环境下安装 .NET Core

    安装步骤: 参见官网 CentOS 会报以下错误: Error downloading packages: dotnet-runtime-2.2-2.2.4-1.x86_64: [Errno 256] ...

  8. 17972 Golden gun的巧克力

    17972 Golden gun的巧克力 时间限制:1000MS  内存限制:65535K提交次数:93 通过次数:13 收入:124 题型: 编程题   语言: G++;GCC;JAVA Descr ...

  9. 摄像头调用,h5调用摄像头进行扫一扫插件备份

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. android开发学习 ------- Retrofit+Rxjava+MVP网络请求的实例

    http://www.jianshu.com/p/7b839b7c5884   推荐 ,照着这个敲完 , 测试成功 , 推荐大家都去看一下 . 下面贴一下我照着这个敲完的代码: Book实体类 - 用 ...