HDU1269 迷宫城堡 (强连通图判定)
来源:互联网 发布:房地产网络销售传播图 编辑:程序博客网 时间:2024/05/01 02:55
题意:判定给出的有向图是不是强连通图
Tarjan算法模板题目
#include<cstdio>#include<iostream>#include<algorithm>#include<cmath>#include<set>#include<map>#include<string>#include<cstring>#include<stack>#include<queue>#include<vector>#include<cstdlib>#define lson (rt<<1),L,M#define rson (rt<<1|1),M+1,R#define M ((L+R)>>1)#define cl(a,b) memset(a,b,sizeof(a));#define LL long long#define P pair<int,int>#define X first#define Y second#define pb push_back#define fread(zcc) freopen(zcc,"r",stdin)#define fwrite(zcc) freopen(zcc,"w",stdout)using namespace std;const int maxn=100005;const int inf=999999;vector<int> G[maxn];int dfn[maxn],low[maxn],s[maxn];//依次对应,访问的序号,能到达的最早的,栈int belong[maxn];//连通分量的编号bool ins[maxn];//是否在栈里int cnt,num,top;//连通分量的编号,访问的序号,栈的指针void dfs(int u){ dfn[u]=low[u]=++num; s[++top]=u; ins[u]=true; int N=G[u].size(); for(int i=0;i<N;i++){ int v=G[u][i]; if(!dfn[v]){ dfs(v); low[u]=min(low[u],low[v]); } else if(ins[v]&&dfn[v]<low[u]){ low[u]=dfn[v]; } } if(low[u]==dfn[u]){ cnt++; int v; do{ v=s[top--]; ins[v]=false; belong[v]=cnt; }while(v!=u); }}void Tarjan(int n){ cnt=num=top=0; cl(dfn,0); cl(ins,false); cl(belong,0); for(int i=1;i<=n;i++)if(!dfn[i]){ dfs(i); }}int main(){ int n,m; while(~scanf("%d%d",&n,&m)&&(n||m)){ for(int i=0;i<m;i++){ int a,b; scanf("%d%d",&a,&b); G[a].pb(b); } Tarjan(n); for(int i=1;i<=n;i++){ if(belong[i]!=cnt){ puts("No");goto A; } } puts("Yes"); A:; for(int i=0;i<=n;i++)G[i].clear(); } return 0;}
0 0
- HDU1269 迷宫城堡 (强连通图判定)
- hdu1269 迷宫城堡(强连通图)
- 强连通图之HDU1269 迷宫城堡
- 迷宫城堡(hdu1269,强连通分量)
- HDU1269迷宫城堡(强连通分量)
- HDU1269:迷宫城堡(强连通)
- hdu1269 迷宫城堡 (强连通模板)(有向图)
- HDU1269:迷宫城堡【强连通】
- HDU 1269 迷宫城堡(强连通图的判定)
- HDU 迷宫城堡 1269 (强连通图判定)
- HDU1269 迷宫城堡(tarjan判断强连通)
- HDU1269 迷宫城堡(裸强连通分量)
- [HDU1269]迷宫城堡(Tarjan求强连通分量)
- HDU1269 迷宫城堡(强连通分量tarjan入门题)
- hdu1269迷宫城堡(tarjan求强连通分量模板)
- HDU1269迷宫城堡(强连通tarjan)
- HDU1269 迷宫城堡 强连通 Tarjan
- hdu1269 迷宫城堡 (简单强连通分量)
- Thinking in C++之1.继承与组合概念、区别及优缺点
- iOS对项目中所有加阴影的代码进行优化
- PE文件学习(三)数据目录表之资源
- 手动扩大栈内存,让AC无忧
- 信用卡使用技巧-资深信用卡使用者推荐
- HDU1269 迷宫城堡 (强连通图判定)
- 实例化和类方法
- form表单无刷新提交并得到返回值
- 数学模板 - 计算方法
- DIV水平和垂直居中问题
- hdu1097 A hard puzzle
- 项目经理的“势能”培养 [Y]
- 玩翻的ViewPager
- 安装 HoldemManager2 的一点经历