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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

struct before_main {
  before_main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
} before_main;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

struct Point {
  int x, y;

  Point(int x, int y) : x(x), y(y) {}

  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
};

struct Segment {
  Point s, t;
  Segment(Point s, Point t) : s(s), t(t) {}
};

bool eq(Point a, Point b) { return (a.x == b.x && a.y == b.y); }

bool eq(Segment a, Segment b) {
  if (eq(a.s, b.s) && eq(a.t, b.t))
    return true;
  if (eq(a.s, b.t) && eq(a.t, b.s))
    return true;
  return false;
}

int dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }

int cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }

bool isParallel(Segment a, Segment b) {
  return (cross(a.s - a.t, b.s - b.t) == 0);
}

bool onSegment(Segment a, Point p) {
  if (eq(a.s, p))
    return false;
  if (eq(a.t, p))
    return false;
  if (cross(a.s - p, a.t - p) != 0)
    return false;
  if (dot(a.t - a.s, p - a.s) <= 0)
    return false;
  if (dot(a.s - a.t, p - a.t) <= 0)
    return false;
  return true;
}

bool haveCommonSegment(Segment a, Segment b) {
  if (eq(a, b))
    return true;
  if (!isParallel(a, b))
    return false;
  if (onSegment(a, b.s))
    return true;
  if (onSegment(a, b.t))
    return true;
  if (onSegment(b, a.s))
    return true;
  if (onSegment(b, a.t))
    return true;
  return false;
}

bool haveCommonSegment(vector<Segment> &a, vector<Segment> &b) {
  for (Segment A : a)
    for (Segment B : b)
      if (haveCommonSegment(A, B))
        return true;
  return false;
}

bool check(int G[10][10], int S) {
  for (int i = 0; i < 10; i++) {
    if ((S >> i & 1) == 0)
      continue;
    for (int j = 0; j < 10; j++) {
      if ((S >> j & 1) == 0)
        continue;
      if (G[i][j])
        return false;
    }
  }
  return true;
}

int main() {

  while (1) {
    int N;
    vector<Segment> Country[10];
    map<string, int> id;
    cin >> N;
    if (N == 0)
      break;
    for (int i = 0; i < N; i++) {
      string name;
      vector<Point> v;
      cin >> name;
      if (id.count(name) == 0)
        id[name] = id.size() - 1;
      while (1) {
        int x, y;
        cin >> x;
        if (x == -1)
          break;
        cin >> y;
        v.push_back(Point(x, y));
      }
      int size = v.size();
      for (int j = 0; j < size; j++) {
        Segment segment = Segment(v[j], v[(j + 1) % size]);
        Country[id[name]].push_back(segment);
      }
    }
    int M = id.size();
    int G[10][10] = {};
    for (int i = 0; i < M; i++) {
      for (int j = 0; j < M; j++) {
        if (i != j) {
          G[i][j] = haveCommonSegment(Country[i], Country[j]);
        }
      }
    }

    vector<int> edge;
    for (int S = 0; S < (1 << M); S++)
      if (check(G, S))
        edge.push_back(S);

    int dp[(1 << M)];
    fill(dp, dp + (1 << M), 10);
    dp[0] = 0;

    for (int S = 0; S < (1 << M); S++)
      for (int T : edge)
        dp[S | T] = min(dp[S | T], dp[S] + 1);

    cout << dp[(1 << M) - 1] << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

struct before_main {
  before_main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
} before_main;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

struct Point {
  int x, y;

  Point(int x, int y) : x(x), y(y) {}

  Point operator+(Point p) { return Point(x + p.x, y + p.y); }
  Point operator-(Point p) { return Point(x - p.x, y - p.y); }
};

struct Segment {
  Point s, t;
  Segment(Point s, Point t) : s(s), t(t) {}
};

bool eq(Point a, Point b) { return (a.x == b.x && a.y == b.y); }

bool eq(Segment a, Segment b) {
  if (eq(a.s, b.s) && eq(a.t, b.t))
    return true;
  if (eq(a.s, b.t) && eq(a.t, b.s))
    return true;
  return false;
}

int dot(Point a, Point b) { return a.x * b.x + a.y * b.y; }

int cross(Point a, Point b) { return a.x * b.y - a.y * b.x; }

bool isParallel(Segment a, Segment b) {
  return (cross(a.s - a.t, b.s - b.t) == 0);
}

bool onSegment(Segment a, Point p) {
  if (eq(a.s, p))
    return false;
  if (eq(a.t, p))
    return false;
  if (cross(a.s - p, a.t - p) != 0)
    return false;
  if (dot(a.t - a.s, p - a.s) <= 0)
    return false;
  if (dot(a.s - a.t, p - a.t) <= 0)
    return false;
  return true;
}

bool haveCommonSegment(Segment a, Segment b) {
  if (eq(a, b))
    return true;
  if (!isParallel(a, b))
    return false;
  if (onSegment(a, b.s))
    return true;
  if (onSegment(a, b.t))
    return true;
  if (onSegment(b, a.s))
    return true;
  if (onSegment(b, a.t))
    return true;
  return false;
}

bool haveCommonSegment(vector<Segment> &a, vector<Segment> &b) {
  for (Segment A : a)
    for (Segment B : b)
      if (haveCommonSegment(A, B))
        return true;
  return false;
}

bool check(int G[10][10], int S) {
  for (int i = 0; i < 10; i++) {
    if ((S >> i & 1) == 0)
      continue;
    for (int j = 0; j < 10; j++) {
      if ((S >> j & 1) == 0)
        continue;
      if (G[i][j])
        return false;
    }
  }
  return true;
}

int main() {
  int N;
  vector<Segment> Country[10];
  map<string, int> id;

  while (1) {
    cin >> N;
    if (N == 0)
      break;
    for (int i = 0; i < N; i++) {
      string name;
      vector<Point> v;
      cin >> name;
      id[name] = id.size() - 1;
      while (1) {
        int x, y;
        cin >> x;
        if (x == -1)
          break;
        cin >> y;
        v.push_back(Point(x, y));
      }
      int size = v.size();
      for (int i = 0; i < size; i++) {
        Segment segment = Segment(v[i], v[(i + 1) % size]);
        Country[id[name]].push_back(segment);
      }
    }
    int M = id.size();
    int G[10][10] = {};
    for (int i = 0; i < M; i++)
      for (int j = 0; j < M; j++)
        G[i][j] = haveCommonSegment(Country[i], Country[j]);

    vector<int> edge;
    for (int S = 0; S < (1 << M); S++)
      if (check(G, S))
        edge.push_back(S);

    int dp[(1 << M)];
    fill(dp, dp + (1 << M), 10);
    dp[0] = 0;

    for (int S = 0; S < (1 << M); S++)
      for (int T : edge)
        dp[S | T] = min(dp[S | T], dp[S] + 1);

    cout << dp[(1 << M) - 1] << endl;
  }
  return 0;
}