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

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int N, M;
bool X[5000][5000];
int W[5000], L[5000];
pair<int, int> P[5000];
bool F = true;
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    int A, B;
    scanf("%d%d", &A, &B);
    X[A - 1][B - 1] = 1;
    W[A - 1]++;
    L[B - 1]++;
  }
  for (int LC = 0; LC < N; LC++) {
    int cou = 0;
    int LCi;
    for (int i = 0; i < N; i++) {
      if (L[i] == LC) {
        cou++;
        LCi = i;
      }
    }
    if (cou >= 2) {
      F = false;
      break;
    } else if (cou == 1) {
      for (int i = 0; i < N; i++) {
        if (i == LCi)
          continue;
        if (!X[LCi][i] && !X[i][LCi]) {
          X[LCi][i] = 1;
          W[LCi]++;
          L[i]++;
        }
      }
    }
  }
  for (int WC = 0; WC < N; WC++) {
    int cou = 0;
    int WCi;
    for (int i = 0; i < N; i++) {
      if (W[i] == WC) {
        cou++;
        WCi = i;
      }
    }
    if (cou >= 2) {
      F = false;
      break;
    } else if (cou == 1) {
      for (int i = 0; i < N; i++) {
        if (i == WCi)
          continue;
        if (!X[WCi][i] && !X[i][WCi]) {
          X[WCi][i] = 1;
          W[WCi]++;
          L[i]++;
        }
      }
    }
  }

  if (!F) {
    for (int LC = 0; LC < N; LC++) {
      int LCi = -1;
      for (int i = 0; i < N; i++) {
        if (L[i] == LC) {
          LCi = i;
          for (int j = 0; j < N; j++) {
            if (j == i)
              continue;
            if (!X[LCi][j] && !X[j][LCi]) {
              X[LCi][j] = 1;
              W[LCi]++;
              L[j]++;
            }
          }
        }
      }
    }
  }
  for (int i = 0; i < N; i++) {
    P[i] = make_pair(L[i], i);
  }
  sort(P, P + N);
  for (int i = 0; i < N; i++)
    printf("%d\\n", P[i].second + 1);
  if (F)
    printf("0\\n");
  else
    printf("1\\n");
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int N, M;
int X[5000][5000];
int W[5000], L[5000];
pair<int, int> P[5000];
bool F = true;
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0; i < M; i++) {
    int A, B;
    scanf("%d%d", &A, &B);
    X[A - 1][B - 1] = 1;
    W[A - 1]++;
    L[B - 1]++;
  }
  for (int LC = 0; LC < N; LC++) {
    int cou = 0;
    int LCi;
    for (int i = 0; i < N; i++) {
      if (L[i] == LC) {
        cou++;
        LCi = i;
      }
    }
    if (cou >= 2) {
      F = false;
      break;
    } else if (cou == 1) {
      for (int i = 0; i < N; i++) {
        if (i == LCi)
          continue;
        if (!X[LCi][i] && !X[i][LCi]) {
          X[LCi][i] = 1;
          W[LCi]++;
          L[i]++;
        }
      }
    }
  }
  for (int WC = 0; WC < N; WC++) {
    int cou = 0;
    int WCi;
    for (int i = 0; i < N; i++) {
      if (W[i] == WC) {
        cou++;
        WCi = i;
      }
    }
    if (cou >= 2) {
      F = false;
      break;
    } else if (cou == 1) {
      for (int i = 0; i < N; i++) {
        if (i == WCi)
          continue;
        if (!X[WCi][i] && !X[i][WCi]) {
          X[WCi][i] = 1;
          W[WCi]++;
          L[i]++;
        }
      }
    }
  }

  if (!F) {
    for (int LC = 0; LC < N; LC++) {
      int LCi = -1;
      for (int i = 0; i < N; i++) {
        if (L[i] == LC) {
          LCi = i;
          for (int j = 0; j < N; j++) {
            if (j == i)
              continue;
            if (!X[LCi][j] && !X[j][LCi]) {
              X[LCi][j] = 1;
              W[LCi]++;
              L[j]++;
            }
          }
        }
      }
    }
  }
  for (int i = 0; i < N; i++) {
    P[i] = make_pair(L[i], i);
  }
  sort(P, P + N);
  for (int i = 0; i < N; i++)
    printf("%d\\n", P[i].second + 1);
  if (F)
    printf("0\\n");
  else
    printf("1\\n");
  return 0;
}