-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathordenacao-topologica.cpp
More file actions
65 lines (51 loc) · 1.16 KB
/
ordenacao-topologica.cpp
File metadata and controls
65 lines (51 loc) · 1.16 KB
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 <iostream>
#include <vector>
#include <queue>
#include <string.h>
#define MAX 100000
#define UNVISITED -1
using namespace std;
int component[MAX],
grau[MAX];
vector<int> grafo[MAX],
orderedTop;
/**
*
* Graph is unweighted,
* or all distances are one between two vertices adjacent.
*
*/
int main() {
int n, m, v, a, b;
// the vertice is marked as 1 to n
cin >> n >> m;
// Building list adjacent
for(int i = 0; i < m; ++i) {
cin >> a >> b;
grafo[a].push_back(b);
grau[b] += 1;
}
for(int i = 1; i <= n; ++i) {
if(!grau[i]) {
orderedTop.push_back(i);
}
}
int index = 0;
while(index < (int)orderedTop.size()){
int atual = orderedTop[index];
index++;
for(int i = 0;i < (int)grafo[atual].size(); ++i){
int v = grafo[atual][i];
grau[v]--;
if(grau[v] == 0) orderedTop.push_back(v); // se o grau se tornar zero, acrescenta-se a lista
}
}
if((int)orderedTop.size() < n)
cout << "impossivel\n";
else {
for(int i = 0;i < (int)orderedTop.size(); ++i)
cout << orderedTop[i] << " ";
cout << endl;
}
return 0;
}