Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 *	created: 22.08.2020 21:11:41
 **/
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using bint = boost::multiprecision::cpp_int;
using namespace std;
using ll = long long;
using P = pair<int, int>;
// #define endl '\\n'
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pcnt(bit) __builtin_popcountll(bit)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long double pi = acos(-1.0);
const int MAX = 1000010;
const int INF = 1ll << 60;
const int MOD = 1000000007;
// const int MOD = 998244353;

signed main() {

  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int h, w, m;
  cin >> h >> w >> m;
  map<int, int> mph;
  map<int, int> mpw;
  set<P> v;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    mph[a]++;
    mpw[b]++;
    v.insert(make_pair(a, b));
  }
  int hmax = -1, wmax = -1;
  for (auto p : mph)
    chmax(hmax, p.second);
  for (auto p : mpw)
    chmax(wmax, p.second);
  vector<int> hm, wm;
  for (auto p : mph)
    if (p.second == hmax)
      hm.push_back(p.first);
  for (auto p : mpw)
    if (p.second == wmax)
      wm.push_back(p.first);
  if (hm.size() * wm.size() > m)
    cout << hmax + wmax << endl;
  else {
    bool ok = false;
    rep(i, hm.size()) rep(j, wm.size()) {
      if (!v.count(make_pair(hm[i], wm[j])))
        ok = true;
    }
    if (ok)
      cout << hmax + wmax << endl;
    else
      cout << hmax + wmax - 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

/**
 *	created: 22.08.2020 21:11:41
 **/
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using bint = boost::multiprecision::cpp_int;
using namespace std;
using ll = long long;
using P = pair<int, int>;
// #define endl '\\n'
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pcnt(bit) __builtin_popcountll(bit)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const long double pi = acos(-1.0);
const int MAX = 1000010;
const int INF = 1ll << 60;
const int MOD = 1000000007;
// const int MOD = 998244353;

signed main() {

  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int h, w, m;
  cin >> h >> w >> m;
  map<int, int> mph;
  map<int, int> mpw;
  vector<P> v;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    mph[a]++;
    mpw[b]++;
    v.push_back(make_pair(a, b));
  }
  int hmax = -1, wmax = -1;
  for (auto p : mph)
    chmax(hmax, p.second);
  for (auto p : mpw)
    chmax(wmax, p.second);
  vector<int> hm, wm;
  for (auto p : mph)
    if (p.second == hmax)
      hm.push_back(p.first);
  for (auto p : mpw)
    if (p.second == wmax)
      wm.push_back(p.first);
  int ans = -1;
  rep(i, hm.size()) rep(j, wm.size()) {
    int cnt = 0;
    int hx = hm[i], wx = wm[j];
    for (auto p : v) {
      if (p.first == hx || p.second == wx)
        cnt++;
    }
    chmax(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}