题目描述
blablabla
样例
blablabla
C++ 代码
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
int idx,ne[N],h[N],e[N];
int n,m;
int path[N];
int st[N];
void add(int a,int b)
{
ne[idx] = h[a],e[idx] = b,h[a] = idx++;
}
bool topsort()
{
int tt = 0,hh = -1;
for(int i = 1; i <= n ;i ++ )
if(!st[i])
path[++hh] = i;
while(tt<=hh)
{
int t = path[tt++];
for(int i = h[t]; i!=-1; i =ne[i])
{
int j = e[i];
if(--st[j]==0) path[++hh] = j;
}
}
return hh == n-1;
}
int main()
{
cin >> n >> m;
memset(h,-1,sizeof h);
for(int i = 0; i < m; i ++ )
{
int a,b;
cin >> a >> b;
add(a,b);
st[b]++;
}
if(topsort())
for(int i = 0; i < n; i ++ )
cout << path[i] << " ";
else cout << -1 << endl;
return 0;
}