Problem p02580 (C++)

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

typedef int_fast32_t int32;
typedef int_fast64_t int64;

const int32 inf = 1e9 + 7;
const int32 MOD = 1000000007;
const int64 llinf = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n")
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n")
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n")
#define ANS(n) cout << (n) << "\n"
#define REP(i, n) for (int64 i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define FORR(i, a, b) for (int64 i = (a); i >= (b); i--)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
#define fi first
#define se second
#define pb(a) push_back(a)
typedef pair<int32, int32> pii;
typedef pair<int64, int64> pll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 h, w, m;
  cin >> h >> w >> m;
  vector<int32> hcnt(h), wcnt(w);
  set<pii> obj;
  REP(i, m) {
    int32 hi, wi;
    cin >> hi >> wi;
    hi--;
    wi--;
    hcnt[hi]++;
    wcnt[wi]++;
    obj.insert(pii(hi, wi));
  }
  int32 hmx = 0, wmx = 0;
  REP(i, h) { chmax(hmx, hcnt[i]); }
  REP(i, w) { chmax(wmx, wcnt[i]); }
  vector<int32> hmxi, wmxj;
  REP(i, h) {
    if (hcnt[i] == hmx)
      hmxi.pb(i);
  }
  REP(j, w) {
    if (wcnt[j] == wmx)
      wmxj.pb(j);
  }
  bool allobj = true;
  for (auto i : hmxi)
    for (auto j : wmxj) {
      if (obj.count(pii(i, j)) == 0)
        allobj = false;
    }
  if (allobj) {
    ANS(hmx + wmx - 1);
  } else {
    ANS(hmx + wmx);
  }
  return 0;
}

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

typedef int_fast32_t int32;
typedef int_fast64_t int64;

const int32 inf = 1e9 + 7;
const int32 MOD = 1000000007;
const int64 llinf = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n")
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n")
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE\n" : "IMPOSSIBLE\n")
#define ANS(n) cout << (n) << "\n"
#define REP(i, n) for (int64 i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int64 i = (a); i < (b); i++)
#define FORR(i, a, b) for (int64 i = (a); i >= (b); i--)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
#define fi first
#define se second
#define pb(a) push_back(a)
typedef pair<int32, int32> pii;
typedef pair<int64, int64> pll;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int32 h, w, m;
  cin >> h >> w >> m;
  vector<int32> hcnt(h), wcnt(w);
  set<pii> obj;
  REP(i, m) {
    int32 hi, wi;
    cin >> hi >> wi;
    hi--;
    wi--;
    hcnt[hi]++;
    wcnt[wi]++;
    obj.insert(pii(hi, wi));
  }
  int32 hmx = 0, wmx = 0;
  REP(i, h) { chmax(hmx, hcnt[i]); }
  REP(i, w) { chmax(wmx, wcnt[i]); }
  vector<int32> hmxi, wmxj;
  REP(i, h) {
    if (hcnt[i] == hmx)
      hmxi.pb(i);
  }
  REP(j, w) {
    if (wcnt[j] == wmx)
      wmxj.pb(j);
  }
  bool allobj = true;
  if ((int64)hmxi.size() * (int64)wmxj.size() <= m) {
    for (auto i : hmxi)
      for (auto j : wmxj) {
        if (obj.count(pii(i, j)) == 0)
          allobj = false;
      }
  } else {
    allobj = false;
  }
  if (allobj) {
    ANS(hmx + wmx - 1);
  } else {
    ANS(hmx + wmx);
  }
  return 0;
}