Problem p00442 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

short table[5010][5010];

vector<int> e[5010], e2[5010];

int done[5010];
vector<int> tpr;
void dfs(int x, int s) {
  if (done[x])
    return;
  else
    done[x] = true;

  if (s != x) {
    table[x][s] = 1;
    table[s][x] = 2;
  }
  for (int i = 0; i < e[x].size(); i++) {
    dfs(e[x][i], s);
  }
}

void dfs2(int x) {
  if (done[x])
    return;
  else
    done[x] = true;

  for (int i = 0; i < e2[x].size(); i++) {
    dfs2(e2[x][i]);
  }
  tpr.push_back(x);
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    e[b].push_back(a);
    e2[a].push_back(b);
    table[a][b] = 1;
    table[b][a] = 2;
  }
  for (int i = 0; i < n; i++) {
    dfs(i, i);
    memset(done, 0, sizeof(done));
  }
  for (int i = 0; i < n; i++)
    dfs2(i);

  int f = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i != j) {
        if (table[i][j] == 0) {
          f |= 1;
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int a = tpr[j];
      int b = tpr[i];
      table[a][b] = 1;
      table[b][a] = 2;
    }
  }
  vector<pair<int, int>> v;
  for (int i = 0; i < n; i++) {
    int cnt = 0;
    for (int j = 0; j < n; j++)
      if (table[i][j] == 1)
        cnt++;
    v.push_back(make_pair(cnt, i));
  }
  sort(v.rbegin(), v.rend());
  for (int i = 0; i < n; i++)
    cout << v[i].second + 1 << endl;
  cout << f << endl;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;

char table[5010][5010];

vector<int> e[5010], e2[5010];

int done[5010];
vector<int> tpr;
void dfs(int x, int s) {
  if (done[x])
    return;
  else
    done[x] = true;

  if (s != x) {
    table[x][s] = 1;
    table[s][x] = 2;
  }
  for (int i = 0; i < e[x].size(); i++) {
    dfs(e[x][i], s);
  }
}

void dfs2(int x) {
  if (done[x])
    return;
  else
    done[x] = true;

  for (int i = 0; i < e2[x].size(); i++) {
    dfs2(e2[x][i]);
  }
  tpr.push_back(x);
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    e[b].push_back(a);
    e2[a].push_back(b);
    table[a][b] = 1;
    table[b][a] = 2;
  }
  for (int i = 0; i < n; i++) {
    dfs(i, i);
    memset(done, 0, sizeof(done));
  }
  for (int i = 0; i < n; i++)
    dfs2(i);

  int f = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i != j) {
        if (table[i][j] == 0) {
          f |= 1;
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int a = tpr[j];
      int b = tpr[i];
      table[a][b] = 1;
      table[b][a] = 2;
    }
  }
  vector<pair<int, int>> v;
  for (int i = 0; i < n; i++) {
    int cnt = 0;
    for (int j = 0; j < n; j++)
      if (table[i][j] == 1)
        cnt++;
    v.push_back(make_pair(cnt, i));
  }
  sort(v.rbegin(), v.rend());
  for (int i = 0; i < n; i++)
    cout << v[i].second + 1 << endl;
  cout << f << endl;
}