How to solve this problem which is based on topological order

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;               
}

Here is my AC code.

const int MAX_N = 1234;

vector<int> adj[MAX_N];
vector<bool> visited(MAX_N,false);
vector<int> order;
vector<int> in(MAX_N,false);

//int arr[MAX_N][MAX_N];

void topsort(int u){
    if(visited[u]){
        return;
    }
    visited[u]=true;
    for(int i=0;i<adj[u].size();i++){
        int v=adj[u][i];
        topsort(v);
    }
    order.push_back(u);
}

int main(){
    int N,K,x,y,ans;
    map<ii,int> M;
    scanf("%d%d",&N,&K);
    for(int i=0;i<K;i++){
        scanf("%d%d",&x,&y);
        adj[y].push_back(x);
        in[x]=true;
        M[ii(x,y)]=1;
    }
    for(int i=0;i<N;i++){
        if(in[i]==false){
            topsort(i);
            break;
        }
    }
    if(order.size()!=N){
        printf("back to the lab\n");
        return 0;
    }
    for(int i=0;i<order.size()-1;i++){
        if(M[ii(order[i],order[i+1])]!=1){
            printf("back to the lab\n");
            return 0;
        }
    }
    for(int i=0;i<order.size();i++){
        printf("%d ",order[i]);
    }
    printf("\n");
    return 0;
}