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

#include <bits/stdc++.h>

using namespace std;

using LL = long long;
const int INF = (int)1e9 + 7;
const int NS = (int)50 + 4;
int N, M;
char arr[NS][NS];
int chk[NS][NS];
vector<vector<int>> way;
vector<int> now;
vector<pair<int, int>> pos;
int augchk[NS * NS], l[NS * NS], r[NS * NS], chkN;

void dfs(int x, int y) {
  if (chk[x][y] || x < 1 || y < 1 || arr[x][y] == '#')
    return;
  chk[x][y] = 1;
  dfs(x - 1, y), dfs(x, y - 1);
}

int aug(int x) {
  for (auto &i : way[x]) {
    if (augchk[i] == chkN)
      continue;
    augchk[i] = chkN;
    if (r[i] == -1 || aug(r[i])) {
      r[i] = x;
      l[x] = i;
      return 1;
    }
  }
  return 0;
}

int main() {
  scanf("%d %d", &N, &M);
  //    N = M = 50;
  for (int i = 1; i <= N; ++i) {
    scanf("%s", arr[i] + 1);
    //        for(int j = 1; j <= M; ++j){
    //            arr[i][j] = (i <= 2 ? 'o' : '.');
    //        }
  }
  for (int I = N + M; I >= 2; --I) {
    int i = 1, j = I - i;
    while (j >= 1) {
      if (i <= N && j <= M && arr[i][j] != '#') {
        now.clear();
        memset(chk, 0, sizeof(chk));
        dfs(i, j);
        for (int k = N + M; k >= 2; --k) {
          int a = 1, b = k - 1;
          while (b >= 1) {
            if (a <= N && b <= M && chk[a][b] && arr[a][b] == 'o') {
              now.push_back((a - 1) * M + b);
            }
            ++a, --b;
          }
        }
        way.push_back(now);
        pos.push_back(make_pair(i, j));
      }
      ++i, --j;
    }
  }
  memset(r, -1, sizeof(r));
  for (int i = 0; i < (int)way.size(); ++i) {
    ++chkN;
    aug(i);
  }
  int ans = 0;
  for (int i = 0; i < (int)way.size(); ++i) {
    if (l[i]) {
      int x = (l[i] - 1) / M + 1, y = (l[i] - 1) % M + 1;
      //            printf("%d %d %d %d\\n", pos[i].first, pos[i].second, x, y);
      ans += -(x + y - pos[i].first - pos[i].second);
    }
  }
  printf("%d\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using LL = long long;
const int INF = (int)1e9 + 7;
const int NS = (int)50 + 4;
int N, M;
char arr[NS][NS];
int chk[NS][NS];
vector<vector<int>> way;
vector<int> now;
vector<pair<int, int>> pos;
int augchk[NS * NS], l[NS * NS], r[NS * NS], chkN;

void dfs(int x, int y) {
  if (chk[x][y] || x > N || y > M || arr[x][y] == '#')
    return;
  chk[x][y] = 1;
  dfs(x + 1, y), dfs(x, y + 1);
}

int aug(int x) {
  for (auto &i : way[x]) {
    if (augchk[i] == chkN)
      continue;
    augchk[i] = chkN;
    if (r[i] == -1 || aug(r[i])) {
      r[i] = x;
      l[x] = i;
      return 1;
    }
  }
  return 0;
}

int main() {
  scanf("%d %d", &N, &M);
  for (int i = 1; i <= N; ++i) {
    scanf("%s", arr[i] + 1);
  }
  for (int i = 1; i <= N; ++i) {
    for (int j = 1; j <= M; ++j) {
      if (arr[i][j] == 'o') {
        now.clear();
        memset(chk, 0, sizeof(chk));
        dfs(i, j);
        for (int k = N + M; k >= 2; --k) {
          int a = 1, b = k - 1;
          while (b >= 1) {
            if (chk[a][b]) {
              now.push_back((a - 1) * M + b);
            }
            ++a, --b;
          }
        }
        way.push_back(now);
        pos.push_back(make_pair(i, j));
      }
    }
  }
  memset(r, -1, sizeof(r));
  for (int i = 0; i < (int)way.size(); ++i) {
    ++chkN;
    aug(i);
  }
  int ans = 0;
  for (int i = 0; i < (int)way.size(); ++i) {
    int x = (l[i] - 1) / M + 1, y = (l[i] - 1) % M + 1;
    ans += x + y - pos[i].first - pos[i].second;
  }
  printf("%d\\n", ans);
  return 0;
}