Problem link : https://open.kattis.com/problems/reactivity
Can anyone here shade some light on where i am going wrong with this problem. Below is the code :
#include< bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define LL long long
vector< int > order;
vector< int > adj[1001];
int main() {
int n, k;
cin >> n >> k;
int indeg[n + 1];
memset(indeg, 0, sizeof indeg);
for(int i = 0;i < k;i++) {
int a, b; cin >> a >> b;
a++, b++;
adj[a].push_back(b);
indeg[b]++;
}
set<int> nextNode;
for(int i = 1;i <= n;i++)
if(!indeg[i]) nextNode.insert(i);
int cnt = 0;
while(!nextNode.empty()) {
int u = *nextNode.begin();
nextNode.erase(nextNode.begin());
order.push_back(u);
cnt++;
for(int i = 0;i < adj[u].size();i++) {
int v = adj[u][i];
indeg[v]--;
if(!indeg[v]) nextNode.insert(v);
}
}
if(cnt != n) cout << "back to the lab" << endl;
else {
for(int i = 0;i < order.size();i++) cout << order[i] - 1 << " ";
}
return 0;
}