-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuva-10305.cpp
66 lines (48 loc) · 934 Bytes
/
uva-10305.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
//uva 10305
//Ordering Tasks
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int main(void)
{
int m = 0, n = 0;
while (1) {
cin >> n >> m;
if (n == 0 && m == 0)
break;
vector < vector <int> > Graph(n);
vector <int> count(n, 0);
for (int i = 0; i < m; ++i){
int t1, t2;
cin >> t1 >> t2;
--t1;
--t2;
Graph[t1].push_back(t2);
++count[t2];
}
queue <int> Queue;
for (int i = 0; i < n; ++i)
if (!count[i])
Queue.push(i);
vector <int> result;
vector <bool> visited(n, false);
while (!Queue.empty()) {
int front = Queue.front();
Queue.pop();
visited[front] = 1;
result.push_back(front);
for (auto a : Graph[front])
if (!visited[a]){
--count[a];
if (count[a] == 0)
Queue.push(a);
}
}
cout << result[0] + 1;
for (int i = 1; i < n; ++i)
cout << ' ' << result[i] + 1;
cout << endl;
}
return 0;
}