使用了小根堆 priority_queue<int,vector<int>,greater<int> >
#include <stdio.h>
#include <vector>
#include <queue>
using namespace std;
vector<int> edge[510];
priority_queue<int,vector<int>,greater<int> > Q;
int main(){
int inDegree[510];
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=0;i<510;i++){
inDegree[i]=0;
edge[i].clear();
}
for(int i=0;i<m;i++){
int a,b;
scanf("%d%d",&a,&b);
inDegree[b]++;
edge[a].push_back(b);
}
while(Q.empty()==false) Q.pop();
for(int i=1;i<=n;i++){
if(inDegree[i]==0) Q.push(i);
}
int cnt=0;
while(Q.empty()==false){
int newP=Q.top();
Q.pop();
if(cnt==0) printf("%d",newP);
else printf(" %d",newP);
cnt++;
for(int i=0;i<edge[newP].size();i++){
inDegree[edge[newP][i]]--;
if(inDegree[edge[newP][i]]==0){
Q.push(edge[newP][i]);
}
}
}
printf("\n");
}
return 0;
}