拓扑排序。。还能比这再水一点么?
#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;
}