hdu_1269, 强连通分量的学习
http://acm.hdu.edu.cn/showproblem.php?pid=1269
#include<cstdio> #include<cstring> const int maxn = 10000 + 10; struct Edge{ int to, next; }e[maxn*10]; int head[maxn], index; int dfn[maxn], low[maxn]; int belong[maxn], vis[maxn]; int step, color; void add_edge(int u, int v){ e[index].to = v, e[index].next = head[u]; head[u] = index ++; } void init_edge(){ memset(head, -1, sizeof head); index= 0; } int n, m; int stack[maxn], top; void targan(int u){ dfn[u] = low[u] = ++step; stack[top++] = u; vis[u] = 1; for(int i = head[u]; i != -1; i = e[i].next){ int v = e[i].to; if(!dfn[v]){ targan(v); if(low[u] > low[v]) low[u] = low[v]; }else if(vis[v] && low[u] > dfn[v]) low[u] = dfn[v]; } if(dfn[u] == low[u]){ color ++; int s; do{ s = stack[top--]; vis[s] = 0; belong[s] = color; }while(s != u); } } void solve(){ memset(vis, 0, sizeof vis); memset(dfn, 0, sizeof dfn); step = color = top = 0; for(int i = 1; i <= n; i ++) if(!dfn[i]) targan(i); } int main(){ while(scanf("%d%d", &n, &m), n||m){ init_edge(); for(int i = 0; i < m; i ++){ int u, v; scanf("%d%d", &u, &v); add_edge(u, v); } solve(); if(color == 1) puts("Yes"); else puts("No"); } return 0; }
这个也当作模板吧
posted on 2012-08-12 21:25 louzhang_swk 阅读(230) 评论(0) 编辑 收藏 举报