You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
953 B
45 lines
953 B
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
const int INF = 0x3f3f3f3f;
|
|
const int N = 1e5 + 10, M = N << 1;
|
|
int n, m;
|
|
int ind[N];
|
|
//邻接表
|
|
int e[M], h[N], idx, ne[M];
|
|
void add(int a, int b) {
|
|
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
|
|
}
|
|
int q[N];
|
|
bool topsort() {
|
|
int hh = 0, tt = -1;
|
|
for (int i = 1; i <= n; i++)
|
|
if (!ind[i]) q[++tt] = i;
|
|
|
|
while (hh <= tt) {
|
|
int u = q[hh++];
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
int j = e[i];
|
|
if (--ind[j] == 0)
|
|
q[++tt] = j;
|
|
}
|
|
}
|
|
return hh == n;
|
|
}
|
|
int main() {
|
|
memset(h, -1, sizeof h);
|
|
cin >> n >> m;
|
|
for (int i = 1; i <= m; i++) {
|
|
int a, b;
|
|
cin >> a >> b;
|
|
add(a, b);
|
|
ind[b]++;
|
|
}
|
|
if (!topsort())
|
|
puts("-1");
|
|
else {
|
|
for (int i = 0; i < n; i++) printf("%d ", q[i]);
|
|
puts("");
|
|
}
|
|
return 0;
|
|
} |