Problem p00442 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int solve();
void visit(int n);

int N, M;
vector<int> vec[5010];
bool O = true;
bool visited[5050] = {0};
vector<int> ANS;
int cou = 0;

int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    vec[a].push_back(b);
  }
  solve();
  return 0;
}

int solve() {
  for (int i = 1; i <= N; i++) {
    visit(i);
  }
  for (int i = ANS.size() - 1; i >= 0; i--) {
    printf("%d\\n", ANS[i]);
  }
  for (int i = ANS.size() - 1; i > 0; i--) {
    bool o = 0;
    for (int j = 0; j < vec[ANS[i]].size(); j++) {
      if (ANS[i - 1] == vec[ANS[i]][j])
        o = true;
    }
    if (o == true)
      cou++;
  }
  if (cou == ANS.size() - 1)
    printf("0\\n");
  else
    printf("1\\n");
  return 0;
}

void visit(int n) {
  if (visited[n])
    return;
  visited[n] = true;
  for (int i = 0; i < vec[n].size(); i++) {
    visit(vec[n][i]);
  }
  ANS.push_back(n);
  return;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int field[5010][5010] = {0}, lim[5010][2] = {0};
int main() {
  int N, M, W, L, mem = 1, les, minn, maxn, memo, mem1, mem2;
  scanf("%d%d", &N, &M);
  les = N;
  for (int i = 0; i < N; i++) {
    lim[i + 1][0] = 1;
    lim[i + 1][1] = N;
  }
  for (int i = 0; i < M; i++) {
    scanf("%d%d", &W, &L);
    lim[W][1]--;
    lim[L][0]++;
    field[W][L] = 1;
    field[L][W] = -1;
  }
  /*for(int i = 1; i <= N; i++)
  {
      for(int j = 1; j <= N; j++)
      {
          printf("%2d",field[i][j]);
      }printf("\\n");
  }*/
  /*while(mem)
  {
      mem = 0;
      for(int i = 1; i <= N; i++)
      {
          if(lim[i][0] == lim[i][1] && str[i] == 0)
          {
              mem = 1;
              ranking[lim[i][0]] = i;
              les--;
              str[i] = 1;
              for(int j = 1; j <= N; j++)
              {
                  if(field[i][j] == 1)lim[j][0] = max(lim[i][0] + 1,lim[j][0]);
                  if(field[i][j] == -1)lim[j][1] = min(lim[i][0] - 1,lim[j][1]);
              }
              for(int j = 0; j < N; j++)
              {
                  if(field[i][j] != 0);
                  else
                  {
                      if(lim[j][0] > lim[i][0])
                      {
                          field[i][j] = 1;
                          field[j][i] = -1;
                      }
                      if(lim[j][1] < lim[i][0])
                      {
                          field[i][j] = -1;
                          field[j][i] = 1;
                      }
                  }

              }
          }
      }
      for(int i = 1; i <= N; i++)
      {
          if(!str[i])
          {
              while(ranking[lim[i][0]])lim[i][0]++;
              while(ranking[lim[i][0]])lim[i][0]--;
          }
      }
  }*/
  /*for(int i = 1; i < N + 1; i++)
  {
      printf("%2d%2d\\n",lim[i][0],lim[i][1]);
  }
  for(int i = 1; i <= N; i++)
  {
      for(int j = 1; j <= N; j++)
      {
          printf("%2d",field[i][j]);
      }printf("\\n");
  }*/
  mem2 = 0;
  for (int i = 1; i <= N; i++) {
    mem1 = 0;

    minn = i;
    maxn = 100000;
    for (int j = 1; j <= N; j++) {
      if (lim[j][0] == minn) {
        mem1++;
        if (lim[j][1] < maxn) {
          memo = j;
          minn = lim[j][0];
          maxn = lim[j][1];
        }
      }
    }
    printf("%d\\n", memo);
    lim[memo][0] = N + 1;
    for (int j = 1; j <= N; j++) {
      if (field[memo][j] == 0 && memo != j) {
        lim[j][0]++;
        field[memo][j] = 1;
        field[j][memo] = -1;
      }
    }
    /*for(int i = 1; i < N + 1; i++)
    {
        printf("%2d%2d\\n",lim[i][0],lim[i][1]);
    }
    for(int i = 1; i <= N; i++)
    {
        for(int j = 1; j <= N; j++)
        {
            printf("%2d",field[i][j]);
        }printf("\\n");
    }*/
    mem2 = max(mem2, mem1);
  }
  if (mem2 == 1)
    printf("%d\\n", 0);
  else
    printf("%d\\n", 1);
  /*for(int i = 1; i < N + 1; i++)
  {
      printf("%2d%2d\\n",lim[i][0],lim[i][1]);
  }*/

  return 0;
}