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

如果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. BZOJ3834:Solar Panels (分块)

    题意 询问两个区间[smin,smax],[wmin,smax]中是否存在k的倍数,使得k最大 分析 将其转化成\([\frac{smin-1}k,\frac{smax}k],[\frac{wmin- ...

  2. Django学习:模板语法

    一.什么是模板? 只要是在html里面有模板语法就不是html文件了,这样的文件就叫做模板. 二.模板语法分类 一.模板语法之变量:语法为 {{ }}: 在 Django 模板中遍历复杂数据结构的关键 ...

  3. jqgrid 分级标题

    参考地址:http://www.trirand.com/jqgridwiki/doku.php?id=wiki:groupingheadar Grouping of the header should ...

  4. python之self本类对象

    不知道写点啥好,讲的都太绕了 似懂非懂,貌似懂了 以后再补充吧,视频day8_3中的20——60分钟那一部分

  5. 一个Nice的生活主题博客模板

    https://www.bitcron.com/ https://api.bitcron.com/ https://chopstack.com/

  6. [SDOI2016]模式字符串

    Description 给出n个结点的树结构T,其中每一个结点上有一个字符,这里我们所说的字符只考虑大写字母A到Z,再给出长度为m的模式串s,其中每一位仍然是A到z的大写字母.Alice希望知道,有多 ...

  7. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) E

    Description Bear Limak prepares problems for a programming competition. Of course, it would be unpro ...

  8. JavaScript-页面打印正方形,各种三角形与菱形

    一.   正方形 a)   在第一个for循环中控制,一共输出几行.依靠的是,每次输出一行,就会在后面输出一个换行符<br>; b)   在第二个for循环中控制每行输出几个五角星.这样的 ...

  9. iOS UILabel UITextView自适应文本,或文本大小自适应

    //UILabel自适应文本的高度 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(, , , )]; label.numberOf ...

  10. Ionic之ui-sref引入图片,图片部分挡住解决方案

    ionic图片设置大小跟图片像素相同,但是使用ui-sref="parkInfo"上半部分图片会挡住,增加height的高度,就可以显示原本图片 页面: <ion-conte ...