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

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define VI vector<int>
#define VVI vector<vector<int>>
#define UM unordered_map
#define MP map
#define ST set
#define US unordered_set
#define V vector
#define S string
#define D deque
#define L list
#define Q queue
#define PQ priority_queue
#define D deque
#define PII pair<int, int>
#define P pair
#define DB double
#define LDB long double
#define FOR(i, n) for (int i = 0; i < (n); ++i)
#define FFOR(i, n) for (int i = 0; i <= (n); ++i)
#define RFOR(i, n) for (int i = (n)-1; i >= 0; --i)
#define all(grid) (grid).begin(), (grid).end()
#define rall(grid) (grid).rbegin(), (grid).rend()

const int dr[] = {0, 1, 0, -1};
const int dc[] = {1, 0, -1, 0};

template <typename T> void read(T &a) {
  a = 0;
  char c;
  while ((c = cin.peek()) < 48 or c > 57)
    cin.get();
  while ((c = cin.get()) > 47 and c < 58)
    a = (a << 3) + (a << 1) + c - 48;
}

template <typename T, typename... Args> void read(T &a, Args &&...args) {
  read(a);
  read(forward<Args>(args...)...);
}

template <typename T> void write(const S &sep, const S &end, T n) {
  char c[128] = {0};
  int i = 0;
  for (; n; ++i, n /= 10)
    c[i] = n % 10;

  for (--i; i >= 0; --i)
    cout.put(c[i] + 48);

  cout.write(end.c_str(), end.size() * sizeof(char));
}

template <typename T, typename... Args>
void write(const S &sep, const S &end, T n, Args &&...args) {
  char c[128] = {0};
  int i = 0;
  for (; n; ++i, n /= 10)
    c[i] = n % 10;

  for (--i; i >= 0; --i)
    cout.put(c[i] + 48);

  cout.write(sep.c_str(), sep.size() * sizeof(char));
  write(sep, end, forward<Args>(args...)...);
}

void solve() {
  int n, m, q;
  cin >> n >> m >> q;
  V<PII> a(n), b(m);
  ST<PII> s;
  FOR(i, n) a[i].second = i;
  FOR(i, m) b[i].second = i;
  FOR(i, q) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    ++a[x].first, ++b[y].first;
    s.emplace(x, y);
  }
  sort(all(a)), sort(all(b));
  int a_max = a.back().first, b_max = b.back().first;
  for (int i = n - 1; i >= 0 and a[i].first == a_max; --i) {
    for (int j = m - 1; j >= 0 and b[j].first == b_max; --j) {
      // cout << i << " " << j << endl;
      if (!s.count({a[i].second, b[j].second})) {
        cout << a_max + b_max;
        return;
      }
    }
  }
  cout << a_max + b_max - 1;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define VI vector<int>
#define VVI vector<vector<int>>
#define UM unordered_map
#define MP map
#define ST set
#define US unordered_set
#define V vector
#define S string
#define D deque
#define L list
#define Q queue
#define PQ priority_queue
#define D deque
#define PII pair<int, int>
#define P pair
#define DB double
#define LDB long double
#define FOR(i, n) for (int i = 0; i < (n); ++i)
#define FFOR(i, n) for (int i = 0; i <= (n); ++i)
#define RFOR(i, n) for (int i = (n)-1; i >= 0; --i)
#define all(grid) (grid).begin(), (grid).end()
#define rall(grid) (grid).rbegin(), (grid).rend()

const int dr[] = {0, 1, 0, -1};
const int dc[] = {1, 0, -1, 0};

template <typename T> void read(T &a) {
  a = 0;
  char c;
  while ((c = cin.peek()) < 48 or c > 57)
    cin.get();
  while ((c = cin.get()) > 47 and c < 58)
    a = (a << 3) + (a << 1) + c - 48;
}

template <typename T, typename... Args> void read(T &a, Args &&...args) {
  read(a);
  read(forward<Args>(args...)...);
}

template <typename T> void write(const S &sep, const S &end, T n) {
  char c[128] = {0};
  int i = 0;
  for (; n; ++i, n /= 10)
    c[i] = n % 10;

  for (--i; i >= 0; --i)
    cout.put(c[i] + 48);

  cout.write(end.c_str(), end.size() * sizeof(char));
}

template <typename T, typename... Args>
void write(const S &sep, const S &end, T n, Args &&...args) {
  char c[128] = {0};
  int i = 0;
  for (; n; ++i, n /= 10)
    c[i] = n % 10;

  for (--i; i >= 0; --i)
    cout.put(c[i] + 48);

  cout.write(sep.c_str(), sep.size() * sizeof(char));
  write(sep, end, forward<Args>(args...)...);
}

void solve() {
  int n, m, q;
  cin >> n >> m >> q;
  VI a(n), b(m);
  MP<int, int> v[100005];
  FOR(i, q) {
    int x, y;
    cin >> x >> y;
    --x, --y;
    ++a[x], ++b[y];
    v[x][y] = 1;
  }
  auto it1 = max_element(all(a)), it2 = max_element(all(b));
  int pos1 = distance(begin(a), it1), pos2 = distance(begin(b), it2);
  if (v[it1 - begin(a)][it2 - begin(b)]) {
    cout << *it1 + *it2 - 1;
  } else {
    cout << *it1 + *it2;
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}