-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask_II_6.cpp
More file actions
53 lines (45 loc) · 781 Bytes
/
Task_II_6.cpp
File metadata and controls
53 lines (45 loc) · 781 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
#include <iostream>
#include <list>
#include <vector>
using namespace std;
vector< list<int> > graph = {
{ 1, 2, 4 },
{ },
{ 0, 5 },
{ 1 },
{ },
{ 0 },
{ 4, 5 } };
vector<bool> used(graph.size());
vector <int> result;
void dfs(int v) {
used[v] = true;
for (auto l : graph[v]) {
int to = l;
if (!used[to]) {
dfs(to);
}
}
result.push_back(v);
}
void topologicalSort() {
for (size_t i = 0; i < graph.size(); ++i) {
used[i] = false;
}
result.clear();
for (size_t i = 0; i < graph.size(); ++i) {
if (!used[i]) {
dfs(i);
}
}
reverse(begin(result), end(result));
}
int main() {
topologicalSort();
for (auto v : result) {
cout << v << ' ';
}
cout << endl;
system("pause");
return 0;
}