The problem is to print the lexicographically smallest topological sorting of the vertices.
But I don’t really know what’s wrong with my code:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
int main() {
int n, m; cin >> n >> m;
vector<int> graph[n+1];
int indegree[n+1];
memset(indegree, 0, sizeof(indegree));
while(m--){
int a, b; cin >> a >> b;
graph[a].pb(b);
indegree[b]++;
}
priority_queue<int,vector<int>,greater<int>> q;
vector<int> order;
for (int i=1; i<=n; i++){
if (indegree[i] == 0) { q.push(i); }
}
while (!q.empty()) {
int curr = q.top();
q.pop();
order.push_back(curr);
for (int next : graph[curr]){
indegree[next]--;
if (indegree[next] == 0) { q.push(next); }
}
}
for (int i = 0; i < n; i++) {cout << order[i] << " ";}
}
This is just the modified version of Topological Sort where I used priority queue which pops the min element to get the lexicographically smallest permutation. But this gives WA for some test cases for example:
Could someone help me where the issue is?