#include<bits/stdc++.h>usingnamespace std;constint N =1e5+10;int n, m;int h[N], e[N], ne[N], idx;int in[N];int q[N];voidadd(inta,intb){
ne[idx]= h[a];
e[idx]= b;
h[a]= idx++;}inttopsort(){int hh =0, tt =-1;for(int i =1; i <= n; i ++){if(in[i]==0)
q[++ tt]= i;}while(hh <= tt){int t = q[hh ++];for(int i = h[t]; i !=-1; i = ne[i]){int j = e[i];if(-- in[j]==0)
q[++ tt]= j;}}return tt != n -1;}intmain(){
cin >> n >> m;memset(h,-1,sizeof h);for(int i =1; i <= m; i ++){int a, b;
cin >> a >> b;add(a, b);
in[b]++;}if(topsort())puts("-1");elsefor(int i =0; i < n; i ++)
cout << q[i]<<"";return0;}
共 3 条回复
???
神马情况?