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

#include <iostream>
#include <vector>
using namespace std;
vector<pair<int, int> *> vertical[5001];
bool find(int, int);
int main() {
  pair<int, int> point[3000];
  int n;
  bool dummy1, dummy2;
  pair<int, int> INF;
  while (cin >> n && n != 0) {
    for (int i = 0; i < n; ++i) {
      cin >> point[i].first >> point[i].second;
      vertical[point[i].first].push_back(&point[i]);
    }
    for (int i = 0; i <= 5000; ++i) {
      vertical[i].push_back(&INF);
    }
    int x, y;
    int answer = 0;
    for (int i = 0; i + 1 < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        x = point[j].second - point[i].second;
        y = point[j].first - point[i].first;
        if (x * x + y * y <= answer)
          continue;
        INF.second = point[i].second - y;
        dummy1 = find(point[i].first + x, point[i].second - y);
        INF.second = point[j].second - y;
        dummy2 = find(point[j].first + x, point[j].second - y);
        if (dummy1 == true && dummy2 == true) {
          answer = x * x + y * y;
          continue;
        }
        INF.second = point[i].second + y;
        dummy1 = find(point[i].first - x, point[i].second + y);
        INF.second = point[j].second + y;
        dummy2 = find(point[j].first - x, point[j].second + y);
        if (dummy1 == true && dummy2 == true) {
          answer = x * x + y * y;
        }
      }
    }
    for (int i = 0; i <= 5000; ++i) {
      vertical[i].clear();
    }
    cout << answer << endl;
  }
}

bool find(int x, int y) {
  if (x < 0 || x > 5000 || y < 0 || y > 5000)
    return false;
  int i = 0, psize = vertical[x].size();
  while (true) {
    if (vertical[x][i]->second == y) {
      if (i + 1 == psize) {
        return false;
      } else {
        return true;
      }
    }
    ++i;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<pair<int, int> *> vertical[5001];
bool find(int, int);
int main() {
  pair<int, int> point[3000];
  int n;
  bool dummy1, dummy2;
  while (cin >> n && n != 0) {
    for (int i = 0; i < n; ++i) {
      cin >> point[i].first >> point[i].second;
      vertical[point[i].first].push_back(&point[i]);
    }
    int x, y;
    int answer = 0;
    for (int i = 0; i + 1 < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        x = point[j].second - point[i].second;
        y = point[j].first - point[i].first;
        if (x * x, y * y <= answer)
          continue;
        dummy1 = find(point[i].first + x, point[i].second - y);
        dummy2 = find(point[j].first + x, point[j].second - y);
        if (dummy1 == true && dummy2 == true) {
          answer = x * x + y * y;
          continue;
        }
        dummy1 = find(point[i].first - x, point[i].second + y);
        dummy2 = find(point[j].first - x, point[j].second + y);
        if (dummy1 == true && dummy2 == true) {
          answer = x * x + y * y;
        }
      }
    }
    for (int i = 1; i <= 5000; ++i) {
      vertical[i].clear();
    }
    cout << answer << endl;
  }
}

bool find(int x, int y) {
  if (x < 0 || x > 5000 || y < 0 || y > 5000)
    return false;
  vertical[x].push_back(new pair<int, int>(x, y));
  int i = 0, psize = vertical[x].size();
  while (true) {
    if (vertical[x][i]->second == y) {
      vertical[x].pop_back();
      if (i + 1 == psize) {
        return false;
      } else {
        return true;
      }
    }
    ++i;
  }
}