经典的拓扑排序,
每次找入度为0的点, 输出, 并且把与这个点相连的点的入度-1
依次循环就行了
因为我这里是每输出完一次就对原数组排序,
所以注意要把输出完毕的点入度设为一个较高的值
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <list>
#include <cassert>
#include <iomanip>
using namespace std;
const int MAXN = 100+10;
const int BASE = 10;
typedef long long LL;
/*
uva 10305
*/
struct task{
int num;
int indegree;
} tasks[MAXN];
bool operator<(const task & t1, const task & t2){
return t1.indegree < t2.indegree;
}
int Edge[MAXN][MAXN];
int m,n;
int main(){
while( (cin >> n >> m) && m+n>0 ){
int a, b, first = 0;
memset(tasks,0,sizeof(tasks));
memset(Edge,0,sizeof(Edge));
for(int i=0; i<n; i++) tasks[i].num = i+1;
for(int i=0; i<m; i++){
cin >> a >> b;
Edge[a][b] = 1;
tasks[b-1].indegree++;
}
for(int i=0; i<n; i++){
sort(tasks, tasks+n);
cout << tasks[0].num << " ";
for(int j=1; j<n-i; j++){
if( Edge[tasks[0].num][tasks[j].num] ){
tasks[j].indegree--;
}
}
tasks[0].indegree = MAXN+1;
}
cout << endl;
}
return 0;
}