-
Notifications
You must be signed in to change notification settings - Fork 0
/
Oredering_Tasks.cpp
61 lines (44 loc) · 1.12 KB
/
Oredering_Tasks.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
// by jvpcms
/* The program must receive a dataset describing an ordered graph.
The output must be the nodes sorted in such a way that if a node A points to node B,
then A comes before B in the sorted sequence.
Keywords: Graph Theory, Topological Sort. */
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
int in[N];
queue<int> q;
int main(){
cin.tie(NULL);
ios_base::sync_with_stdio(false);
int n, m;
while (cin >> n >> m)
{
if (!m and !n) break;
vector<int> tsort;
memset(in, 0, N);
vector<int> adj[N];
while (m--)
{
int a, b;
cin >> a >> b;
adj[a].push_back(b);
in[b]++;
}
for (int i = 1; i <= n; i++) if (!in[i]) q.push(i);
while (!q.empty())
{
int u = q.front();
q.pop();
tsort.push_back(u);
for (int v : adj[u])
{
in[v]--;
if (in[v] == 0) q.push(v);
}
}
for (auto i : tsort) cout << i << ' ';
cout << '\n';
}
return 0;
}