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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

typedef vector<pii> vp;

int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int main() {
  int n;
  while (cin >> n, n) {
    map<pii, vp> g;
    map<pii, vi> type;
    map<pii, int> hf;
    REP(i, n) {
      int x, y;
      char d;
      cin >> x >> y >> d;
      int dir = (d == 'x' ? 2 : 3);
      pii c = make_pair(y, x);
      pii n = make_pair(y + dy[dir], x + dx[dir]);

      g[c] = vp();
      type[c] = vi();
      g[c].push_back(n);
      type[c].push_back(1);
      hf[c] = -1;

      g[n] = vp();
      type[n] = vi();
      g[n].push_back(c);
      type[n].push_back(1);
      hf[n] = -1;

      REP(j, 2) {
        REP(k, 4) {
          pii base = (j == 0 ? c : n);
          pii neighbor = make_pair(base.first + dy[k], base.second + dx[k]);
          if (EXIST(g, neighbor) &&
              find(ALL(g[base]), neighbor) == g[base].end()) {
            g[base].push_back(neighbor);
            type[base].push_back(0);
            g[neighbor].push_back(base);
            type[neighbor].push_back(0);
          }
        }
      }
    }

    bool ok = true;
    queue<pii> q;
    for (map<pii, vp>::iterator it = g.begin(); it != g.end(); it++) {
      if (hf[(*it).first] == -1) {
        q.push((*it).first);
        hf[(*it).first] = 0;
        while (!q.empty()) {
          pii cur = q.front();
          q.pop();
          int cur_hf = hf[cur];
          REP(i, g[cur].size()) {
            pii next = g[cur][i];
            int next_hf = (cur_hf + type[cur][i]) % 2;
            if (hf[next] == -1) {
              hf[next] = next_hf;
              q.push(next);
            } else if (hf[next] == next_hf) {
            } else {
              ok = false;
              goto end;
            }
          }
        }
      }
    }
  end:
    cout << (ok ? "Yes" : "No") << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

typedef vector<pii> vp;

bool dfs(map<pii, vp> &g, map<pii, vi> &type, map<pii, int> &hf, pii cur,
         int cur_hf) {
  hf[cur] = cur_hf;
  bool ret = true;
  REP(i, g[cur].size()) {
    pii next = g[cur][i];
    int next_hf = (cur_hf + type[cur][i]) % 2;
    if (hf[next] == -1) {
      ret &= dfs(g, type, hf, next, next_hf);
    } else if (hf[next] == next_hf) {
    } else {
      return false;
    }
  }
  return ret;
}

int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int main() {
  int n;
  while (cin >> n, n) {
    map<pii, vp> g;
    map<pii, vi> type;
    map<pii, int> hf;
    REP(i, n) {
      int x, y;
      char d;
      cin >> x >> y >> d;
      int dir = (d == 'x' ? 2 : 3);
      pii c = make_pair(y, x);
      pii n = make_pair(y + dy[dir], x + dx[dir]);

      g[c] = vp();
      type[c] = vi();
      g[c].push_back(n);
      type[c].push_back(1);
      hf[c] = -1;

      g[n] = vp();
      type[n] = vi();
      g[n].push_back(c);
      type[n].push_back(1);
      hf[n] = -1;

      REP(j, 2) {
        REP(k, 4) {
          pii base = (j == 0 ? c : n);
          pii neighbor = make_pair(base.first + dy[k], base.second + dx[k]);
          if (EXIST(g, neighbor) &&
              find(ALL(g[base]), neighbor) == g[base].end()) {
            g[base].push_back(neighbor);
            type[base].push_back(0);
            g[neighbor].push_back(base);
            type[neighbor].push_back(0);
          }
        }
      }
    }

    bool ok = true;
    for (map<pii, vp>::iterator it = g.begin(); it != g.end(); it++) {
      if (hf[(*it).first] == -1) {
        ok &= dfs(g, type, hf, (*it).first, 0);
      }
    }
    cout << (ok ? "Yes" : "No") << endl;
  }
}