Problem p03157 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define fast_cin()                                                             \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define MAX 1000000
#define ll long long
#define db double
#define pb push_back
#define For(i, s, e) for (ll i = (s); i < (e); i++)
#define Debug_array(a, n)                                                      \
  for (ll i = (0); i < (n); i++)                                               \
  cout << a[i] << " "
#define Foe(i, s, e) for (ll i = (s); i <= (e); i++)
#define Fod(i, s, e) for (ll i = (s)-1; i >= (e); i--)

#define vl vector<ll>
#define vs vector<string>

#define mapll map<ll, ll>
#define mapss map<string, string>
#define mapsl map<string, ll>
#define mapls map<ll, string>

#define pll pair<ll, ll>
#define pss pair<string, string>
#define psl pair<ll, string>
#define pls pair<string, ll>

#define minheap priority_queue<ll>
#define maxheap priority_queue<ll, vector<ll>, greater<ll>>

#define fi first
#define se second
#define endl "\n"
#define mp make_pair
#define big_prime 15486277
#define bigger_prime 179424697
#define biggest_prime 32416188691
const ll mod = 1e9 + 7;

ll takemod(ll a) { return ((a % mod) + mod) % mod; }

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll fast_exp(ll base, ll expo) {
  ll res = 1;
  while (expo > 0) {
    if (expo & 1)
      res = (res * base) % mod;
    base = (base * base) % mod;
    expo >>= 1;
  }
  return res;
}

ll modinv(ll a) { return takemod(fast_exp(takemod(a), mod - 2)); }

ll arr1[400][400];
ll visit[400][400];
ll ans, n, m;
ll val1, val2;
vector<pll> v1;

void dfs(vector<pll> v1, ll index, ll count) {
  if (index >= v1.size()) {
    return;
  }
  ll x1 = v1[index].fi;
  ll y1 = v1[index].se;
  if (x1 > 0 and arr1[x1][y1] != arr1[x1 - 1][y1] and visit[x1 - 1][y1] == 0) {
    if (arr1[x1 - 1][y1] == 0)
      ++val2;
    else
      ++val1;
    visit[x1 - 1][y1] = count;
    v1.pb(mp(x1 - 1, y1));
  }
  if (y1 > 0 and arr1[x1][y1] != arr1[x1][y1 - 1] and visit[x1][y1 - 1] == 0) {
    if (arr1[x1][y1 - 1] == 0)
      ++val2;
    else
      ++val1;
    visit[x1][y1 - 1] = count;
    v1.pb(mp(x1, y1 - 1));
  }
  if (x1 + 1 < n and arr1[x1][y1] != arr1[x1 + 1][y1] and
      visit[x1 + 1][y1] == 0) {
    if (arr1[x1 + 1][y1] == 0)
      ++val2;
    else
      ++val1;
    visit[x1 + 1][y1] = count;
    v1.pb(mp(x1 + 1, y1));
  }
  if (y1 + 1 < m and arr1[x1][y1] != arr1[x1][y1 + 1] and
      visit[x1][y1 + 1] == 0) {
    if (arr1[x1][y1 + 1] == 0)
      ++val2;
    else
      ++val1;
    visit[x1][y1 + 1] = count;
    v1.pb(mp(x1, y1 + 1));
  }
  dfs(v1, index + 1, count);
  return;
}

signed main() {
  fast_cin();

  cin >> n >> m;
  ans = 0;
  ll count = 1;
  For(i, 0, n) {
    string s1;
    cin >> s1;
    For(j, 0, m) {
      if (s1[j] == '.')
        arr1[i][j] = 0;
      else
        arr1[i][j] = 1;
      visit[i][j] = 0;
    }
  }
  For(i, 0, n) {
    For(j, 0, m) {
      if (visit[i][j] == 0) {
        val1 = 0;
        val2 = 0;
        v1.clear();
        v1.push_back(mp(i, j));
        visit[i][j] = count;
        if (arr1[i][j] == 1)
          ++val1;
        else
          ++val2;
        dfs(v1, 0, count);
        ++count;
        ans += (val1 * val2);
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define fast_cin()                                                             \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define MAX 1000000
#define ll long long
#define db double
#define pb push_back
#define For(i, s, e) for (ll i = (s); i < (e); i++)
#define Debug_array(a, n)                                                      \
  for (ll i = (0); i < (n); i++)                                               \
  cout << a[i] << " "
#define Foe(i, s, e) for (ll i = (s); i <= (e); i++)
#define Fod(i, s, e) for (ll i = (s)-1; i >= (e); i--)

#define vl vector<ll>
#define vs vector<string>

#define mapll map<ll, ll>
#define mapss map<string, string>
#define mapsl map<string, ll>
#define mapls map<ll, string>

#define pll pair<ll, ll>
#define pss pair<string, string>
#define psl pair<ll, string>
#define pls pair<string, ll>

#define minheap priority_queue<ll>
#define maxheap priority_queue<ll, vector<ll>, greater<ll>>

#define fi first
#define se second
#define endl "\n"
#define mp make_pair
#define big_prime 15486277
#define bigger_prime 179424697
#define biggest_prime 32416188691
const ll mod = 1e9 + 7;

ll takemod(ll a) { return ((a % mod) + mod) % mod; }

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

ll fast_exp(ll base, ll expo) {
  ll res = 1;
  while (expo > 0) {
    if (expo & 1)
      res = (res * base) % mod;
    base = (base * base) % mod;
    expo >>= 1;
  }
  return res;
}

ll modinv(ll a) { return takemod(fast_exp(takemod(a), mod - 2)); }

ll arr1[400][400];
ll visit[400][400];
ll ans, n, m;
ll val1, val2;
vector<pll> v1;

void dfs(vector<pll> &v1, ll index, ll count) {
  if (index >= v1.size()) {
    return;
  }
  ll x1 = v1[index].fi;
  ll y1 = v1[index].se;
  if (x1 > 0 and arr1[x1][y1] != arr1[x1 - 1][y1] and visit[x1 - 1][y1] == 0) {
    if (arr1[x1 - 1][y1] == 0)
      ++val2;
    else
      ++val1;
    visit[x1 - 1][y1] = count;
    v1.pb(mp(x1 - 1, y1));
  }
  if (y1 > 0 and arr1[x1][y1] != arr1[x1][y1 - 1] and visit[x1][y1 - 1] == 0) {
    if (arr1[x1][y1 - 1] == 0)
      ++val2;
    else
      ++val1;
    visit[x1][y1 - 1] = count;
    v1.pb(mp(x1, y1 - 1));
  }
  if (x1 + 1 < n and arr1[x1][y1] != arr1[x1 + 1][y1] and
      visit[x1 + 1][y1] == 0) {
    if (arr1[x1 + 1][y1] == 0)
      ++val2;
    else
      ++val1;
    visit[x1 + 1][y1] = count;
    v1.pb(mp(x1 + 1, y1));
  }
  if (y1 + 1 < m and arr1[x1][y1] != arr1[x1][y1 + 1] and
      visit[x1][y1 + 1] == 0) {
    if (arr1[x1][y1 + 1] == 0)
      ++val2;
    else
      ++val1;
    visit[x1][y1 + 1] = count;
    v1.pb(mp(x1, y1 + 1));
  }
  dfs(v1, index + 1, count);
  return;
}

signed main() {
  fast_cin();

  cin >> n >> m;
  ans = 0;
  ll count = 1;
  For(i, 0, n) {
    string s1;
    cin >> s1;
    For(j, 0, m) {
      if (s1[j] == '.')
        arr1[i][j] = 0;
      else
        arr1[i][j] = 1;
      visit[i][j] = 0;
    }
  }
  For(i, 0, n) {
    For(j, 0, m) {
      if (visit[i][j] == 0) {
        val1 = 0;
        val2 = 0;
        v1.clear();
        v1.push_back(mp(i, j));
        visit[i][j] = count;
        if (arr1[i][j] == 1)
          ++val1;
        else
          ++val2;
        dfs(v1, 0, count);
        ++count;
        ans += (val1 * val2);
      }
    }
  }
  cout << ans << endl;
  return 0;
}