Description
Input
Output
Sample Input
4 3 1 2 2 3 4 3
Sample Output
1 2 4 3
题目大意:中文。
#include<stdio.h>
#include<string.h>
int gra[505][505], degree[505];
int main() {
int n, m;
while (scanf("%d %d", &n, &m) == 2) {
memset(gra, 0, sizeof(gra));
memset(degree, 0, sizeof(degree));
for (int i = 0; i < m; i++) {
int a, b;
scanf("%d %d", &a, &b);
if (!gra[a][b]) { //判断重边
gra[a][b] = 1;
degree[b]++;
}
}
for (int i = 1; i <= n; i++) { //拓扑
for (int j = 1; j <= n; j++) {
if (degree[j] == 0) {//寻找入度为零的点作为起点
degree[j]--;
if (i != n) printf("%d ", j);
else printf("%d\n", j);
for (int k = 1; k <= n; k++) {//删除与该点相关联的边
if (gra[j][k]) {
degree[k]--;
}
}
break;
}
}
}
}
return 0;
}
原文地址:http://blog.csdn.net/llx523113241/article/details/43281855