-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoposort.cpp
63 lines (60 loc) · 1.21 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
#include <bits/stdc++.h>
#include <algorithm>
#define ONE_BASE // use this to define if input uses nodes start from 1, default 0-based nodes
/*
* Topological Sort: returns lex-smallest ordering
* Input: DAG
*/
using namespace std;
size_t n, m;
vector<int> inDegree; // node index and its inDegree;
priority_queue<int, vector<int>, greater<int>> pq;
void toposort(vector<vector<int> > &adj, int s)
{
for(int i=0; i<adj[s].size(); i++)
{
{
inDegree[adj[s][i]]--;
if(!inDegree[adj[s][i]])
pq.push(adj[s][i]);
}
}
#ifdef ONE_BASE
cout <<s+1<<" ";
#else
cout <<s<<" ";
#endif
}
int main()
{
std::cin >> n >> m;
inDegree.reserve(n);
inDegree.assign(n, 0);
vector<vector<int> > adj(n, vector<int>());
for (size_t i = 0; i < m; i++) {
int x, y;
std::cin >> x >> y;
#ifdef ONE_BASE
adj[x-1].push_back(y-1);
inDegree[y-1]++;
#else
adj[x].push_back(y);
inDegree[y]++;
#endif
}
// Put nodes with 0 in-degree in priority_queue i.e. nodes with higher value is on top.
for (int i=0; i< n; i++)
if(inDegree[i]==0)
pq.push(i);
if(pq.size()==0)
{
cout <<"Sandro fails."<<"\n";
return 0;
}
while(!pq.empty())
{
int t = pq.top();
pq.pop();
toposort(adj, t);
}
}