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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define IFOR(i, n, a) for (int i = n; i >= a; i--)
#define mp make_pair
#define full(a) a.begin(), a.end()
#define sz(x) ((int)(x).size())
#define pii pair<int, int>
#define trav(a, x) for (auto &a : x)
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
using namespace std;
const ll mod = 1e9 + 7;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int h, w, m;
  cin >> h >> w >> m;
  set<pii> target;
  vector<int> a(h + 1), b(w + 1);
  FOR(i, 0, m) {
    int x, y;
    cin >> x >> y;
    target.emplace(x, y);
    a[x]++;
    b[y]++;
  }
  int mx = 0;
  trav(i, a) if (i > mx) mx = i;
  vector<int> best_rows, best_cols;
  FOR(i, 1, h + 1) if (a[i] == mx) best_rows.push_back(i);
  mx = 0;
  trav(i, b) if (i > mx) mx = i;
  FOR(i, 1, w + 1) if (b[i] == mx) best_cols.push_back(i);
  int res = a[best_rows.front()] + b[best_cols.front()];
  FOR(i, 0, sz(best_rows)) {
    FOR(j, 0, sz(best_cols)) {
      if (target.find(mp(best_rows[i], best_cols[j])) == target.end()) {
        cout << res;
        return 0;
      }
    }
  }
  cout << res - 1;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define IFOR(i, n, a) for (int i = n; i >= a; i--)
#define mp make_pair
#define full(a) a.begin(), a.end()
#define sz(x) ((int)(x).size())
#define pii pair<int, int>
#define trav(a, x) for (auto &a : x)
#define exist(st, val) st.find(val) != st.end()
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
using namespace std;
const ll mod = 1e9 + 7;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int h, w, m;
  cin >> h >> w >> m;
  vector<vector<bool>> target(h + 1, vector<bool>(w + 1));
  vector<int> a(h + 1), b(w + 1);
  FOR(i, 0, m) {
    int x, y;
    cin >> x >> y;
    target[x][y] = 1;
    a[x]++;
    b[y]++;
  }
  int mx = 0;
  trav(i, a) if (i > mx) mx = i;
  vector<int> best_rows, best_cols;
  FOR(i, 1, h + 1) if (a[i] == mx) best_rows.push_back(i);
  mx = 0;
  trav(i, b) if (i > mx) mx = i;
  FOR(i, 1, w + 1) if (b[i] == mx) best_cols.push_back(i);
  int res = a[best_rows.front()] + b[best_cols.front()];
  FOR(i, 0, sz(best_rows)) {
    FOR(j, 0, sz(best_cols)) {
      if (!target[best_rows[i]][best_cols[j]]) {
        cout << res;
        return 0;
      }
    }
  }
  cout << res - 1;
  return 0;
}