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

#include <algorithm>
#include <complex>
#include <cstring>
#include <iostream>
#include <vector>

#define rep(i, a) for (int i = 0; i < a; i++)
#define EQ(a, b) (abs((a) - (b)) < EPS)
#define pb push_back
#define fs first
#define sc second
using namespace std;

typedef double D;
typedef complex<D> P;
typedef pair<P, P> L;
typedef vector<P> Poly;

const D EPS = 1e-8;

namespace std {
bool operator<(const P &a, const P &b) {
  return EQ(real(a), real(b)) ? imag(a) < imag(b) : real(a) < real(b);
}
} // namespace std

vector<int> g[110];
vector<int> rg[110];
vector<int> vs;
bool use[110];
int ord[110];
int n, d;
Poly tri[110];
Poly rect[110];

P unit(P p) { return p / abs(p); }
pair<P, P> norm(P p) { return make_pair(p * P(0, 1), p * P(0, -1)); }

D dot(P x, P y) { return real(conj(x) * y); }
D cross(P x, P y) { return imag(conj(x) * y); }

vector<P> convex_hull(vector<P> v) {
  int n = v.size(), k = 0;
  sort(v.begin(), v.end());
  vector<P> r(2 * n);
  for (int i = 0; i < n; i++) {
    while (k > 1 && cross(r[k - 1] - r[k - 2], v[i] - r[k - 1]) <= EPS)
      k--;
    r[k++] = v[i];
  }
  for (int i = n - 2, t = k; i >= 0; i--) {
    while (k > t && cross(r[k - 1] - r[k - 2], v[i] - r[k - 1]) <= EPS)
      k--;
    r[k++] = v[i];
  }
  r.resize(k - 1);
  return r;
}

int ccw(P a, P b, P c) {
  b -= a;
  c -= a;
  if (cross(b, c) > EPS)
    return 1;
  if (cross(b, c) < -EPS)
    return -1;
  if (dot(b, c) < -EPS)
    return 2;
  if (abs(b) < abs(c))
    return -2;
  return 0;
}

bool is_cp(L a, L b) {
  if (ccw(a.fs, a.sc, b.fs) * ccw(a.fs, a.sc, b.sc) <= 0)
    if (ccw(b.fs, b.sc, a.fs) * ccw(b.fs, b.sc, a.sc) <= 0)
      return true;
  return false;
}

bool inter_pos(Poly p, P x) {
  int s = p.size();
  if (!s)
    return false;
  rep(i, s) if (ccw(p[i], p[(i + 1) % s], x) == -1) return false;
  return true;
}

bool cross_poly(Poly a, Poly b) {
  int n1 = a.size(), n2 = b.size();
  rep(i, n2) if (inter_pos(a, b[i])) return true;
  rep(i, n1) if (inter_pos(b, a[i])) return true;
  rep(i, n1) rep(j, n2) if (is_cp(L(a[i], a[(i + 1) % n1]),
                                  L(b[j], b[(j + 1) % n2]))) return true;
  return false;
}

void fdfs(int v) {
  use[v] = true;
  for (int i = 0; i < g[v].size(); i++) {
    if (!use[g[v][i]])
      fdfs(g[v][i]);
  }
  vs.pb(v);
}

void rdfs(int v, int k) {
  use[v] = true;
  ord[v] = k;
  for (int i = 0; i < rg[v].size(); i++) {
    if (!use[rg[v][i]])
      rdfs(rg[v][i], k);
  }
}

int scc() {
  memset(use, 0, sizeof(use));
  vs.clear();
  for (int i = 0; i < n; i++) {
    if (!use[i])
      fdfs(i);
  }

  memset(use, 0, sizeof(use));
  int k = 0;
  for (int i = vs.size() - 1; i >= 0; i--) {
    if (!use[vs[i]])
      rdfs(vs[i], k++);
  }
  return k;
}

int main() {
  while (cin >> n >> d, n + d) {
    rep(i, n) {
      tri[i].resize(3);
      cin >> tri[i][0].real() >> tri[i][0].imag();
      cin >> tri[i][1].real() >> tri[i][1].imag();
      cin >> tri[i][2].real() >> tri[i][2].imag();
      tri[i] = convex_hull(tri[i]);

      rect[i].clear();
      rep(j, 3) {
        int n1 = (j + 1) % 3, n2 = (j + 2) % 3;
        if (EQ(abs(tri[i][n1] - tri[i][j]), abs(tri[i][n2] - tri[i][j]))) {
          rect[i].pb(tri[i][n1]);
          rect[i].pb(tri[i][n2]);
          break;
        }
      }

      P ver = (D)d * unit(norm(rect[i][0] - rect[i][1]).sc);
      rect[i].pb(ver + rect[i][1]);
      rect[i].pb(ver + rect[i][0]);
      rect[i] = convex_hull(rect[i]);
    }

    rep(i, n) {
      g[i].clear();
      rg[i].clear();
    }
    rep(i, n) {
      rep(j, n) {
        if (i == j)
          continue;
        if (cross_poly(rect[i], tri[j])) {
          g[i].pb(j);
          rg[j].pb(i);
        }
      }
    }

    int cmp = scc();

    int in[110] = {0};
    rep(i, n) {
      rep(j, g[i].size()) if (ord[g[i][j]] != ord[i]) in[ord[g[i][j]]]++;
    }

    int res = 0;
    rep(i, cmp) if (!in[i]) res++;
    cout << res << endl;
  }
}

Error: -6
d7fd8355-39e3-4d9f-ad76-bed48c2622b3.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p00237/C++/s665509414.cpp:11: int main(): Assertion `n+d>0' failed.


Fixed submission:

#include <cassert>
#include <iostream>
using namespace std;

int main() {
  int n, d;
  int x, y;
  int cnt = 0;
  while (cin >> n >> d) {
    if (cnt >= 13)
      cerr << n << " " << d << endl;
    assert(n + d > 0);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 3; j++) {
        cin >> x >> y;
        if (cnt >= 13)
          cerr << x << " " << y << ((j == 2) ? '\\n' : ' ');
      }
    }
    cnt++;
  }
}