Problem p01139 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
// #define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int, int> P;

int w, h;
char c[51][51];
int dy[4] = {1, -1, 0, 0};
int dx[4] = {0, 0, 1, -1};
bool contain(int y, int x) { return (0 <= y && y < h && 0 <= x && x < w); }

signed main() {
  while (1) {
    cin >> w >> h;
    if (w + h == 0)
      break;
    int wsum = 0, bsum = 0;
    rep(i, 0, h) {
      rep(j, 0, w) {
        cin >> c[i][j];
        if (c[i][j] == 'W')
          wsum++;
        if (c[i][j] == 'B')
          bsum++;
      }
    }
    int data[51][51] = {};
    rep(i, 0, h) {
      rep(j, 0, w) {
        if (c[i][j] != '.') {
          int id = (c[i][j] == 'W' ? 1 : 2);
          string str = "?BW";
          if (data[i][j] & id)
            continue;
          queue<P> q;
          q.push(P(i, j));
          data[i][j] |= id;
          while (!q.empty()) {
            P p = q.front();
            q.pop();
            int y = p.first;
            int x = p.second;
            rep(k, 0, 4) {
              int ny = y + dy[k];
              int nx = x + dx[k];
              if (!contain(ny, nx))
                continue;
              if (c[ny][nx] == str[id])
                continue;
              if (data[ny][nx] & id)
                continue;
              data[ny][nx] |= id;
              q.push(P(ny, nx));
            }
          }
        }
      }
    }
    int wc = 0, bc = 0;
    rep(i, 0, h) {
      rep(j, 0, w) {
        if (data[i][j] == 1)
          wc++;
        if (data[i][j] == 2)
          bc++;
        // cout << data[i][j];
      }
      // cout << endl;
    }
    cout << bc - bsum << " " << wc - wsum << endl;
  }
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef pair<int, int> P;

int w, h;
char c[51][51];
int dy[4] = {1, -1, 0, 0};
int dx[4] = {0, 0, 1, -1};
bool contain(int y, int x) { return (0 <= y && y < h && 0 <= x && x < w); }

signed main() {
  while (1) {
    cin >> w >> h;
    if (w + h == 0)
      break;
    int wsum = 0, bsum = 0;
    rep(i, 0, h) {
      rep(j, 0, w) {
        cin >> c[i][j];
        if (c[i][j] == 'W')
          wsum++;
        if (c[i][j] == 'B')
          bsum++;
      }
    }
    int data[51][51] = {};
    rep(i, 0, h) {
      rep(j, 0, w) {
        if (c[i][j] != '.') {
          int id = (c[i][j] == 'W' ? 1 : 2);
          int op = id * 2 % 3;
          string str = "?BW";
          if (data[i][j] & id)
            continue;
          queue<P> q;
          q.push(P(i, j));
          while (!q.empty()) {
            P p = q.front();
            q.pop();
            int y = p.first;
            int x = p.second;
            data[y][x] |= id;
            rep(k, 0, 4) {
              int ny = y + dy[k];
              int nx = x + dx[k];
              if (!contain(ny, nx))
                continue;
              if (c[ny][nx] == str[id])
                continue;
              if (data[ny][nx] & id)
                continue;
              q.push(P(ny, nx));
            }
          }
        }
      }
    }
    int wc = 0, bc = 0;
    rep(i, 0, h) {
      rep(j, 0, w) {
        if (data[i][j] == 1)
          wc++;
        if (data[i][j] == 2)
          bc++;
        // cout << data[i][j];
      }
      // cout << endl;
    }
    cout << bc - bsum << " " << wc - wsum << endl;
  }
}