-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVA-10305.cpp
More file actions
65 lines (54 loc) · 917 Bytes
/
UVA-10305.cpp
File metadata and controls
65 lines (54 loc) · 917 Bytes
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
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll mod = 998244353;
const int N = 1234;
vector<int> adj[N];
int deg[N];
queue<int> q;
vector<int> ans;
void init() {
for (int i = 0; i < N; ++i) {
adj[i].clear();
deg[i] = 0;
}
q = queue<int>();
}
void solve() {
ans.clear();
while (!q.empty()) {
int p = q.front(); q.pop();
ans.pb(p);
for (int u : adj[p]) {
deg[u]--;
if (deg[u] == 0) {
q.push(u);
}
}
}
}
int main() {
ios::sync_with_stdio(0);
int n, m;
while (cin >> n >> m && (n || m)) {
init();
for (int i = 0; i < m; ++i) {
int x, y; cin >> x >> y;
adj[x].pb(y);
deg[y]++;
}
for (int i = 1; i <= n; ++i) {
if (!deg[i]) {
q.push(i);
}
}
solve();
for (int i = 0; i < n; ++i) {
cout << ans[i] << " \n"[n - i == 1];
}
}
return 0;
}