-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTopological sort.cpp
100 lines (83 loc) · 1.85 KB
/
Topological sort.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <string.h>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <sstream>
using namespace std;
#define FOR( i, L, U ) for(int i=(int)L ; i<=(int)U ; i++ )
#define FORD( i, U, L ) for(int i=(int)U ; i>=(int)L ; i-- )
#define SQR(x) ((x)*(x))
#define INF INT_MAX
#define READ(filename) freopen(filename, "r", stdin);
#define WRITE(filename) freopen(filename, "w", stdout);
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<vector<int> > vvi;
typedef vector<vector<int> > vvc;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef map<int, string> mis;
typedef map<string, string> mss;
typedef map<string, char> msc;
#define WHITE 0
#define GRAY 1
#define BLACK 2
int nodes, edges;
vvi g;
vi color;
vi topoOrder;
void DFS_Visit(int u)
{
color[u] = GRAY;
FOR(i, 0, g[u].size()-1) {
int v = g[u][i];
if(color[v] == WHITE)
DFS_Visit(v);
}
topoOrder.push_back(u);
color[u] = BLACK;
}
void TopoSort()
{
color = vi(nodes+1, WHITE);
topoOrder.clear();
FOR(i, 1, nodes) {
if(color[i] == WHITE)
DFS_Visit(i);
}
reverse(topoOrder.begin(), topoOrder.end());
}
int main()
{
READ("input.txt");
WRITE("output.txt");
int u, v;
while(cin >> nodes >> edges) {
g = vvi(nodes+1);
FOR(i, 1, edges) {
cin >> u >> v;
g[u].push_back(v);
}
TopoSort();
FOR(i, 0, topoOrder.size()-1)
cout << topoOrder[i] << " " ;
cout << "\n";
}
return 0;
}