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

#include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (int i = 0; i < (n); i++)
#define fi first
#define se second
typedef double D;
typedef complex<D> P;
typedef pair<P, P> L;
typedef vector<P> VP;
const D EPS = 1e-9;
#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)
D dot(P a, P b) { return (conj(a) * b).X; }
D cross(P a, P b) { return (conj(a) * b).Y; }
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(b) < norm(c))
    return -2;
  return 0;
}
bool isecSP(P a1, P a2, P b) { return !ccw(a1, a2, b); }
bool isecSS(P a1, P a2, P b1, P b2) {
  if (a1 == b1 && !isecSP(a1, a2, b2) || a1 == b2 && !isecSP(a1, a2, b1) ||
      a2 == b1 && !isecSP(a1, a2, b2) || a2 == b2 && !isecSP(a1, a2, b1))
    return 0;
  return ccw(a1, a2, b1) * ccw(a1, a2, b2) <= 0 &&
         ccw(b1, b2, a1) * ccw(b1, b2, a2) <= 0;
}
string s[101];
double x, y;
vector<int> a[101], aa[12];
int color[11], ans, n, used[101], yy;
void dfs(int p, int d) {
  if (d + 1 >= ans)
    return;
  if (p == n) {
    ans = min(ans, d + 1);
    return;
  }
  int y[12] = {}, pp = 0, pe;
  r(j, aa[p].size()) y[aa[p][j]] = 1;
  for (int i = 0; i < d + 2; i++) {
    int f = 0;
    r(j, 12) if (y[j] && color[j] == i) f++;
    if (!f) {
      color[p] = i;
      if (i == d + 1)
        dfs(p + 1, d + 1);
      else
        dfs(p + 1, d);
      color[p] = 11;
    }
  }
}
int main() {
  while (cin >> n, n) {
    ans = 1e9;
    yy = n;
    r(i, 11) color[i] = 11;
    r(i, 12) aa[i].clear();
    r(i, 101) a[i].clear();
    VP v[n];
    for (int i = 0; i < yy; i++) {
      cin >> s[i];
      while (cin >> x, x != -1) {
        cin >> y;
        v[i].push_back(P(x, y));
      }
    }
    r(i, n) r(j, v[i].size()) r(k, n) if (i != k)
        r(l, v[k].size()) if (isecSS(v[i][j], v[i][(j + 1) % (int)v[i].size()],
                                     v[k][l], v[k][(l + 1) % (int)v[k].size()]))
            a[i]
                .push_back(k);
    r(i, n) sort(a[i].begin(), a[i].end());
    r(i, n) a[i].erase(unique(a[i].begin(), a[i].end()), a[i].end());
    // r(i,n)cout<<a[i].size()<<endl;
    int c = 0;
    map<int, int> m;
    memset(used, 0, sizeof(used));
    r(i, yy) if (!used[i]) {
      used[i] = 1;
      r(j, yy) if (!used[j] && s[i] == s[j]) {
        used[j] = 1;
        m[j] = c;
        r(k, a[j].size()) aa[c].push_back(a[j][k]);
        n--;
      }
      m[i] = c;
      r(k, a[i].size()) aa[c].push_back(a[i][k]);
      c++;
    }
    r(i, n) r(j, aa[i].size()) aa[i][j] = m[aa[i][j]];
    r(i, n) sort(aa[i].begin(), aa[i].end());
    r(i, n) aa[i].erase(unique(aa[i].begin(), aa[i].end()), aa[i].end());
    color[0] = 0;
    // r(i,aa[0].size())cout<<"    "<<aa[0][i]<<endl;
    dfs(1, 0);
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (int i = 0; i < (n); i++)
#define fi first
#define se second
typedef double D;
typedef complex<D> P;
typedef pair<P, P> L;
typedef vector<P> VP;
const D EPS = 1e-9;
#define X real()
#define Y imag()
#define LE(n, m) ((n) < (m) + EPS)
#define GE(n, m) ((n) + EPS > (m))
#define EQ(n, m) (abs((n) - (m)) < EPS)
D dot(P a, P b) { return (conj(a) * b).X; }
D cross(P a, P b) { return (conj(a) * b).Y; }
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(b) < norm(c))
    return -2;
  return 0;
}
bool isecSP(P a1, P a2, P b) { return !ccw(a1, a2, b); }
bool isecSS(P a1, P a2, P b1, P b2) {
  if (a1 == b1 && !isecSP(a1, a2, b2) || a1 == b2 && !isecSP(a1, a2, b1) ||
      a2 == b1 && !isecSP(a1, a2, b2) || a2 == b2 && !isecSP(a1, a2, b1))
    return 0;
  return ccw(a1, a2, b1) * ccw(a1, a2, b2) <= 0 &&
         ccw(b1, b2, a1) * ccw(b1, b2, a2) <= 0;
}
string s[11];
double x, y;
vector<int> a[11];
int color[11], ans, n, used[11], yy;
void dfs(int p, int d) { // cout<<p<<d<<endl;
  if (p == n) {
    ans = min(ans, d);
    return;
  }
  int y[12] = {}, pp = 0, pe;
  r(j, a[p].size()) y[a[p][j]] = 1;
  r(i, p) if (s[i] == s[p]) {
    pp++;
    pe = i;
    r(j, 11) if (s[j] != s[p] && y[j] && color[i] == color[j]) return;
  }
  if (pp) {
    color[p] = color[pe];
    dfs(p + 1, d);
    color[p] = 11;
  }
  for (int i = 0; i < d + 2; i++) {
    int f = 0;
    r(j, 12) if (y[j] && i == j) f++;
    if (!f) {
      color[p] = i;
      if (i == d + 1)
        dfs(p + 1, d + 1);
      else
        dfs(p + 1, d);
      color[p] = 11;
    }
  }
}
int main() {
  while (cin >> n, n) {
    ans = 1e9;
    yy = n;
    r(i, 11) color[i] = 11;
    r(i, 11) a[i].clear();
    VP v[n];
    for (int i = 0; i < yy; i++) {
      cin >> s[i];
      while (cin >> x, x != -1) {
        cin >> y;
        v[i].push_back(P(x, y));
      }
    }
    r(i, n) r(j, v[i].size()) r(k, n) if (i != k)
        r(l, v[k].size()) if (isecSS(v[i][j], v[i][(j + 1) % (int)v[i].size()],
                                     v[k][l], v[k][(l + 1) % (int)v[k].size()]))
            a[i]
                .push_back(k);
    r(i, n) sort(a[i].begin(), a[i].end());
    r(i, n) a[i].erase(unique(a[i].begin(), a[i].end()), a[i].end());
    // r(i,n)cout<<a[i].size()<<endl;
    color[0] = 0;
    dfs(1, 0);
    cout << ans << endl;
  }
}