hdu1269 迷宫城堡 (强连通分量Tarjan算法)
2013-09-11 09:25
204 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1269
题解:对于任意的i和j,至少存在一条路径可以从房间i到房间j,也存在一条路径可以从房间j到房间i。求强连通分量==1?
题解:对于任意的i和j,至少存在一条路径可以从房间i到房间j,也存在一条路径可以从房间j到房间i。求强连通分量==1?
#include <stdio.h> #include <string.h> #define MAXN 10001 struct node { int from,to,next; }edge[10*MAXN]; int head[MAXN],instack[MAXN],low[MAXN],dfn[MAXN]; int stack[MAXN],tot,Dindex,top,Bcnt; void Init() {//初始化 tot=0,top=0,Dindex=0,Bcnt=0; memset(head,-1,sizeof(head)); memset(instack,0,sizeof(instack)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); } void addEdge(int from,int to) { edge[tot].from=from; edge[tot].to=to; edge[tot].next=head[from]; head[from]=tot++; } void Tarjan(int x) { int i,u,temp; dfn[x]=low[x]=++Dindex;//时间戳 stack[top++]=x; instack[x]=1; for(i=head[x];i!=-1;i=edge[i].next) { u=edge[i].to; if(!dfn[u]) { Tarjan(u); low[x]=low[x]>low[u]?low[u]:low[x]; } else if(instack[u]&&low[x]>dfn[u]) low[x]=dfn[u]; } if(low[x]==dfn[x]) { Bcnt++; do { temp=stack[--top]; instack[temp]=0; } while (x!=temp); } } int Scan() { char ch; int ret=0; while((ch=getchar())<'0'||ch>'9'); while(ch>='0'&&ch<='9') { ret=ret*10+(ch-'0'); ch=getchar(); } return ret; } int main() { int n,m,i,x,y; while(scanf("%d %d",&n,&m)&&(n+m)!=0) { Init(); while(m--) { //scanf("%d %d",&x,&y); x=Scan(); y=Scan(); addEdge(x,y); } for(i=1;i<=n;++i) { if(!dfn[i]) Tarjan(i); } if(Bcnt==1) printf("Yes\n"); else printf("No\n"); } return 0; }
相关文章推荐
- hdu - 1269 迷宫城堡 (强连通裸题)
- hdu 1269 迷宫城堡 最简单的联通图题 kosaraju缩点算法
- HDU 1269 迷宫城堡
- hdu 1269 迷宫城堡 (并查集)
- 迷宫城堡 HDU - 1269(tarjan求强联通)
- hdu 1269 迷宫城堡(tarjan判连通)
- hdu 1269 迷宫城堡 (强连通入门题)
- HDU 1269 迷宫城堡 (裸的Tarjan算法求强连通分量, 可作模版)
- HDU1269 迷宫城堡(强连通分量,Tarjan算法)
- HDU - 1269 - 迷宫城堡(强连通)
- hdu 1269 迷宫城堡 (图的联通,tarjan)
- HDU 1269 -- 迷宫城堡【有向图求SCC的数目 && 模板】
- HDU 1269 迷宫城堡(强连通分量):强连通图判定
- HDU - 1269 迷宫城堡
- hdu 1269 迷宫城堡 (并查集)
- 迷宫城堡 HDU - 1269(tarjan求强联通)
- hdu 1269 迷宫城堡(强连通分量 Tarjan算法)
- hdu 1269 tarjan算法 图的强连通之迷宫城堡
- HDU 1269 迷宫城堡(强连通模板题)
- HDU 1269 迷宫城堡(强联通图判定)