最后一个不能有空格就离谱
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
int e[N],ne[N],h[N],n,m,idx,top[N],d[N],cnt;
void add(int a,int b){
e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
queue<int> q;
bool topsort(){
for(int i=1;i<=n;i++)
if(!d[i])
q.push(i);
while(q.size()){
int t=q.front();
q.pop();
top[cnt++]=t;
for(int i=h[t];i!=-1;i=ne[i]){
int j=e[i];
if(--d[j]==0)
q.push(j);
}
}
if(cnt<n)return 0;
return 1;
}
int main(){
cin>>n>>m;
memset(h,-1,sizeof h);
while(m--){
int a,b;
cin>>a>>b;
add(a,b);
d[b]++;
}
if(topsort()){
for(int i=0;i<cnt;i++){
cout<<top[i];
if(i!=cnt-1)cout<<" ";
}
}else{
cout<<-1;
}
}
京公网安备 11010502036488号