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

#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)
// #define int long long
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define all(x) x.begin(), x.end()
using pii = pair<int, int>;
using ll = long long;

int t = 0;

struct Futon {
  int X[2], Y[2];
  int id;
  Futon(int x, int y, char c, int id) : id(id) {
    X[0] = X[1] = x, Y[0] = Y[1] = y;
    if (c == 'x')
      X[1] = x + 1;
    else
      Y[1] = y + 1;
  }
  bool operator<(const Futon &rhs) {
    if (X[0] == rhs.X[0])
      return Y[0] < rhs.Y[0];
    return X[0] < rhs.X[0];
  }
  void show() {
    cerr << X[0] << " " << Y[0] << " " << X[1] << " " << Y[1] << endl;
  }
  pii p1() { return {X[0], Y[0]}; }
  pii p2() { return {X[1], Y[1]}; }
};

constexpr int dx[4] = {0, 0, -1, 1}, dy[4] = {1, -1, 0, 0};
bool connect(Futon &a, Futon &b) {
  REP(i, 2) REP(j, 2) REP(k, 4) {
    if (a.X[i] + dx[k] == b.X[j] && a.Y[i] + dy[k] == b.Y[j])
      return true;
  }
  return false;
}

vector<int> G[100010];

bool solve() {
  t++;
  set<pii> stX, stY;
  int N;
  cin >> N;
  if (N == 0)
    return false;
  vector<Futon> fs;
  REP(i, N) {
    int x, y;
    char c;
    cin >> x >> y >> c;
    Futon f(x, y, c, i);
    fs.push_back(f);
    G[i].clear();
  }
  sort(all(fs));
  REP(i, N) {
    FOR(j, i + 1, N) {
      if (fs[j].X[0] > fs[i].X[0] + 2)
        break;
      if (connect(fs[i], fs[j])) {
        // fs[i].show();
        // fs[j].show();
        // cerr << endl;
        G[i].push_back(j);
        G[j].push_back(i);
      }
    }
  }
  map<pii, int> mp;
  vector<bool> done(N);
  bool possible = true;
  queue<int> que;
  REP(i, N) {
    if (!possible)
      break;
    if (done[i])
      continue;

    que.push(i);
    while (!que.empty()) {
      if (!possible)
        break;
      int id = que.front();
      que.pop();
      if (done[id])
        continue;
      done[id] = true;
      bool found1 = false, found2 = false;
      int x0 = fs[id].X[0], y0 = fs[id].Y[0];
      REP(k, 4) {
        if (!mp.count({x0 + dx[k], y0 + dy[k]}))
          continue;
        if (mp[{x0 + dx[k], y0 + dy[k]}] == 1)
          found1 = true;
        if (mp[{x0 + dx[k], y0 + dy[k]}] == 2)
          found2 = true;
      }
      if (found1 && found2)
        possible = false;
      int x1 = fs[id].X[1], y1 = fs[id].Y[1];
      bool found11 = false, found22 = false;
      REP(k, 4) {
        if (!mp.count({x1 + dx[k], y1 + dy[k]}))
          continue;
        if (mp[{x1 + dx[k], y1 + dy[k]}] == 1)
          found11 = true;
        if (mp[{x1 + dx[k], y1 + dy[k]}] == 2)
          found22 = true;
        if (mp[{x1 + dx[k], y1 + dy[k]}] == 1 && found1)
          possible = false;
        if (mp[{x1 + dx[k], y1 + dy[k]}] == 2 && found2)
          possible = false;
      }
      if (found1 && found11)
        possible = false;
      if (found2 && found22)
        possible = false;
      if (found11 && found22)
        possible = false;
      if (found2 || found11) {
        mp[{x0, y0}] = 2;
        mp[{x1, y1}] = 1;
      } else {
        mp[{x0, y0}] = 1;
        mp[{x1, y1}] = 2;
      }

      for (auto nxt : G[id]) {
        que.push(nxt);
      }
    }
  }
  REP(i, N) {
    int x = fs[i].X[0], y = fs[i].Y[0];
    int x1 = fs[i].X[1], y1 = fs[i].Y[1];
    if (!possible)
      break;
    if (mp[{x, y}] == 1) {
      REP(k, 4) {
        if (x + dx[k] == x1 && y + dy[k] == y1)
          continue;
        if (mp[{x + dx[k], y + dy[k]}] == 2)
          possible = false;
      }
    } else if (mp[{x, y}] == 2) {
      REP(k, 4) {
        if (x + dx[k] == x1 && y + dy[k] == y1)
          continue;
        if (mp[{x + dx[k], y + dy[k]}] == 1)
          possible = false;
      }
    } else {
      cerr << "not decided!!!!" << endl;
    }
  }
  if (possible)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return true;
}

signed main() {
  while (solve())
    ;
}


Error: 0

Fixed submission:

#ifdef _WIN32
#define getc_unlocked(stdin) getc(stdin)
#endif
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

const int dx[] = {0, -1, 0, 1, -1, 1, 1, -1};
const int dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
const double eps = 1e-8;

typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vgraph;
typedef vector<pii> vpii;
typedef vector<LL> vLL;
typedef vector<string> vs;

#define sz(a) a.size()
#define fori(i, a, b) for (int i(a), _b(b); i <= _b; ++i)
#define ford(i, a, b) for (int i(a), _b(b); i >= _b; --i)
#define forn(i, n) fori(i, 0, n - 1)
#define fora(i, a) forn(i, sz(a))
#define fore(it, c)                                                            \\
  for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(a) a.begin(), a.end()
#define mp make_pair
#define pb push_back
#define clear0(a) memset(a, 0, sizeof(a))
#define clearm1(a) memset(a, -1, sizeof(a))
#define maximum(a) (*max_element(all(a)))
#define minimum(a) (*min_element(all(a)))
#define findx(a, x) (find(all(a), x) - a.begin())
#define two(X) (1LL << (X))
#define contain(S, X) ((S)&two(X))
#define setbit(S, X) ((S) |= two(X))
#define clearbit(S, X) ((S) &= ~two(X))
#define togglebit(S, X) ((S) ^= two(X))
#define sqr(a) ((a) * (a))
#define fi "i.inp"
#define fo "output.out"
#define nmax 100
#define mmax 100
#define kmax 10
#define modulo 1000000007
#define maxc 999999999
map<int, map<int, int>> mmap;
vector<int> adj[nmax];
int a[nmax], b[nmax], c[nmax], d[nmax], fr[nmax];
int n;

int go(int u, int color) {
  fr[u] = color;
  fora(i, adj[u]) {
    int v = adj[u][i];
    if (fr[v] == -1)
      go(v, 1 - color);
    else if (fr[v] == fr[u])
      return 0;
  }
  return 1;
}
int main() {
#ifndef ONLINE_JUDGE
  // freopen(fi,"r",stdin);
  // freopen(fo,"w", stdout);
#endif
  vector<int> uu;

  while (1) {
    mmap.clear();
    int m = 0;
    string s;

    scanf("%d", &n);
    if (n == 0)
      break;
    fori(i, 1, n) {
      while (!adj[i].empty())
        adj[i].clear();
      fr[i] = -1;
      int u, v;
      cin >> u >> v >> s;
      ++m;
      if (s == "x") {
        mmap[u + 1][v - 1] = m;
        mmap[u + 2][v - 1] = m;
        a[m] = u + 1;
        b[m] = v - 1;
        c[m] = u + 2;
        d[m] = v - 1;
      } else {
        mmap[u + 1][v + 1] = m;
        mmap[u + 1][v + 2] = m;
        a[m] = u + 1;
        b[m] = v + 1;
        c[m] = u + 1;
        d[m] = v + 2;
      }
    }
    fori(i, 1, n) {
      int id1 = mmap[a[i]][b[i]];
      fori(j, 0, 3) {
        int u = a[i] + dx[j];
        int v = b[i] + dy[j];
        int id2 = mmap[u][v];
        if (id2 > 0 && id1 != id2) {
          adj[id1].pb(id2);
          adj[id2].pb(id1);
        }
      }
    }
    int ans = 1;
    fori(i, 1, n) if (fr[i] == -1) if (!go(i, 0)) {
      ans = 0;
      break;
    }
    if (ans)
      printf("Yes\\n");
    else
      printf("No\\n");
  }
  return 0;
}