-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescalonamento.cpp
68 lines (54 loc) · 1.25 KB
/
escalonamento.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
68
#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <string>
#include <string.h> // memset
#include <algorithm> // lower_bound
using namespace std;
#define fs first
#define sd second
#define pb push_back
#define MAXN 50010
int grau[MAXN];
vector<int> grafo[MAXN];
// it's necessary because after remove a conection between two vertices,
// it can come before an already taken vertice.
priority_queue<int, vector<int>, greater<int>> list;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m, a, b;
cin >> n >> m;
for(int i = 0; i < m; ++i) {
cin >> a >> b;
grafo[a].pb(b);
grau[b]++;
}
for(int i = 0; i < n; ++i) {
if(!grau[i]) {
list.push(i);
}
}
int ini = 0;
vector<int> ans;
while(!list.empty()) {
int atual = list.top();
list.pop();
for(int i = 0; i < (int)grafo[atual].size(); ++i) {
int u = grafo[atual][i];
grau[u]--;
if(!grau[u])
list.push(u);
}
ans.pb(atual);
}
if((int)ans.size() == n) {
for(auto i: ans)
cout << i << endl;
} else {
cout << "*" << endl;
}
return 0;
}