Problem p01299 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

class Point {
public:
  int x, y;
  Point() {}
  Point(int X, int Y) : x(X), y(Y) {}
};

class Circle {
public:
  Point c;
  int r;
  Circle() {}
  Circle(const Point &C, int R) : c(C), r(R) {}
};

#define sq(x) (x) * (x)
int dist2(const Point &p, const Point &q) {
  return sq(p.x - q.x) + sq(p.y - q.y);
}

bool cover(const Circle &c, const Point &p) { return dist2(p, c.c) < sq(c.r); }

bool cover(const Circle &c1, const Circle &c2) {
  return c1.r >= c2.r && dist2(c1.c, c2.c) < sq(c1.r - c2.r);
}

bool peri_intersect(const Circle &c1, const Circle &c2) {
  int d2 = dist2(c1.c, c2.c);
  return sq(c1.r - c2.r) <= d2 && d2 <= sq(c1.r + c2.r);
}

int dp[1002];
vector<int> adj[1002];

int dfs(int u) {
  if (~dp[u])
    return dp[u];
  int res = 0;
  rep(i, adj[u].size()) {
    int v = adj[u][i];
    res = max(res, dfs(v) + 1);
  }
  return dp[u] = res;
}

int main() {
  for (int n; scanf("%d", &n), n;) {
    Point s, t;
    scanf("%d%d%d%d", &s.x, &s.y, &t.x, &t.y);
    Circle c[1000];
    vector<int> S, T;
    rep(i, n) {
      scanf("%d%d%d", &c[i].c.x, &c[i].c.y, &c[i].r);
      bool bs = cover(c[i], s);
      bool bt = cover(c[i], t);
      if (bs ^ bt) {
        if (bs)
          S.push_back(i);
        if (bt)
          T.push_back(i);
      }
    }

    rep(u, n + 2) adj[u].clear();
    rep(i, S.size()) {
      int u = S[i];
      rep(j, S.size()) {
        int v = S[j];
        if (u != v && cover(c[v], c[u]))
          adj[u].push_back(v);
      }
      adj[n].push_back(u);
      adj[u].push_back(n + 1);
    }
    rep(i, S.size()) rep(j, T.size()) {
      int u = S[i], v = T[j];
      if (u != v && !peri_intersect(c[u], c[v]))
        adj[u].push_back(v);
    }
    rep(i, T.size()) {
      int u = T[i];
      rep(j, T.size()) {
        int v = T[j];
        if (u != v && cover(c[u], c[v]))
          adj[u].push_back(v);
      }
      adj[n].push_back(u);
      adj[u].push_back(n + 1);
    }
    adj[n].push_back(n + 1);

    rep(u, n + 2) dp[u] = -1;
    rep(u, n + 2) dfs(u);

    printf("%d\\n", *max_element(dp, dp + n + 2) - 1);
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

class Point {
public:
  int x, y;
  Point() {}
  Point(int X, int Y) : x(X), y(Y) {}
};

class Circle {
public:
  Point c;
  int r;
  Circle() {}
  Circle(const Point &C, int R) : c(C), r(R) {}
};

#define sq(x) (x) * (x)
int dist2(const Point &p, const Point &q) {
  return sq(p.x - q.x) + sq(p.y - q.y);
}

bool cover(const Circle &c, const Point &p) { return dist2(p, c.c) <= sq(c.r); }

bool cover(const Circle &c1, const Circle &c2) {
  return c1.r >= c2.r && dist2(c1.c, c2.c) <= sq(c1.r - c2.r);
}

bool peri_intersect(const Circle &c1, const Circle &c2) {
  int d2 = dist2(c1.c, c2.c);
  return sq(c1.r - c2.r) <= d2 && d2 <= sq(c1.r + c2.r);
}

int dp[1002];
vector<int> adj[1002];

int dfs(int u) {
  if (~dp[u])
    return dp[u];
  int res = 0;
  rep(i, adj[u].size()) {
    int v = adj[u][i];
    res = max(res, dfs(v) + 1);
  }
  return dp[u] = res;
}

int main() {
  for (int n; scanf("%d", &n), n;) {
    Point s, t;
    scanf("%d%d%d%d", &s.x, &s.y, &t.x, &t.y);
    Circle c[1000];
    vector<int> S, T;
    rep(i, n) {
      scanf("%d%d%d", &c[i].c.x, &c[i].c.y, &c[i].r);
      bool bs = cover(c[i], s);
      bool bt = cover(c[i], t);
      if (bs ^ bt) {
        if (bs)
          S.push_back(i);
        if (bt)
          T.push_back(i);
      }
    }

    rep(u, n + 2) adj[u].clear();
    rep(i, S.size()) {
      int u = S[i];
      rep(j, S.size()) {
        int v = S[j];
        if (u != v && cover(c[v], c[u]))
          adj[u].push_back(v);
      }
      adj[n].push_back(S[u]);
      adj[u].push_back(n + 1);
    }
    rep(i, S.size()) rep(j, T.size()) {
      int u = S[i], v = T[j];
      if (u != v && !peri_intersect(c[u], c[v]))
        adj[u].push_back(v);
    }
    rep(i, T.size()) {
      int u = T[i];
      rep(j, T.size()) {
        int v = T[j];
        if (u != v && cover(c[u], c[v]))
          adj[u].push_back(v);
      }
      adj[n].push_back(u);
      adj[u].push_back(n + 1);
    }
    adj[n].push_back(n + 1);

    rep(u, n + 2) dp[u] = -1;
    rep(u, n + 2) dfs(u);

    printf("%d\\n", *max_element(dp, dp + n + 2) - 1);
  }

  return 0;
}