[模板] 强连通分量
tarjan
(讲道理信息传递就是这样的?)
#include<iostream> #include<cstdio> using namespace std; const int MAXN=2048; struct Edge{ int next,to; }e[MAXN*MAXN*2]; 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=0,f=1;char c; while(c=getchar(),!isdigit(c))f=c=='-'?-1:1; while(isdigit(c))ret=ret*10+c-'0',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]=1;sta[++top]=x; for(int i=head[x];i;i=e[i].next){ int v=e[i].to; 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=1;i<=n;i++) for(int j=1;j<=n;j++){ int x=rd(); if(x) add(i,j); } for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i); if(cnt!=1) return puts("NO"),0; puts("YES"); return 0; }
本文来自博客园,作者:GhostCai,转载请注明原文链接:https://www.cnblogs.com/ghostcai/p/9247514.html