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

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

struct before_main {
  before_main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
} before_main;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \\
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int const OppoSide = 0;
int const SameSide = 1;

int N;
vector<pair<int, int>> G[44444];
map<pair<int, int>, int> MP;
map<int, pair<int, int>> CD;
int vis[44444];

bool dfs(int curr, int col) {
  for (auto &e : G[curr]) {
    int next, dir;
    tie(next, dir) = e;
    int nextCol = dir == OppoSide ? col ^ 1 : col;
    if (vis[next] + 1) {
      if (vis[next] != nextCol)
        return false;
    } else {
      vis[next] = nextCol;
      if (!dfs(next, nextCol))
        return false;
    }
  }
  return true;
}

int main() {

  for (int M; cin >> M && M;) {
    vector<int> xs, ys;
    vector<int> X, Y;
    vector<char> D;
    rep(i, M) {
      int x, y;
      char d;
      cin >> x >> y >> d;
      X.push_back(x), Y.push_back(y), D.push_back(d);

      xs.push_back(x - 1), ys.push_back(y - 1);
      xs.push_back(x), ys.push_back(y);
      xs.push_back(x + 1), ys.push_back(y + 1);

      if (d == 'x') {
        xs.push_back(x + 2);
      } else {
        ys.push_back(y + 2);
      }
    }

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

    vector<int> NX, NY;
    rep(i, X.size()) { NX.push_back(lower_bound(all(xs), X[i]) - X.begin()); }
    rep(i, Y.size()) { NY.push_back(lower_bound(all(ys), Y[i]) - Y.begin()); }
    X = NX, Y = NY;

    MP.clear(), CD.clear();
    rep(i, 44444) G[i].clear();
    N = 0;

    rep(i, X.size()) {
      MP[make_pair(Y[i], X[i])] = N;
      CD[N++] = make_pair(Y[i], X[i]);
      if (D[i] == 'x') {
        MP[make_pair(Y[i], X[i] + 1)] = N;
        CD[N] = make_pair(Y[i], X[i] + 1);
        G[N - 1].emplace_back(N, OppoSide), G[N].emplace_back(N - 1, OppoSide);
        N++;
      } else {
        MP[make_pair(Y[i] + 1, X[i])] = N;
        CD[N] = make_pair(Y[i] + 1, X[i]);
        G[N - 1].emplace_back(N, OppoSide), G[N].emplace_back(N - 1, OppoSide);
        N++;
      }
    }

    rep(i, N) {
      auto curr = CD[i];
      rep(k, 4) {
        auto next = make_pair(CD[i].first + dy[k], CD[i].second + dx[k]);
        if (MP.find(next) == MP.end())
          continue; // テ・ツクツε・ツ崢」テ」ツ??」ツ?妥ィツェツソテ」ツ?ケテ」ツつ古」ツ?ーOK
        if (find(all(G[i]), make_pair(MP[next], OppoSide)) != G[i].end())
          continue; // テ・ツ按・テ」ツ?ョテ・ツクツε・ツ崢」テ」ツ?ッSameSideテ」ツ?療」ツ?凝・ツ?・テ」ツつ古」ツつ暗」ツ??」ツ?ィテ」ツ?療」ツ?ェテ」ツ??」ツ?凝」ツつ碓K
        G[i].emplace_back(MP[next], SameSide);
      }
    }

    minus(vis);

    bool ok = 1;

    rep(i, N) {
      if (vis[i] < 0) {
        vis[i] = 0;
        if (!dfs(i, 0)) {
          ok = 0;
          break;
        }
      }
    }

    cout << (ok ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

struct before_main {
  before_main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
} before_main;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \\
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

struct union_find {
  int H, W;
  vector<int> rank_, size_, rid_;
  union_find() {}
  union_find(int h, int w) {
    H = h + 10, W = w + 10;
    rank_.resize(H * W);
    rid_.assign(H * W, -1);
    size_.resize(H * W, 1);
  }
  void operator()(pair<int, int> x, pair<int, int> y) {
    int u = x.first * W + x.second;
    int v = y.first * W + y.second;
    u = operator[](u), v = operator[](v);
    if (u == v) {
      return;
    }
    size_[u] = size_[v] = size_[u] + size_[v];
    if (rank_[u] < rank_[v]) {
      rid_[u] = v;
    } else {
      rid_[v] = u;
      if (rank_[u] == rank_[v]) {
        rank_[u]++;
      }
    }
  }
  int operator[](pair<int, int> a) {
    int x = a.first * W + a.second;
    return operator[](operator[](x));
  }
  int size_of(int x) { return size_[x]; }

private:
  int operator[](int x) {
    if (rid_[x] < 0)
      return x;
    else
      return rid_[x] = operator[](rid_[x]);
  }
};

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

union_find uf;
vector<int> fx, fy;
vector<char> fd;
vector<int> xs, ys;
map<int, int> X, Y;
set<pair<int, int>> st;

int main() {

  for (int N; cin >> N && N;) {
    fx.clear(), fx.resize(N);
    fy.clear(), fy.resize(N);
    fd.clear(), fd.resize(N);
    xs.clear(), xs.resize(N);
    ys.clear(), ys.resize(N);
    rep(i, N) {
      cin >> fx[i] >> fy[i] >> fd[i];
      REP(d, -1, 2) xs.push_back(fx[i] + d), ys.push_back(fy[i] + d);
    }

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

    const int H = ys.size();
    const int W = xs.size();

    X.clear(), Y.clear();
    rep(i, xs.size()) X[xs[i]] = i;
    rep(i, ys.size()) Y[ys[i]] = i;

    uf = union_find(H, W);

    st.clear();

    rep(i, N) {
      fx[i] = X[fx[i]];
      fy[i] = Y[fy[i]];
    }

    rep(i, N) {
      st.emplace(fy[i], fx[i]);
      if (fd[i] == 'x') {
        st.emplace(fy[i], fx[i] + 1);
      } else {
        st.emplace(fy[i] + 1, fx[i]);
      }
    }

    rep(i, N) {
      if (fd[i] == 'x') {
        auto po = make_pair(fy[i], fx[i]);
        rep(k, 4) if (dx[k] != +1) {
          auto np = make_pair(fy[i] + dy[k], fx[i] + dx[k]);
          if (st.count(np))
            uf(po, np);
        }
        po = make_pair(fy[i], fx[i] + 1);
        rep(k, 4) if (dx[k] != -1) {
          auto np = make_pair(fy[i] + dy[k], fx[i] + 1 + dx[k]);
          if (st.count(np))
            uf(po, np);
        }
      } else {
        auto po = make_pair(fy[i], fx[i]);
        rep(k, 4) if (dy[k] != +1) {
          auto np = make_pair(fy[i] + dy[k], fx[i] + dx[k]);
          if (st.count(np))
            uf(po, np);
        }

        po = make_pair(fy[i] + 1, fx[i]);
        rep(k, 4) if (dy[k] != -1) {
          auto np = make_pair(fy[i] + 1 + dy[k], fx[i] + dx[k]);
          if (st.count(np))
            uf(po, np);
        }
      }
    }

    bool ok = 1;
    rep(i, N) {
      auto a = make_pair(fy[i], fx[i]);
      if (fd[i] == 'x') {
        auto b = make_pair(fy[i], fx[i] + 1);
        ok &= uf[a] != uf[b];
      } else {
        auto b = make_pair(fy[i] + 1, fx[i]);
        ok &= uf[a] != uf[b];
      }
    }

    cout << (ok ? "Yes" : "No") << endl;
  }

  return 0;
}