Topological sort
We must perform n tasks, one at a time. Furthermore, some tasks must be done before others: there are m precedence relations between tasks. Write a program to print a way to sort the n tasks satisfying the m given precedences.
Input: Input consists of several cases. Every case begins with n
, followed by m
, followed by m distinct pairs x y
that indicate that task x must be done before task y. You can assume 1 \leq n \leq 10^4, 0 \leq m \leq 10n, and that the tasks are numbered from 0 to n − 1.
Output: For every case, print the lexicographically smallest order of the n tasks that fulfills the m given precedences. There will always be, at least, one solution.
This problem is related to Problem 5.12 you will see in Problem class P6.
// Topological sort
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
using graph = vector<vector<int>>;
using VI = vector<int>;
(const graph& g, VI& indegree){
VI compute_orderingint n = g.size();
<int, vector<int>, greater<int>> pq;
priority_queuefor (int u = 0; u < n; ++u){
if (indegree[u]==0) pq.push(u);
}
;
VI ordwhile(not pq.empty()){
int u = pq.top();
.pop();
pq.push_back(u);
ordfor (size_t k = 0; k < g[u].size(); ++k){
int v = g[u][k];
--indegree[v];
if (indegree[v]==0) pq.push(v);
}
}
return ord;
}
void write(const VI& v){
<< v[0];
cout for (size_t i = 1; i < v.size(); ++i)
<< ' ' << v[i];
cout << endl;
cout }
int main(){
int n, m;
while(cin >> n >> m){
(n);
graph g(n, 0);
VI indegreefor (int i = 0; i < m; ++i){
int u, v;
>> u >> v;
cin [u].push_back(v);
g++indegree[v];
}
= compute_ordering(g,indegree);
VI ord (ord);
write}
}