解题报告:
这题小意思。。啦 啦啦
#include <stdio.h> #include <string.h> #define MAXN 600 int n, m, indegree[MAXN], G[MAXN][MAXN], q[MAXN]; int toposort(){ int i, j, k; i=0; while(i<n){ for(j=1; j<=n; j++){ if(indegree[j] == 0){ indegree[j]--; q[i++] = j; for(k=1; k<=n; k++) if(G[j][k]) indegree[k]--; break; } } } return 1; } int main(){ int i, a, b; while(scanf("%d%d", &n, &m) == 2){ memset(G, 0, sizeof(G)); memset(indegree, 0, sizeof(indegree)); for(i=1; i<=m; i++){ scanf("%d %d", &a, &b); if(G[a][b] == 0){ G[a][b] = 1; indegree[b]++; } } toposort(); for(i=0; i<n; i++){ if(i != n-1) printf("%d ", q[i]); else printf("%d\n", q[i]); } } return 0; }