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

#include <bits/stdc++.h>

using namespace std;

// #define int long long
#define PB push_back
#define PPB pop_back
typedef pair<int, int> pii;
// static const int INF = 1LL<<61;
static const int INF = 1 << 30;

bool visit[5005];
vector<int> tree, G[5005];

void dfs(int a) {
  if (visit[a])
    return;
  visit[a] = true;
  for (int i = 0; i < G[a].size(); ++i) {
    dfs(G[a][i]);
  }
  tree.PB(a);
}

bool isclosed(int a) {
  if (visit[a])
    return true;
  visit[a] = true;

  for (int i = 0; i < G[a].size(); ++i) {
    if (isclosed(G[a][i]))
      return true;
  }

  return false;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].PB(b);
  }

  for (int i = 1; i <= n; ++i) {
    sort(G[i].begin(), G[i].end());
    dfs(i);
  }

  bool other = true;
  for (int i = 0; i < tree.size() - 1; ++i) {
    int a = tree[i];
    other = binary_search(G[tree[i + 1]].begin(), G[tree[i + 1]].end(), a);
    if (!other)
      break;
  }

  for (int i = tree.size() - 1; 0 <= i; --i) {
    cout << tree[i] << endl;
  }
  cout << !other << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

// #define int long long
#define PB push_back
#define PPB pop_back
typedef pair<int, int> pii;
// static const int INF = 1LL<<61;
static const int INF = 1 << 30;

bool visit[5005], used[5005];
vector<int> tree, G[5005];

void dfs(int a) {
  if (visit[a])
    return;
  visit[a] = true;
  for (int i = 0; i < G[a].size(); ++i) {
    dfs(G[a][i]);
  }
  tree.PB(a);
}

bool isclosed(int a) {
  if (visit[a])
    return true;
  visit[a] = true;

  for (int i = 0; i < G[a].size(); ++i) {
    if (isclosed(G[a][i]))
      return true;
  }

  return false;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].PB(b);
  }

  for (int i = 1; i <= n; ++i) {
    sort(G[i].begin(), G[i].end());
    dfs(i);
  }

  bool other = false;
  for (int i = 0; i < tree.size(); ++i) {
    int a = tree[i];
    used[a] = true;
    int pre = 0;
    for (int j = a + 1; j <= n; ++j) {
      bool flg = false;
      for (int k = pre; k < G[a].size(); ++k) {
        if (j < G[a][k]) {
          pre = k;
          break;
        }

        if (j == G[a][k]) {
          flg = true;
          break;
        }
      }
      if (flg || binary_search(G[j].begin(), G[j].end(), a))
        continue;

      pii p;
      {
        memset(visit, 0, sizeof(visit));
        bool flg = false;
        G[a].PB(j);
        if (!isclosed(a)) {
          flg = true;
          p = pii(a, j);
        }
        G[a].PPB();
        G[j].PB(a);
        memset(visit, 0, sizeof(visit));
        if (!flg && !isclosed(j)) {
          flg = true;
          p = pii(j, a);
        }
        G[j].PPB();
        other = other || flg;
      }
      G[p.first].PB(p.second);
    }
  }

  for (int i = tree.size() - 1; 0 <= i; --i) {
    cout << tree[i] << endl;
  }
  cout << other << endl;

  return 0;
}