-
Notifications
You must be signed in to change notification settings - Fork 1
/
toponical_sort.cpp
67 lines (56 loc) · 1.16 KB
/
toponical_sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <bits/stdc++.h>
#define REP(i, a, b) for(int i = a, _b = b; i < _b; ++i)
#define FOR(i, a, b) for(int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for(int i = a, _b = b; i >= _b; --i)
#define endl "\n"
#define newline cout << "\n"
#define puts(x) cout << x << "\n"
using namespace std;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef long long llong;
vector<vector<int>> adj;
queue<int> que;
int V, E;
vector<int> indeg;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef Home
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
while(cin >> V >> E and (V | E)) {
adj.assign(V+1, vector<int>(0));
indeg.assign(V+1, 0);
int u, v;
REP(i, 0, E) {
cin >> u >> v;
adj[u].push_back(v);
indeg[v]++;
}
FOR(v, 1, V) {
if(indeg[v] == 0) {
que.push(v);
}
}
vector<int> tp;
while(!que.empty()) {
int u = que.front(); que.pop();
tp.push_back(u);
for(int v: adj[u]) {
indeg[v]--;
if(indeg[v] == 0) {
que.push(v);
}
}
}
cout << tp[0];
REP(i, 1, tp.size()) {
cout << " " << tp[i];
}
newline;
}
return 0;
}