Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define deb(x) cerr << #x << ":" << x << "\n"
#define all(x) x.begin(), x.end()
#define sz(x) x.size()

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  unordered_map<int, unordered_set<int>> mpx;
  unordered_map<int, unordered_set<int>> mpy;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    mpx[x].insert(y);
    mpy[y].insert(x);
  }
  int mxx = -1, mxy = -1;
  for (auto P : mpx) {
    mxx = max(mxx, int(P.second.size()));
  }
  for (auto P : mpy) {
    mxy = max(mxy, int(P.second.size()));
  }
  // deb(mxx);deb(mxy);
  vector<int> a, b;
  for (auto P : mpx) {
    if (P.second.size() == mxx) {
      a.emplace_back(P.first);
    }
  }
  for (auto P : mpy) {
    if (P.second.size() == mxy)
      b.emplace_back(P.first);
  }
  int ans = 0;
  int cnt = 0;
  for (auto x : a) {
    for (auto y : b) {
      // cout<<x<<":"<<y<<"\n";
      ++cnt;
      ans = max(ans, int(mpx[x].size() + mpy[y].size() -
                         (mpx[x].count(y) || mpy[y].count(x))));
    }
    if (cnt == 1000000)
      break;
  }
  cout << ans << "\n";
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define deb(x) cerr << #x << ":" << x << "\n"
#define all(x) x.begin(), x.end()
#define sz(x) x.size()

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>

void solve() {
  int h, w, m;
  cin >> h >> w >> m;
  unordered_map<int, unordered_set<int>> mpx;
  unordered_map<int, unordered_set<int>> mpy;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    mpx[x].insert(y);
    mpy[y].insert(x);
  }
  int mxx = -1, mxy = -1;
  for (auto P : mpx) {
    mxx = max(mxx, int(P.second.size()));
  }
  for (auto P : mpy) {
    mxy = max(mxy, int(P.second.size()));
  }
  // deb(mxx);deb(mxy);
  vector<int> a, b;
  for (auto P : mpx) {
    if (P.second.size() == mxx) {
      a.emplace_back(P.first);
    }
  }
  for (auto P : mpy) {
    if (P.second.size() == mxy)
      b.emplace_back(P.first);
  }
  if (sz(a) * sz(b) > 300000) {
    int x = a[0], y = b[0];
    cout << int(mpx[x].size() + mpy[y].size()) << "\n";
    return;
  }
  int ans = 0;
  int cnt = 0;
  for (auto x : a) {
    for (auto y : b) {
      // cout<<x<<":"<<y<<"\n";
      ++cnt;
      ans = max(ans, int(mpx[x].size() + mpy[y].size() -
                         (mpx[x].count(y) || mpy[y].count(x))));
    }
    if (cnt == 1000000)
      break;
  }
  cout << ans << "\n";
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}