#include<bits/stdc++.h>
using namespace std;
vector<int> g[1001];
priority_queue<int,vector<int>,greater<int> > q;
int rudu[1001];
vector<int> ans;
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++){
int x,y;
cin>>x>>y;
g[x].push_back(y);
rudu[y]++;
}
for(int i=1;i<=n;i++){
if(rudu[i]==0){
q.push(i);
}
}
while(!q.empty()){
int f=q.top();
q.pop();
ans.push_back(f);
for(int i=0;i<g[f].size();i++){
int to=g[f][i];
rudu[to]--;
if(rudu[to]==0){
q.push(to);
}
}
}
if(ans.size()!=n){
cout<<-1<<endl;
return 0;
}
for(int i=0;i<n;i++){
cout<<ans[i]<<" ";
}
return 0;
}
标签:int,拓扑,vector,rudu,排序,1001
From: https://www.cnblogs.com/huangxirui/p/17606201.html