hdu 1269 迷宫城堡 Tarjan算法
2016-04-22 19:59
369 查看
题目链接
题意:给出n个节点和m条边,判断这张图是不是强连通图。
第一次学习Tarjan算法,以下是Tarjan算法模板。
题意:给出n个节点和m条边,判断这张图是不是强连通图。
第一次学习Tarjan算法,以下是Tarjan算法模板。
#include <iostream> #include<cstdio> #include<cstring> #include<cmath> #define N 11000 #define M 110000 using namespace std; struct node { int next,v; }e[M]; int head ,dfn ,low ,v ,q ,cnt,scnt,top,n,m,blong ; void init() { memset(head,-1,sizeof(head)); memset(dfn,0,sizeof(dfn)); cnt=top=scnt=0; } void add_edge(int u,int v) { e[cnt].v=v; e[cnt].next=head[u]; head[u]=cnt++; } void tarjan(int u) { int t; dfn[u]=low[u]=cnt++; v[u]=1; q[top++]=u; for(int i=head[u];i!=-1;i=e[i].next) { int c=e[i].v; if(!dfn[c]) { tarjan(c); low[u]=min(low[u],low[c]); } else if(v[c]) low[u]=min(low[u],dfn[c]); } if(dfn[u]==low[u]) { scnt++; do { t=q[--top]; v[t]=0; blong[t]=scnt; }while(t!=u); } } void solve() { for(int i=1;i<=n;i++) if(!dfn[i]) tarjan(i); } int main() { while(~scanf("%d%d",&n,&m)&&(n||m)) { init(); for(int i=0;i<m;i++) { int u,v; scanf("%d%d",&u,&v); add_edge(u,v); } solve(); cout<<(scnt==1?"Yes":"No")<<endl; } }
相关文章推荐
- 例题(8.9) 打印水仙花数 (1041)
- 使用JsonConfig中的setExcludes方法过滤不需要转换的属性
- 【LeetCode】LeetCode——第11题:Container With Most Water
- sqlite、mysql和postgresql对比
- CSS display 属性
- linux
- Android ecludeFromRecents
- 客户端测试
- 实验:C++实验4-项目6
- HDU4177 Avoiding a disaster(模拟时间)
- Problem 1027
- Select Distinct
- 典型用户和用户场景描述
- 站立会议4
- 个人总结4
- CentOS 7安装gVim
- XML特殊转义规则
- Android 程序进入后台 恢复到前台
- iOS动画的暂停与恢复
- 源代码设计、实现、源代码控制