Problem p03157 (C++)

Buggy submission (status: Runtime Error):

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;

struct UnionFind {
  vector<int> par; // 親を指すvector,-par[親]は木のサイズ
  UnionFind(int n) : par(n, -1) {} // uniteで親を埋め込んでいく必要あり
  int root(int x) {                // 親をたどる&データの整理
    if (par[x] < 0)
      return x;
    return par[x] = root(par[x]);
  }
  bool unite(int x, int y) { // データの結合
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y); } // 所属判定
  int size(int x) { return -par[root(x)]; }              // 木のサイズ
} uft(1600);

int main() {
  int h, w;
  cin >> h >> w;
  vs s(h);
  rep(i, h) { cin >> s[i]; }

  int di[4] = {0, 1, -1, 0};
  int dj[4] = {1, 0, 0, -1};

  ll ans = 0;
  rep(i, h) {
    rep(j, w) {
      int now = i * w + j;
      if (uft.size(now) != 1)
        continue;
      queue<int> que;
      que.push(now);
      int cou = 0;
      while (!que.empty()) {
        int nn = que.front();
        que.pop();
        if (s[nn / w][nn % w] == '.')
          cou++;
        rep(k, 4) {
          int ni = nn / w + di[k];
          int nj = nn % w + dj[k];
          if (ni < 0 or nj < 0 or ni > h - 1 or nj > w - 1)
            continue;
          if (s[ni][nj] == s[ni - di[k]][nj - dj[k]])
            continue;
          if (uft.same(now, ni * w + nj))
            continue;
          que.push(ni * w + nj);
          uft.unite(now, ni * w + nj);
        }
      }
      ll aa = uft.size(now);
      ans += (aa - cou) * cou;
    }
  }

  cout << ans << endl;
}

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;

struct UnionFind {
  vector<int> par; // 親を指すvector,-par[親]は木のサイズ
  UnionFind(int n) : par(n, -1) {} // uniteで親を埋め込んでいく必要あり
  int root(int x) {                // 親をたどる&データの整理
    if (par[x] < 0)
      return x;
    return par[x] = root(par[x]);
  }
  bool unite(int x, int y) { // データの結合
    x = root(x);
    y = root(y);
    if (x == y)
      return false;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
    return true;
  }
  bool same(int x, int y) { return root(x) == root(y); } // 所属判定
  int size(int x) { return -par[root(x)]; }              // 木のサイズ
} uft(160000);

int main() {
  int h, w;
  cin >> h >> w;
  vs s(h);
  rep(i, h) { cin >> s[i]; }

  int di[4] = {0, 1, -1, 0};
  int dj[4] = {1, 0, 0, -1};

  ll ans = 0;
  rep(i, h) {
    rep(j, w) {
      int now = i * w + j;
      if (uft.size(now) != 1)
        continue;
      queue<int> que;
      que.push(now);
      int cou = 0;
      while (!que.empty()) {
        int nn = que.front();
        que.pop();
        if (s[nn / w][nn % w] == '.')
          cou++;
        rep(k, 4) {
          int ni = nn / w + di[k];
          int nj = nn % w + dj[k];
          if (ni < 0 or nj < 0 or ni > h - 1 or nj > w - 1)
            continue;
          if (s[ni][nj] == s[ni - di[k]][nj - dj[k]])
            continue;
          if (uft.same(now, ni * w + nj))
            continue;
          que.push(ni * w + nj);
          uft.unite(now, ni * w + nj);
        }
      }
      ll aa = uft.size(now);
      ans += (aa - cou) * cou;
    }
  }

  cout << ans << endl;
}