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

#include <cstdlib>
#include <iostream>
#include <map>
#include <stack>
#include <utility>
#include <vector>
using namespace std;

struct futon {
  int x, y;
  char d;
  futon(int x = 0, int y = 0, char d = 0) : x(x), y(y), d(d) {}
};

istream &operator>>(istream &is, futon &f) { return is >> f.x >> f.y >> f.d; }

const int LEFT_BOTTOM = (1 << 25);
const int dx[2][2][3] = {{{-1, 0, 0}, {0, -1, 1}}, {{2, 1, 1}, {0, -1, 1}}};
const int dy[2][2][3] = {{{0, 1, -1}, {-1, 0, 0}}, {{0, -1, 1}, {2, 1, 1}}};

typedef pair<int, int> point;

vector<int> color;
map<point, int> field;
vector<futon> futons;

bool dfs(int start) {
  stack<pair<int, int>> sta;
  sta.push(make_pair(start, 0));

  while (!sta.empty()) {
    const int idx = sta.top().first;
    const int c = sta.top().second;
    sta.pop();

    if (color[idx] != -1) {
      if (color[idx] == c)
        continue;
      else
        return false;
    }
    color[idx] = c;

    const int x = futons[idx].x;
    const int y = futons[idx].y;
    const int d = futons[idx].d - 'x';

    for (int i = 0; i < 2; ++i) {
      for (int j = 0; j < 3; ++j) {
        point np(x + dx[i][d][j], y + dy[i][d][j]);
        if (!field.count(np))
          continue;

        int next_c = (c ^ i);
        int next_idx = field[np];
        if (!(next_idx & LEFT_BOTTOM))
          next_c = !next_c;
        else
          next_idx &= ~LEFT_BOTTOM;

        sta.push(make_pair(next_idx, next_c));
      }
    }
  }

  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  for (int n; cin >> n && n;) {
    futons.clear();
    futons.resize(n);

    field.clear();
    for (int i = 0; i < n; ++i) {
      cin >> futons[i];
      field[point(futons[i].x, futons[i].y)] = (i | LEFT_BOTTOM);
      if (futons[i].d == 'x')
        field[point(futons[i].x + 1, futons[i].y)] = i;
      else
        field[point(futons[i].x, futons[i].y + 1)] = i;
    }

    color.assign(n, -1);
    string ans = "Yes";
    for (int i = 0; i < n; ++i) {
      if (color[i] == -1 && !dfs(i)) {
        ans = "No";
        break;
      }
    }

    cout << ans << endl;
  }

  return EXIT_SUCCESS;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

#define dump(a) (cerr << (#a) << " = " << (a) << endl)

template <class T> inline void chmax(T &a, const T &b) {
  if (b > a)
    a = b;
}
template <class T> inline void chmin(T &a, const T &b) {
  if (b < a)
    a = b;
}

template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << '(' << p.first << ", " << p.second << ')';
  return os;
}

template <class Tuple, unsigned Index>
void print_tuple(ostream &os, const Tuple &t) {}

template <class Tuple, unsigned Index, class Type, class... Types>
void print_tuple(ostream &os, const Tuple &t) {
  if (Index > 0)
    os << ", ";
  os << get<Index>(t);
  print_tuple<Tuple, Index + 1, Types...>(os, t);
}

template <class... Types>
ostream &operator<<(ostream &os, const tuple<Types...> &t) {
  os << '(';
  print_tuple<tuple<Types...>, 0, Types...>(os, t);
  return os << ')';
}

template <class Iterator>
ostream &dump_range(ostream &, Iterator, const Iterator &);

template <typename T> ostream &operator<<(ostream &os, vector<T> c) {
  return dump_range(os, c.cbegin(), c.cend());
}

template <class Iterator>
ostream &dump_range(ostream &os, Iterator first, const Iterator &last) {
  os << '[';
  for (int i = 0; first != last; ++i, ++first) {
    if (i)
      os << ", ";
    os << *first;
  }
  return os << ']';
}

struct futon {
  int x, y;
  char d;
  futon(int x = 0, int y = 0, char d = 0) : x(x), y(y), d(d) {}
};

istream &operator>>(istream &os, futon &f) { return os >> f.x >> f.y >> f.d; }

const int LEFT_BOTTOM = (1 << 25);

const int dx[2][2][3] = {{{-1, 0, 0}, {0, -1, 1}}, {{2, 1, 1}, {0, -1, 1}}};
const int dy[2][2][3] = {{{0, 1, -1}, {-1, 0, 0}}, {{0, -1, 1}, {2, 1, 1}}};

vector<int> color;
vector<vector<int>> field;
vector<futon> futons;

inline bool out(int x, int y) {
  return x < 0 || y < 0 || x >= (int)field[0].size() || y >= (int)field.size();
}

bool dfs(int idx, int c = 0) {
  if (color[idx] != -1)
    return color[idx] == c;
  color[idx] = c;

  const int d = futons[idx].d - 'x';
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 3; ++j) {
      const int nx = futons[idx].x + dx[i][d][j];
      const int ny = futons[idx].y + dy[i][d][j];
      if (out(nx, ny))
        continue;

      if (field[ny][nx] != -1) {
        int next_c = (c ^ i);
        int next_idx = field[ny][nx];
        if (!(next_idx & LEFT_BOTTOM))
          next_c = !next_c;
        else
          next_idx &= ~LEFT_BOTTOM;

        if (!dfs(next_idx, next_c))
          return false;
      }
    }
  }

  return true;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  for (int n; cin >> n && n;) {
    futons.assign(n, futon());
    vector<int> xs, ys;
    xs.reserve(n + n + 2);
    ys.reserve(n + n + 2);

    for (int i = 0; i < n; ++i) {
      cin >> futons[i];
      xs.emplace_back(futons[i].x);
      ys.emplace_back(futons[i].y);
      if (futons[i].d == 'x')
        xs.emplace_back(futons[i].x + 1);
      else
        ys.emplace_back(futons[i].y + 1);
    }

    sort(xs.begin(), xs.end());
    sort(ys.begin(), ys.end());
    xs.erase(unique(xs.begin(), xs.end()), xs.end());
    ys.erase(unique(ys.begin(), ys.end()), ys.end());

    for (int i = 0; i < n; ++i) {
      futons[i].x = lower_bound(xs.begin(), xs.end(), futons[i].x) - xs.begin();
      futons[i].y = lower_bound(ys.begin(), ys.end(), futons[i].y) - ys.begin();
    }

    field.assign(ys.size(), vector<int>(xs.size(), -1));
    for (int i = 0; i < n; ++i) {
      field[futons[i].y][futons[i].x] = (i | LEFT_BOTTOM);
      if (futons[i].d == 'x')
        field[futons[i].y][futons[i].x + 1] = i;
      else
        field[futons[i].y + 1][futons[i].x] = i;
    }

    color.assign(n, -1);
    string ans = "Yes";
    for (int i = 0; i < n; ++i) {
      if (color[i] == -1 && !dfs(i)) {
        ans = "No";
        break;
      }
    }

    cout << ans << endl;
  }

  return EXIT_SUCCESS;
}