首先认识一下01邻接矩阵k次幂的意义:经过k条边(x,y)之间的路径条数

所以可以把矩阵当成邻接矩阵,全是>0的话意味着两两之间都能相连,也就是整个都要在一个强连通分量里,所以直接tarjan染色,如果只有一个色块的话就是YES否则都是NO(其实应该能更简单一些,不过tarjan比较顺手)

还有就是我没仔细看题判了对角巷为0就NO结果WA……

#include<iostream>
#include<cstdio>
using namespace std;
const int N=2005;
int n,h[N],cnt,low[N],dfn[N],tot,s[N],top,col;
bool v[N];
bool f;
struct qwe
{
int ne,to;
}e[N*N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
void add(int u,int v)
{
cnt++;
e[cnt].ne=h[u];
e[cnt].to=v;
h[u]=cnt;
}
void tarjan(int u)
{
dfn[u]=low[u]=++tot;
v[s[++top]=u]=1;
for(int i=h[u];i;i=e[i].ne)
{
if(!dfn[e[i].to])
{
tarjan(e[i].to);
low[u]=min(low[u],low[e[i].to]);
}
else if(v[e[i].to])
low[u]=min(low[u],dfn[e[i].to]);
}
if(dfn[u]==low[u])
{
col++;
while(s[top]!=u)
v[s[top--]]=0;
v[s[top--]]=0;
}
}
int main()
{
n=read();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
int x=read();
if(i!=j&&x)
add(i,j);
}
for(int i=1;i<=n;i++)
if(!dfn[i])
tarjan(i);
if(col==1)
puts("YES");
else
puts("NO");
return 0;
}

codeforces 402E - Strictly Positive Matrix【tarjan】的更多相关文章

  1. codeforces 949C - Data Center Maintenance【tarjan】

    首先转换图论模型,把某个客户一个终端的维修时间(+1)%h之后和另一个终端维修时间一样,这样的两个终端连一条有向边,表示推后一个终端就必须推后另一个 然后tarjan缩点,一个scc里的终端是要一起推 ...

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

  4. 28. Search a 2D Matrix 【easy】

    28. Search a 2D Matrix [easy] Write an efficient algorithm that searches for a value in an mx n matr ...

  5. [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】

    [CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...

  6. Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))

    B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. 【tarjan】BZOJ2140-稳定婚姻

    又名NTR的故事 [题目大意] n对夫妻Bi和Gi.若某男Bi与某女Gj曾经交往过,他们有私奔的可能性.不妨设Bi和Gj旧情复燃,进而Bj会联系上了他的初恋情人Gk,以此递推.若在Bi和Gi离婚的前提 ...

  8. Codeforces Round #540 (Div. 3) C. Palindromic Matrix 【暴力】

    任意门:http://codeforces.com/contest/1118/problem/C C. Palindromic Matrix time limit per test 2 seconds ...

  9. codeforces 400 C Inna and Huge Candy Matrix【模拟】

    题意:给出一个矩形的三种操作,顺时针旋转,逆时针旋转,对称,给出原始坐标,再给出操作数,问最后得到的坐标 画一下模拟一下操作就可以找到规律了 #include<iostream> #inc ...

随机推荐

  1. dynamic-imports & web components & html dynamic import

    dynamic-imports web components & html dynamic import https://github.com/webcomponents/html-impor ...

  2. multi cookie & read bug

    js cookie multi cookie & read bug document.cookie; // "access_token_test=eyJhbGciOiJIUzI1Ni ...

  3. ibatis的初识

    在工作中,服务端的框架基本上是struts+spring+ibatis+velocity.ibatis曾经没有接触到,而曾经使用的hibernate在公司居然没碰着.同样都是数据库封装,为什么没有选择 ...

  4. java和c/c++通过JNI相互调用

    JNI :Java Native Interface 随便找几篇文章看下就掌握了 http://www.cnblogs.com/icejoywoo/archive/2012/02/22/2363709 ...

  5. MicroPython实现wifi干扰与抓包

    0×00前言 之前做的WIFI攻击实验都是基于arduino环境开发的,最近想尝试一下使用micropython完成deautch(解除认证)攻击.本次开发板使用的还是TPYBoardv202. 0× ...

  6. Android和MVC

    Activity和Android的mvc模式 http://blog.csdn.net/dengshengjin2234/article/details/8502097   //android涉及到的 ...

  7. AutoCAD如何设置线宽

    一般要求粗实线粗实线0.4,细实线0.2mm. 1 先打开图层特性管理器,新建一个图层,专门放粗实线(我起名叫"我的粗实线",颜色设置为紫色,线宽为0.4mm),此前的乱七八糟的图 ...

  8. VC++ 提示无法打开包括文件“iostream.h”怎么办

    把 //#include "iostream.h" 改成 #include<iostream> using namespace std;                 ...

  9. Linux系统下ssh登陆很慢的解决办法

    很多的Linux用户发现连接上Linux服务器在输入用户名之后还要再等一下才能输入密码,时间过长了,现在小白与大家分享一下如何解决ssh登陆问题的问题,希望对您有所帮助. 1.我们平时登陆Linux服 ...

  10. 单一责任原则(SRP)

    1.就一个类而言,应该仅有一个引起它变化的原因. 2.在SRP中定义职责为:“变化的原因”.  如果你想到多个动机去改变这个类,那这个类就有多个职责