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

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int inf = 1e9;
const double EPS = 1e-10;
const double INF = 1e18;
#define EQ(n, m) (abs((n) - (m)) < EPS)
#define X real()
#define Y imag()
typedef complex<double> P;
typedef vector<P> VP;
struct L : array<P, 2> {
  L(const P &a, const P &b) {
    at(0) = a;
    at(1) = b;
  }
  L() {}
};

double dot(P a, P b) { return (conj(a) * b).X; }
double cross(P a, P b) { return (conj(a) * b).Y; }
bool strictItsSP(const L &s, const P &p) {
  return abs(cross(s[0] - p, s[1] - p)) < EPS && dot(s[0] - p, s[1] - p) < -EPS;
}
bool overlapSS(const L &a, const L &b) {
  return strictItsSP(a, b[0]) || strictItsSP(a, b[1]) || strictItsSP(b, a[0]) ||
         strictItsSP(b, a[1]) || (a[0] == b[0] && a[1] == b[1]) ||
         (a[0] == b[1] && a[1] == b[0]);
}
bool isParallel(const P &a, const P &b) { return abs(cross(a, b)) < EPS; }
bool isParallel(const L &a, const L &b) {
  return isParallel(a[1] - a[0], b[1] - b[0]);
}

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    map<string, int> name;
    vector<vector<L>> edge;
    int num = 0;
    for (int i = 0; i < n; i++) {
      string city;
      cin >> city;
      if (name.count(city) == 0) {
        name[city] = num;
        num++;
        edge.push_back(vector<L>());
      }
      VP v;
      while (1) {
        int x, y;
        cin >> x;
        if (x == -1)
          break;
        cin >> y;
        v.push_back(P(x, y));
      }
      for (int j = 0; j < (int)v.size(); j++) {
        edge[name[city]].push_back(L(v[j], v[(j + 1) % v.size()]));
      }
    }

    vector<vector<bool>> adj(num, vector<bool>(num, false));
    for (int i = 0; i < num; i++) {
      for (int j = i + 1; j < num; j++) {
        for (int k = 0; k < (int)edge[i].size(); k++) {
          for (int l = 0; l < (int)edge[j].size(); l++) {
            if (isParallel(edge[i][k], edge[j][l]) &&
                overlapSS(edge[i][k], edge[j][l])) {
              adj[i][j] = adj[j][i] = true;
              k = edge[i].size();
              break;
            }
          }
        }
      }
    }

    int ans = inf;
    vector<int> ord(num);
    for (int i = 0; i < num; i++)
      ord[i] = i;
    do {
      int count = 1;
      int last = 0;
      for (int i = 1; i < num; i++) {
        for (int j = last; j < i; j++) {
          if (adj[ord[i]][ord[j]]) {
            count++;
            last = i;
            break;
          }
        }
      }
      ans = min(ans, count);
    } while (next_permutation(ord.begin(), ord.end()));
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int inf = 1e9;
const double EPS = 1e-10;
const double INF = 1e18;
#define EQ(n, m) (abs((n) - (m)) < EPS)
#define X real()
#define Y imag()
typedef complex<double> P;
typedef vector<P> VP;
struct L : array<P, 2> {
  L(const P &a, const P &b) {
    at(0) = a;
    at(1) = b;
  }
  L() {}
};

double dot(P a, P b) { return (conj(a) * b).X; }
double cross(P a, P b) { return (conj(a) * b).Y; }
bool strictItsSP(const L &s, const P &p) {
  return abs(cross(s[0] - p, s[1] - p)) < EPS && dot(s[0] - p, s[1] - p) < -EPS;
}
bool overlapSS(const L &a, const L &b) {
  return strictItsSP(a, b[0]) || strictItsSP(a, b[1]) || strictItsSP(b, a[0]) ||
         strictItsSP(b, a[1]) || (a[0] == b[0] && a[1] == b[1]) ||
         (a[0] == b[1] && a[1] == b[0]);
}
bool isParallel(const P &a, const P &b) { return abs(cross(a, b)) < EPS; }
bool isParallel(const L &a, const L &b) {
  return isParallel(a[1] - a[0], b[1] - b[0]);
}

int color[10];
int solve(int d, vector<vector<bool>> &adj) {
  if (d == 0) {
    int n = adj.size();
    vector<vector<int>> comp(n);
    for (int i = 0; i < n; i++) {
      comp[color[i]].push_back(i);
    }
    int res = 0;
    for (int i = 0; i < n; i++) {
      if (comp[i].empty())
        continue;
      for (int j = 0; j < (int)comp[i].size(); j++) {
        for (int k = j + 1; k < (int)comp[i].size(); k++) {
          if (adj[comp[i][j]][comp[i][k]]) {
            j = inf;
            i = n;
            res = inf;
            break;
          }
        }
      }
      res++;
    }
    return res;
  }
  int res = inf;
  for (int i = 0; i < d; i++) {
    color[d - 1] = i;
    res = min(res, solve(d - 1, adj));
  }
  return res;
}

int main() {
  while (1) {
    int n;
    cin >> n;
    if (n == 0)
      break;

    map<string, int> name;
    vector<vector<L>> edge;
    int num = 0;
    for (int i = 0; i < n; i++) {
      string city;
      cin >> city;
      if (name.count(city) == 0) {
        name[city] = num;
        num++;
        edge.push_back(vector<L>());
      }
      VP v;
      while (1) {
        int x, y;
        cin >> x;
        if (x == -1)
          break;
        cin >> y;
        v.push_back(P(x, y));
      }
      for (int j = 0; j < (int)v.size(); j++) {
        edge[name[city]].push_back(L(v[j], v[(j + 1) % v.size()]));
      }
    }

    vector<vector<bool>> adj(num, vector<bool>(num, false));
    for (int i = 0; i < num; i++) {
      for (int j = i + 1; j < num; j++) {
        for (int k = 0; k < (int)edge[i].size(); k++) {
          for (int l = 0; l < (int)edge[j].size(); l++) {
            if (isParallel(edge[i][k], edge[j][l]) &&
                overlapSS(edge[i][k], edge[j][l])) {
              adj[i][j] = adj[j][i] = true;
              k = edge[i].size();
              break;
            }
          }
        }
      }
    }

    cout << solve(num, adj) << endl;
  }
  return 0;
}