syhd142 |
|
|||
日歷
統(tǒng)計(jì)
導(dǎo)航常用鏈接留言簿(2)隨筆檔案(23)文章分類(270)
文章檔案(122)我的豆瓣搜索最新評(píng)論
閱讀排行榜
評(píng)論排行榜 |
拓?fù)渑判颉!_€能比這再水一點(diǎn)么? #include <stdio.h>
#include <string.h> #define N 105 bool g[N][N]; int in[N]; void Topsort(int n) { int front, top, queue[N * N]; front = top = 0; for(int i = 1; i <= n; i++) { if(!in[i]) queue[top++] = i; } while(front < top) { int u = queue[front++]; for(int i = 1; i <= n; i++) { if(g[u][i]) { g[u][i] = 0; in[i]--; if(!in[i]) queue[top++] = i; } } } for(int i = 0; i < top; i++) { printf("%d", queue[i]); if(i != top - 1) printf(" "); else printf("\n"); } } int main() { int n, m; while(scanf("%d %d", &n, &m), n + m) { memset(g, 0, sizeof(g)); memset(in, 0, sizeof(in)); for(int i = 0; i < m; i++) { int a, b; scanf("%d %d", &a, &b); g[a][b] = 1; in[b]++; } Topsort(n); } return 0; }
|
![]() |
|
Copyright © Fucker | Powered by: 博客園 模板提供:滬江博客 |