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

#include <complex>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef complex<double> P;
typedef pair<P, P> L;
#define X real()
#define Y imag()
const double EPS = (1e-7);
const int INF = 1 << 20;
double dot(P &a, P &b) { return a.X * b.X + a.Y * b.Y; }
double cross(P &a, P &b) { return a.X * b.Y - a.Y * b.X; }

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 (norm(c) > norm(b) - EPS)
    return -2;
  return 0;
}

bool is_adjacent(vector<P> &A, vector<P> &B) {
  int n = A.size(), m = B.size();
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      P x = A[i], y = A[(i + 1) % n], z = B[j], w = B[(j + 1) % m];
      int s = ccw(x, y, z), t = ccw(x, y, w);
      if (abs(s) == 1 or abs(t) == 1)
        continue;
      if (s * t > 0)
        continue;
      return true;
    }
  }
  return false;
}

int main() {
  int n;
  while (cin >> n, n) {
    map<string, int> M;
    vector<vector<vector<P>>> V(n);
    int t = 1;
    for (int i = 0; i < n; ++i) {
      string s;
      cin >> s;
      vector<P> Poly;
      if (!M[s]) {
        M[s] = t;
        ++t;
      }
      double x;
      while (cin >> x, x != -1) {
        double y;
        cin >> y;
        Poly.emplace_back(x, y);
      }
      V[M[s] - 1].emplace_back(Poly);
    }
    int c = t - 1;
    vector<vector<int>> G(c, vector<int>(c, 0));
    for (int i = 0; i < c; ++i) {
      for (int j = 0; j < i; ++j) {
        for (int k = 0; k < V[i].size(); ++k) {
          for (int l = 0; l < V[j].size(); ++l) {
            if (is_adjacent(V[i][k], V[j][l]))
              ++G[i][j], ++G[j][i];
          }
        }
      }
    }

    /*
    for(int i=0; i<c; i++) {
      for(int j=0; j<c; j++) {
        fprintf(stderr, "%d ", G[i][j]);
      }
      fprintf(stderr, "\\n");
    }
    */

    vector<int> dp(1 << c, INF);
    dp[0] = 0;
    for (int bit = 0; bit < (1 << c); bit++) {
      for (int mask = 0; mask < (1 << c); mask++) {
        if ((bit & mask) != 0)
          continue;
        bool ok = true;
        for (int i = 0; i < c; i++) {
          for (int j = 0; j < c; j++) {
            if (!(mask >> i & 1))
              continue;
            if (!(mask >> j & 1))
              continue;
            if (G[i][j])
              ok = false;
          }
        }

        int nbit = bit | mask;
        if (ok)
          dp[nbit] = min(dp[nbit], dp[bit] + 1);
      }
    }
    cout << dp[(1 << c) - 1] << endl;
  }
}


Error: WA

Fixed submission:

#include <complex>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef complex<double> P;
typedef pair<P, P> L;
#define X real()
#define Y imag()
const double EPS = (1e-7);
const int INF = 1 << 20;
double dot(P &a, P &b) { return a.X * b.X + a.Y * b.Y; }
double cross(P &a, P &b) { return a.X * b.Y - a.Y * b.X; }

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 (norm(c) > norm(b) - EPS)
    return -2;
  return 0;
}

bool is_adjacent(vector<P> &A, vector<P> &B) {
  int n = A.size(), m = B.size();
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      P x = A[i], y = A[(i + 1) % n], z = B[j], w = B[(j + 1) % m];
      int s = ccw(x, y, z), t = ccw(x, y, w);
      if (abs(s) == 1 or abs(t) == 1)
        continue;
      if (s * t > 0)
        continue;
      return true;
    }
  }
  return false;
}

int main() {
  int n;
  while (cin >> n, n) {
    map<string, int> M;
    vector<vector<vector<P>>> V(n);
    int t = 1;
    for (int i = 0; i < n; ++i) {
      string s;
      cin >> s;
      vector<P> Poly;
      if (!M[s]) {
        M[s] = t;
        ++t;
      }
      double x;
      while (cin >> x, x != -1) {
        double y;
        cin >> y;
        Poly.emplace_back(x, y);
      }
      V[M[s] - 1].emplace_back(Poly);
    }
    int c = t - 1;
    vector<vector<int>> G(c, vector<int>(c, 0));
    for (int i = 0; i < c; ++i) {
      for (int j = 0; j < i; ++j) {
        for (int k = 0; k < V[i].size(); ++k) {
          for (int l = 0; l < V[j].size(); ++l) {
            if (is_adjacent(V[i][k], V[j][l]))
              ++G[i][j], ++G[j][i];
          }
        }
      }
    }

    for (int i = 0; i < c; i++) {
      for (int j = 0; j < c; j++) {
        fprintf(stderr, "%d ", G[i][j]);
      }
      fprintf(stderr, "\\n");
    }

    vector<int> dp(1 << c, INF);
    dp[0] = 0;
    for (int bit = 0; bit < (1 << c); bit++) {
      for (int mask = 0; mask < (1 << c); mask++) {
        if ((bit & mask) != 0)
          continue;
        bool ok = true;
        for (int i = 0; i < c; i++) {
          for (int j = 0; j < c; j++) {
            if (!(mask >> i & 1))
              continue;
            if (!(mask >> j & 1))
              continue;
            if (G[i][j])
              ok = false;
          }
        }

        int nbit = bit | mask;
        if (ok)
          dp[nbit] = min(dp[nbit], dp[bit] + 1);
      }
    }
    cout << dp[(1 << c) - 1] << endl;
  }
}