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

#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>

#define MAX_M (100)
#define EPS (1e-5)
#define SQ(X) ((X) * (X))

using namespace std;

typedef struct {
  double x, y;
} Point;

vector<Point> Rect[MAX_M];
vector<int> G[MAX_M];
bool used[MAX_M];

double getSize(double a, double b, double c) {
  double s;

  s = (a + b + c) / 2.0;

  if (s * (s - a) * (s - b) * (s - c) <= EPS) {
    return (0);
  }

  return (sqrt(s * (s - a) * (s - b) * (s - c)));
}

double dist(Point a, Point b) { return (sqrt(SQ(a.x - b.x) + SQ(a.y - b.y))); }

bool checkCross(vector<Point> p, vector<Point> q) {
  for (int i = 0; i < 4; i++) {
    for (int j = i + 1; j < 4; j++) {
      for (int k = j + 1; k < 4; k++) {
        for (int t = 0; t < 4; t++) {
          double AB = dist(p[i], p[j]), BC = dist(p[j], p[k]),
                 CA = dist(p[k], p[i]);
          double AP = dist(p[i], q[t]), BP = dist(p[j], q[t]),
                 CP = dist(p[k], q[t]);
          double S = getSize(AB, BC, CA);
          double M =
              getSize(AP, BP, AB) + getSize(AP, CP, CA) + getSize(BP, CP, BC);
          if (fabs(S - M) <= EPS) {
            return (true);
          }
        }
      }
    }
  }

  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      if (i == j)
        continue;
      for (int k = 0; k < 4; k++) {
        for (int l = 0; l < 4; l++) {
          if (k == l)
            continue;
          Point p1 = p[i], p2 = p[j], p3 = q[k], p4 = q[l];
          double a =
              (p3.x - p4.x) * (p1.y - p3.y) + (p3.y - p4.y) * (p3.x - p1.x);
          double b =
              (p3.x - p4.x) * (p2.y - p3.y) + (p3.y - p4.y) * (p3.x - p2.x);
          double c =
              (p1.x - p2.x) * (p3.y - p1.y) + (p1.y - p2.y) * (p1.x - p3.x);
          double d =
              (p1.x - p2.x) * (p4.y - p1.y) + (p1.y - p2.y) * (p1.x - p4.x);
          if (a * b < 0 && c * d < 0)
            return (true);
        }
      }
    }
  }

  return (false);
}

void dfs(int k) {
  used[k] = true;
  for (int i = 0; i < (int)G[k].size(); i++) {
    if (!used[G[k][i]]) {
      dfs(G[k][i]);
    }
  }
}

int main() {
  int n, m;
  int ans;

  while (scanf("%d", &n) && n) {

    for (int i = 0; i < n; i++) {
      scanf("%d", &m);
      for (int j = 0; j < m; j++) {
        Rect[j].clear();
        G[j].clear();
      }

      for (int j = 0; j < m; j++) {
        for (int k = 0; k < 4; k++) {
          Point in;
          scanf("%lf %lf", &in.x, &in.y);
          Rect[j].push_back(in);
        }
      }

      for (int j = 0; j < m; j++) {
        for (int k = 0; k < m; k++) {
          if (checkCross(Rect[j], Rect[k]) == true) {
            G[j].push_back(k);
            G[k].push_back(j);
          }
        }
      }

      memset(used, false, sizeof(used));
      ans = 0;
      for (int j = 0; j < m; j++) {
        if (used[j] == false) {
          ans++;
          dfs(j);
        }
      }

      printf("%d\\n", ans);
    }
  }

  return (0);
}

Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>

#define MAX_M (100)
#define EPS (1e-5)
#define SQ(X) ((X) * (X))

using namespace std;

typedef struct {
  double x, y;
} Point;

vector<Point> Rect[MAX_M];
vector<int> G[MAX_M];
bool used[MAX_M];
int flag;

double getSize(double a, double b, double c) {
  double s;

  s = (a + b + c) / 2.0;

  flag |= s * (s - a) * (s - b) * (s - c) < 0;

  return (sqrt(s * (s - a) * (s - b) * (s - c)));
}

bool checkCross(vector<Point> p, vector<Point> q) {
  double len[5];
  double areaA, areaB;

  for (int i = 0; i < 4; i++) {
    len[i] =
        sqrt(SQ(p[i].x - p[(i + 1) % 4].x) + SQ(p[i].y - p[(i + 1) % 4].y));
  }
  len[4] = sqrt(SQ(p[0].x - p[2].x) + SQ(p[0].y - p[2].y));
  areaA = getSize(len[0], len[1], len[4]) + getSize(len[2], len[3], len[4]);

  for (int i = 0; i < 4; i++) {
    areaB = 0;
    for (int j = 0; j < 4; j++) {
      double two[2];
      two[0] = sqrt(SQ(p[j].x - q[i].x) + SQ(p[j].y - q[i].y));
      two[1] =
          sqrt(SQ(p[(j + 1) % 4].x - q[i].x) + SQ(p[(j + 1) % 4].y - q[i].y));
      areaB += getSize(len[j], two[0], two[1]);
    }
    if (fabs(areaA - areaB) <= EPS) {
      return (true);
    }
  }

  return (false);
}

void dfs(int k) {
  used[k] = true;
  for (int i = 0; i < G[k].size(); i++) {
    if (!used[G[k][i]]) {
      dfs(G[k][i]);
    }
  }
}

int main() {
  int n, m;
  int ans;

  while (scanf("%d", &n) && n) {

    for (int i = 0; i < n; i++) {
      scanf("%d", &m);
      for (int j = 0; j < m; j++) {
        Rect[j].clear();
        G[j].clear();
      }

      for (int j = 0; j < m; j++) {
        for (int k = 0; k < 4; k++) {
          Point in;
          scanf("%lf %lf", &in.x, &in.y);
          Rect[j].push_back(in);
        }
      }

      for (int j = 0; j < m; j++) {
        for (int k = 0; k < m; k++) {
          if (checkCross(Rect[j], Rect[k]) == true) {
            G[j].push_back(k);
            G[k].push_back(j);
          }
        }
      }

      memset(used, false, sizeof(used));
      ans = 0;
      for (int j = 0; j < m; j++) {
        if (used[j] == false) {
          ans++;
          dfs(j);
        }
      }

      printf("%d\\n", ans);
    }
  }

  return (flag);
}