Topological orderings
There are n tasks, which have to be done one by one. Some tasks must be done before others: there are m precedence relations between tasks. Write a program that prints all possible ways to order the n tasks according to the m given precedences.
Input: Input consists of a natural number n \geq 1, followed by a natural number m, followed by m different pairs x, y, indicating that task x must be done before task y. Suppose that the tasks are numbered from 0 to n − 1.
Output: Print, one per line and in lexicographic order, all the ways of sorting the n tasks according to the m given precedences. There will always be at least one solution.
Revise how we solved Topological Sort in the lab L3.
// Topological orderings
#include<iostream>
#include<vector>
#include<set>
using namespace std;
using VVI = vector<vector<int>>;
using VI = vector<int>;
using VB = vector<bool>;
using SI = set<int>;
void write(const VI& sol){
int n = sol.size();
<< sol[0];
cout for (int i = 1; i < n; ++i)
<< ' ' << sol[i];
cout << endl;
cout }
void top_sorts(int k, const VVI& g, VI& sol, VB& mkd, VI& indegree, SI& deg_zero_vertices){
int n = sol.size();
if (k == n)
(sol);
writeelse{
= deg_zero_vertices;
SI tmp for (int x : tmp){
if (not mkd[x]){
[x] = true;
mkd[k] = x;
sol.erase(x);
deg_zero_verticesfor (int y : g[x]){
--indegree[y];
if (indegree[y] == 0) deg_zero_vertices.insert(y);
}
(k+1, g, sol, mkd, indegree, deg_zero_vertices);
top_sorts[x] = false;
mkdfor (int y : g[x]){
if (indegree[y] == 0) deg_zero_vertices.erase(y);
++indegree[y];
}
.insert(x);
deg_zero_vertices}
}
}
}
int main(){
int n, m;
>> n >> m;
cin (n);
VVI g(n, 0);
VI indegreefor (int i = 0; i < m; ++i){
int u, v;
>> u >> v;
cin [u].push_back(v);
g++indegree[v];
}
;
SI deg_zero_verticesfor (int v = 0; v < n; ++v)
if (indegree[v] == 0)
.insert(v);
deg_zero_vertices(n);
VI sol(n, false);
VB mkd(0, g, sol, mkd, indegree, deg_zero_vertices);
top_sorts}