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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ITER(c) __typeof((c).begin())
#define PB(e) push_back(e)
#define FOREACH(i, c) for (ITER(c) i = (c).begin(); i != (c).end(); ++i)
#define MP(a, b) make_pair(a, b)
#define PARITY(n) ((n)&1)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;

const int sz = 100010;
vector<int> G[sz];
vector<int> rG[sz];
int cmp[sz];
bool visit[sz];

void dfs(int v, vector<int> &vs) {
  visit[v] = true;
  REP(i, G[v].size()) if (!visit[G[v][i]]) dfs(G[v][i], vs);
  vs.push_back(v);
}

void rdfs(int v, int k) {
  visit[v] = true;
  cmp[v] = k;
  REP(i, rG[v].size()) if (!visit[rG[v][i]]) rdfs(rG[v][i], k);
}

int scc(int V) {
  int k = 0;
  vector<int> vs;

  memset(visit, false, sizeof(visit));
  REP(i, V) if (!visit[i]) dfs(i, vs);
  memset(visit, false, sizeof(visit));
  reverse(ALL(vs));
  REP(i, vs.size()) if (!visit[vs[i]]) rdfs(vs[i], k++);
  return k;
}

void clear() {
  REP(i, sz) {
    G[i].clear();
    rG[i].clear();
  }
}

void add_edge(int from, int to) {
  G[from].push_back(to);
  rG[to].push_back(from);
}

int main() {
  int n;
  while (cin >> n && n) {
    clear();
    char dir;
    vector<int> x(n * 2);
    vector<int> y(n * 2);
    vector<int> d(n * 2);

    REP(i, n) {
      cin >> x[i] >> y[i] >> dir;
      x[i + n] = x[i];
      y[i + n] = y[i];

      if (dir == 'x') {
        x[i + n]++;
        d[i] = 0;
        d[i + n] = 2;
      }

      if (dir == 'y') {
        y[i + n]++;
        d[i] = 1;
        d[i + n] = 3;
      }
    }

    n *= 2;
    map<P, int> futon;
    REP(i, n) futon[P(x[i], y[i])] = i;

    REP(i, n) {
      int dx[4] = {1, 0, -1, 0};
      int dy[4] = {0, 1, 0, -1};
      REP(j, 4) {
        int xa = x[i] + dx[j];
        int ya = y[i] + dy[j];

        if (futon.find(P(xa, ya)) != futon.end()) {
          int f = futon[P(xa, ya)];
          if (j == d[i]) {
            add_edge(i, f + n);
            add_edge(i + n, f);
            add_edge(f, i + n);
            add_edge(f + n, i);
          } else {
            add_edge(i + n, f + n);
            add_edge(f + n, i + n);
            add_edge(i, f);
            add_edge(f, i);
          }
        }
      }
    }

    bool possible = true;
    scc(2 * n);
    REP(i, n) { possible &= cmp[i] != cmp[n + i]; }
    cout << (possible ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define ITER(c) __typeof((c).begin())
#define PB(e) push_back(e)
#define FOREACH(i, c) for (ITER(c) i = (c).begin(); i != (c).end(); ++i)
#define MP(a, b) make_pair(a, b)
#define PARITY(n) ((n)&1)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;

const int sz = 40010;
vector<int> G[sz];
vector<int> rG[sz];
int cmp[sz];
bool visit[sz];

void dfs(int v, vector<int> &vs) {
  visit[v] = true;
  REP(i, G[v].size()) {
    if (!visit[G[v][i]])
      dfs(G[v][i], vs);
  }
  vs.push_back(v);
}

void rdfs(int v, int k) {
  visit[v] = true;
  cmp[v] = k;
  REP(i, rG[v].size()) if (!visit[rG[v][i]]) rdfs(rG[v][i], k);
}

int scc(int V) {
  REP(i, sz) rG[i].clear();
  REP(i, V) REP(j, G[i].size()) rG[G[i][j]].push_back(i);

  int k = 0;
  vector<int> vs;

  memset(visit, false, sizeof(visit));
  REP(i, V) if (!visit[i]) dfs(i, vs);
  memset(visit, false, sizeof(visit));
  reverse(ALL(vs));
  REP(i, vs.size()) {
    if (!visit[vs[i]]) {
      rdfs(vs[i], k++);
    }
  }
  return k;
}

int main() {
  int n;
  while (cin >> n && n) {

    REP(i, sz) G[i].clear();

    char dir;
    vector<int> x(n * 2);
    vector<int> y(n * 2);
    vector<int> d(n * 2);

    REP(i, n) {
      cin >> x[i] >> y[i] >> dir;
      x[i + n] = x[i];
      y[i + n] = y[i];
      if (dir == 'x') {
        x[i + n]++;
        d[i] = 0;
        d[i + n] = 2;
      }

      if (dir == 'y') {
        y[i + n]++;
        d[i] = 1;
        d[i + n] = 3;
      }
    }

    n *= 2;
    map<P, int> futon;
    REP(i, n) futon[P(x[i], y[i])] = i;

    REP(i, n) {
      int dx[4] = {1, 0, -1, 0};
      int dy[4] = {0, 1, 0, -1};
      REP(j, 4) {
        int xa = x[i] + dx[j];
        int ya = y[i] + dy[j];

        if (futon.find(P(xa, ya)) != futon.end()) {
          int f = futon[P(xa, ya)];

          if (j == d[i]) {
            G[i].push_back(f + n);
            G[f].push_back(i + n);
            G[i + n].push_back(f);
            G[f + n].push_back(i);
          } else {
            G[i + n].push_back(f + n);
            G[f + n].push_back(i + n);
          }
        }
      }
    }

    bool possible = true;
    scc(2 * n);
    REP(i, n) { possible &= cmp[i] != cmp[n + i]; }
    cout << (possible ? "Yes" : "No") << endl;
  }
  return 0;
}