Problem p00118 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// common
#define BR "\n"

#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<string> vs;
typedef long double ld;

// config
#define MODE_DEBUG
// #define INF 1<<30
// #define EPS 1e-8
// const ll MOD =100000007;

// debug
#ifdef MODE_DEBUG
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \
  DUMP(x) << " (L" << __LINE__ << ")"                                          \
          << " " << __FILE__ << endl
#define CHECK(exp, act)                                                        \
  if (exp != act) {                                                            \
    DUMP(exp);                                                                 \
    DEBUG(act);                                                                \
  }
#define STOP(e)                                                                \
  CHECK(e, true);                                                              \
  if (!(e))                                                                    \
    exit(1);
#else
#define DUMP(x)
#define DEBUG(x)
#define CHECK(exp, act)
#define STOP(e)
#endif

template <class T> inline string toString(const vector<T> &x) {
  stringstream ss;
  REP(i, x.size()) {
    if (i != 0)
      ss << " ";
    ss << x[i];
  }
  return ss.str();
}
template <class T> inline string toString(const vector<vector<T>> &map) {
  stringstream ss;
  REP(i, map.size()) {
    if (i != 0)
      ss << BR;
    ss << toString(map[i]);
  }
  return ss.str();
}
template <class K, class V> string toString(map<K, V> &x) {
  string res;
  stringstream ss;
  for (auto &p : x)
    ss << p.first << ":" << p.second << " ";
  return ss.str();
}

template <typename T, typename V> inline T mod(T v, V MOD) {
  return (v % MOD + MOD) % MOD;
}
struct UnionFind {
  vector<int> par;  // 親
  vector<int> rank; // 木の深さ
  int size;
  UnionFind(int n) {
    REP(i, n) par.push_back(i);
    rank = vector<int>(n);
    size = n;
  }
  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (rank[x] < rank[y])
      par[x] = y;
    else
      par[y] = x;
    if (rank[x] == rank[y])
      rank[x]++;
    size--;
  }
};

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
  while (true) {
    int H, W;
    cin >> H >> W;
    if (H == 0 && W == 0)
      return 0;
    vector<vector<int>> map(H, vector<int>(W));
    REP(y, H) {
      string str;
      cin >> str;
      REP(x, W) map[y][x] = str[x];
    }
    UnionFind uf(H * W);
    REP(y, H) REP(x, W) {
      REP(di, 4) {
        int px = x + dx[di], py = y + dy[di];
        if (IN(0, px, W) && IN(0, py, H)) {
          if (map[y][x] == map[py][px]) {
            uf.unite(y * H + x, py * H + px);
          }
        }
      }
    }
    cout << uf.size << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

// common
#define BR "\n"

#define ALL(c) (c).begin(), (c).end()
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define EACH(it, o) for (auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l, v, r) ((l) <= (v) && (v) < (r))

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vpii;
typedef long long ll;
typedef vector<long long> vl;
typedef pair<long long, long long> pll;
typedef vector<pair<long long, long long>> vpll;
typedef vector<string> vs;
typedef long double ld;

// config
#define MODE_DEBUG
// #define INF 1<<30
// #define EPS 1e-8
// const ll MOD =100000007;

// debug
#ifdef MODE_DEBUG
#define DUMP(x) cerr << #x << " = " << (x) << endl
#define DEBUG(x)                                                               \
  DUMP(x) << " (L" << __LINE__ << ")"                                          \
          << " " << __FILE__ << endl
#define CHECK(exp, act)                                                        \
  if (exp != act) {                                                            \
    DUMP(exp);                                                                 \
    DEBUG(act);                                                                \
  }
#define STOP(e)                                                                \
  CHECK(e, true);                                                              \
  if (!(e))                                                                    \
    exit(1);
#else
#define DUMP(x)
#define DEBUG(x)
#define CHECK(exp, act)
#define STOP(e)
#endif

template <class T> inline string toString(const vector<T> &x) {
  stringstream ss;
  REP(i, x.size()) {
    if (i != 0)
      ss << " ";
    ss << x[i];
  }
  return ss.str();
}
template <class T> inline string toString(const vector<vector<T>> &map) {
  stringstream ss;
  REP(i, map.size()) {
    if (i != 0)
      ss << BR;
    ss << toString(map[i]);
  }
  return ss.str();
}
template <class K, class V> string toString(map<K, V> &x) {
  string res;
  stringstream ss;
  for (auto &p : x)
    ss << p.first << ":" << p.second << " ";
  return ss.str();
}

template <typename T, typename V> inline T mod(T v, V MOD) {
  return (v % MOD + MOD) % MOD;
}
struct UnionFind {
  vector<int> par;  // 親
  vector<int> rank; // 木の深さ
  int size;
  UnionFind(int n) {
    REP(i, n) par.push_back(i);
    rank = vector<int>(n);
    size = n;
  }
  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }
  bool same(int x, int y) { return root(x) == root(y); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (rank[x] < rank[y])
      par[x] = y;
    else
      par[y] = x;
    if (rank[x] == rank[y])
      rank[x]++;
    size--;
  }
};

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int main() {
  while (true) {
    int H, W;
    cin >> H >> W;
    if (H == 0 && W == 0)
      return 0;
    vector<vector<int>> map(H, vector<int>(W));
    REP(y, H) {
      string str;
      cin >> str;
      REP(x, W) map[y][x] = str[x];
    }
    UnionFind uf(H * W);
    REP(y, H) REP(x, W) {
      REP(di, 4) {
        int px = x + dx[di], py = y + dy[di];
        if (IN(0, px, W) && IN(0, py, H)) {
          if (map[y][x] == map[py][px]) {
            uf.unite(y * W + x, py * W + px);
          }
        }
      }
    }
    cout << uf.size << endl;
  }
  return 0;
}