Problem p02580 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() { cout << endl; }
template <typename First, typename... Rest>
void print(const First &first, const Rest &...rest) {
  cout << first << ' ';
  print(rest...);
}

template <typename T> void print(const vector<T> &v) {
  for (auto e : v)
    cout << e << ' ';
  cout << endl;
}

//------------------------- Libraries -------------------------//

//--------------------------- Solve ---------------------------//

void solve() {
  int H, W, M;
  cin >> H >> W >> M;
  set<pair<int, int>> items;
  vector<long long> sum_h(W), sum_w(H);
  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    sum_h[w]++;
    sum_w[h]++;
    items.insert(make_pair(h, w));
  }

  set<int> ans_w, ans_h;
  long long max_w = 0, max_h = 0;
  for (int w = 0; w < W; w++) {
    if (max_h < sum_h[w]) {
      max_h = max(max_h, sum_h[w]);
      ans_w.clear();
      ans_w.insert(w);
    } else if (max_h == sum_h[w]) {
      ans_w.insert(w);
    }
  }

  for (int h = 0; h < W; h++) {
    if (max_w < sum_w[h]) {
      max_w = max(max_w, sum_w[h]);
      ans_h.clear();
      ans_h.insert(h);
    } else if (max_w == sum_w[h]) {
      ans_h.insert(h);
    }
  }

  long long siz = (long long)ans_w.size() * (long long)ans_h.size();
  if (siz > M) {
    cout << max_w + max_h << '\n';
  } else {
    for (int w : ans_w) {
      for (int h : ans_h) {
        if (items.find(make_pair(h, w)) == items.end()) {
          cout << max_w + max_h << '\n';
          return;
        }
      }
    }
    cout << max_w + max_h - 1 << '\n';
  }
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  solve();

  return 0;
}

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

using namespace std;

//----------------------- Print Function ----------------------//

inline void print() { cout << endl; }
template <typename First, typename... Rest>
void print(const First &first, const Rest &...rest) {
  cout << first << ' ';
  print(rest...);
}

template <typename T> void print(const vector<T> &v) {
  for (auto e : v)
    cout << e << ' ';
  cout << endl;
}

//------------------------- Libraries -------------------------//

//--------------------------- Solve ---------------------------//

void solve() {
  int H, W, M;
  cin >> H >> W >> M;
  set<pair<int, int>> items;
  vector<long long> sum_h(W), sum_w(H);
  for (int i = 0; i < M; i++) {
    int h, w;
    cin >> h >> w;
    h--;
    w--;
    sum_h[w]++;
    sum_w[h]++;
    items.insert(make_pair(h, w));
  }

  set<int> ans_w, ans_h;
  long long max_w = 0, max_h = 0;
  for (int w = 0; w < W; w++) {
    if (max_h < sum_h[w]) {
      max_h = max(max_h, sum_h[w]);
      ans_w.clear();
      ans_w.insert(w);
    } else if (max_h == sum_h[w]) {
      ans_w.insert(w);
    }
  }

  for (int h = 0; h < H; h++) {
    if (max_w < sum_w[h]) {
      max_w = max(max_w, sum_w[h]);
      ans_h.clear();
      ans_h.insert(h);
    } else if (max_w == sum_w[h]) {
      ans_h.insert(h);
    }
  }

  long long siz = (long long)ans_w.size() * (long long)ans_h.size();
  if (siz > M) {
    cout << max_w + max_h << '\n';
  } else {
    for (int w : ans_w) {
      for (int h : ans_h) {
        if (items.find(make_pair(h, w)) == items.end()) {
          cout << max_w + max_h << '\n';
          return;
        }
      }
    }
    cout << max_w + max_h - 1 << '\n';
  }
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  solve();

  return 0;
}