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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

vector<int> Spath, Tpath;

struct circle {
  int x, y, r;
  vector<int> e;
};

vector<circle> S, T;

bool asc(const circle &a, const circle &b) { return a.r < b.r; }

double dist(int x0, int y0, int x1, int y1) {
  return sqrt((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1));
}

bool include(circle a, circle b) { // a???b??????????????????
  if (dist(a.x, a.y, b.x, b.y) + b.r < a.r)
    return true;
  else
    return false;
}

bool intersect(circle a, circle b) {
  if (dist(a.x, a.y, b.x, b.y) <= a.r + b.r)
    return true;
  else
    return false;
}

void S_search(int x) {
  if (Spath[x] < 0) {
    if (S[x].e.size() > 0) {
      int m = -1;
      for (int i = 0; i < S[x].e.size(); i++) {
        if (Spath[S[x].e[i]] > 0) { // already search
          if (m < Spath[S[x].e[i]])
            m = Spath[S[x].e[i]];
        } else { // not yet search
          S_search(S[x].e[i]);
          if (m < Spath[S[x].e[i]])
            m = Spath[S[x].e[i]];
        }
      }
      Spath[x] = m + 1;
    } else
      Spath[x] = 1;
  }
}

void T_search(int x) {
  if (Tpath[x] < 0) {
    if (T[x].e.size() > 0) {
      int m = -1;
      for (int i = 0; i < T[x].e.size(); i++) {
        if (Tpath[T[x].e[i]] > 0) { // already search
          if (m < Tpath[T[x].e[i]])
            m = Tpath[T[x].e[i]];
        } else { // not yet search
          T_search(S[x].e[i]);
          if (m < Tpath[T[x].e[i]])
            m = Tpath[T[x].e[i]];
        }
      }
      Tpath[x] = m + 1;
    } else {
      Tpath[x] = 1;
    }
  }
}

int main() {
  while (true) {
    int n, xt, yt, xs, ys, x, y, r;
    cin >> n;
    if (n == 0)
      break;
    cin >> xs >> ys >> xt >> yt;
    for (int i = 0; i < n; i++) {
      cin >> x >> y >> r;
      double ds, dt;
      ds = dist(x, y, xs, ys);
      dt = dist(x, y, xt, yt);
      if ((ds > r) && (dt < r)) { //??´???????????????
        circle tmp;
        tmp.x = x;
        tmp.y = y;
        tmp.r = r;
        T.push_back(tmp);
      } else if ((ds < r) && (dt > r)) { //?????????????????????
        circle tmp;
        tmp.x = x;
        tmp.y = y;
        tmp.r = r;
        S.push_back(tmp);
      }
    }
    if (S.size() > 0)
      sort(S.begin(), S.end(), asc);
    if (T.size() > 0)
      sort(T.begin(), T.end(), asc);
    for (int i = 0; i < S.size(); i++) {
      for (int j = 0; j < S.size(); j++) {
        if (include(S[i], S[j]))
          S[i].e.push_back(j);
      }
      Spath.push_back(-1);
    }
    for (int i = 0; i < T.size(); i++) {
      for (int j = 0; j < T.size(); j++) {
        if (include(T[i], T[j]))
          T[i].e.push_back(j);
      }
      Tpath.push_back(-1);
    }
    for (int i = 0; i < S.size(); i++) {
      S_search(i);
    }
    for (int i = 0; i < T.size(); i++) {
      T_search(i);
    }
    int maxS = 0, maxT = 0, maxST = 0;
    if (S.size() > 0)
      maxS = *max_element(Spath.begin(), Spath.end());
    if (T.size() > 0)
      maxT = *max_element(Tpath.begin(), Tpath.end());
    for (int i = 0; i < S.size(); i++) {
      for (int j = 0; j < T.size(); j++) {
        if (!intersect(S[i], T[j])) {
          if (maxST < Spath[i] + Tpath[j])
            maxST = Spath[i] + Tpath[j];
        }
      }
    }
    cout << max({maxS, maxT, maxST}) << endl;
    S.clear();
    T.clear();
    Spath.clear();
    Tpath.clear();
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

vector<int> Spath, Tpath;

struct circle {
  int x, y, r;
  vector<int> e;
};

vector<circle> S, T;

double dist(int x0, int y0, int x1, int y1) {
  return sqrt((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1));
}

bool include(circle a, circle b) { // a???b??????????????????
  if (dist(a.x, a.y, b.x, b.y) + b.r < a.r)
    return true;
  else
    return false;
}

bool intersect(circle a, circle b) {
  if (dist(a.x, a.y, b.x, b.y) <= a.r + b.r)
    return true;
  else
    return false;
}

void S_search(int x) {
  if (Spath[x] < 0) {
    if (S[x].e.size() > 0) {
      int m = -1;
      for (int i = 0; i < S[x].e.size(); i++) {
        if (Spath[S[x].e[i]] > 0) { // already search
          if (m < Spath[S[x].e[i]])
            m = Spath[S[x].e[i]];
        } else { // not yet search
          S_search(S[x].e[i]);
          if (m < Spath[S[x].e[i]])
            m = Spath[S[x].e[i]];
        }
      }
      Spath[x] = m + 1;
    } else
      Spath[x] = 1;
  }
}

void T_search(int x) {
  if (Tpath[x] < 0) {
    if (T[x].e.size() > 0) {
      int m = -1;
      for (int i = 0; i < T[x].e.size(); i++) {
        if (Tpath[T[x].e[i]] > 0) { // already search
          if (m < Tpath[T[x].e[i]])
            m = Tpath[T[x].e[i]];
        } else { // not yet search
          T_search(S[x].e[i]);
          if (m < Tpath[T[x].e[i]])
            m = Tpath[T[x].e[i]];
        }
      }
      Tpath[x] = m + 1;
      cout << "Tpath[" << x << "] " << m + 1 << endl;
    } else {
      Tpath[x] = 1;
      cout << "Tpath[" << x << "] " << 1 << endl;
    }
  }
}

int main() {
  while (true) {
    int n, xt, yt, xs, ys, x, y, r;
    cin >> n;
    if (n == 0)
      break;
    cin >> xs >> ys >> xt >> yt;
    for (int i = 0; i < n; i++) {
      cin >> x >> y >> r;
      double ds, dt;
      ds = dist(x, y, xs, ys);
      dt = dist(x, y, xt, yt);
      if ((ds > r) && (dt < r)) { //??´???????????????
        circle tmp;
        tmp.x = x;
        tmp.y = y;
        tmp.r = r;
        T.push_back(tmp);
      } else if ((ds < r) && (dt > r)) { //?????????????????????
        circle tmp;
        tmp.x = x;
        tmp.y = y;
        tmp.r = r;
        S.push_back(tmp);
      }
    }
    for (int i = 0; i < S.size(); i++) {
      for (int j = 0; j < S.size(); j++) {
        if (include(S[i], S[j]))
          S[i].e.push_back(j);
      }
      Spath.push_back(-1);
    }
    for (int i = 0; i < T.size(); i++) {
      for (int j = 0; j < T.size(); j++) {
        if (include(T[i], T[j]))
          T[i].e.push_back(j);
      }
      Tpath.push_back(-1);
    }
    /*
    for(int i = 0; i < S.size(); i++) {
      cout << "S" << i << ": ";
      for(int j = 0; j < S[i].e.size(); j++) cout << S[i].e[j] << " ";
      cout << endl;
    }
    for(int i = 0; i < T.size(); i++) {
      cout << "T" << i << ": ";
      for(int j = 0; j < T[i].e.size(); j++) cout << T[i].e[j] << " ";
      cout << endl;
    }
    */
    for (int i = 0; i < S.size(); i++) {
      S_search(i);
    }
    for (int i = 0; i < T.size(); i++) {
      T_search(i);
    }
    int maxS = 0, maxT = 0, maxST = 0;
    if (S.size() > 0)
      maxS = *max_element(Spath.begin(), Spath.end());
    if (T.size() > 0)
      maxT = *max_element(Tpath.begin(), Tpath.end());
    cout << "od2" << endl;
    for (int i = 0; i < S.size(); i++) {
      for (int j = 0; j < T.size(); j++) {
        if (!intersect(S[i], T[j])) {
          if (maxST < Spath[i] + Tpath[j])
            maxST = Spath[i] + Tpath[j];
        }
      }
    }
    cout << max({maxS, maxT, maxST}) << endl;
    for (int i = 0; i < S.size(); i++)
      cout << Spath[i] << endl;
    for (int i = 0; i < T.size(); i++)
      cout << Tpath[i] << endl;
    S.clear();
    T.clear();
    Spath.clear();
    Tpath.clear();
  }
  return 0;
}