-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTOPOSORT.cpp
73 lines (60 loc) · 1.26 KB
/
TOPOSORT.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
// https://www.spoj.com/problems/TOPOSORT/
#include <bits/stdc++.h>
#define MAX 50001
using namespace std;
#define fast_cin() \
ios_base::sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL)
vector<vector<int>> graph;
vector<int> order;
set<int> q;
vector<int> indegree;
int toposort()
{
int u, v, cnt = 0;
for (int i = 1; i < indegree.size(); i++)
{
if (indegree[i] == 0)
q.insert(i);
}
while (!q.size() > 0)
{
cnt++;
u = *q.begin();
q.erase(q.begin());
order.push_back(u);
for (int adj : graph[u])
{
indegree[adj]--;
if (indegree[adj] == 0)
q.insert(adj);
}
}
return cnt;
}
int main()
{
fast_cin();
int m, n;
cin >> n >> m;
indegree.resize(n + 1, 0);
graph.resize(n + 1, vector<int>(0));
while (m--)
{
int u, v;
cin >> u >> v;
graph[u].push_back(v);
indegree[v]++;
}
int numNodes = toposort();
if (numNodes != n)
cout << "Sandro fails." << endl;
else
{
cout << order[0];
for (int i = 1; i < order.size(); i++)
cout << " " << order[i];
}
return 0;
}