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

/////////////////////////////////TEST CASES////////////////////////////////////
/*
 */
/////////////////////////////////////CODE//////////////////////////////////////
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = a; i > b; i--)
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define PI 3.14159265
typedef long long ll;
#define vl vector<ll>
#define IN(inp)                                                                \\
  ll inp;                                                                      \\
  cin >> inp;
#define pb push_back
#define all(a) a.begin(), a.end()
#define FR(i, a) for (auto i : a)
#define what(A) cout << #A << " is " << A << endl;
ll MAX = 100000000000;
ll MOD = 1000000007;
void solve() {
  IN(h) IN(w) IN(m);
  map<ll, ll> r, c;
  map<pair<ll, ll>, ll> pairs;
  FOR(i, 0, m) {
    IN(in1) IN(in2);
    r[in1]++;
    c[in2]++;
    pairs[{in1, in2}] = 1;
  }
  ll check1 = 0, check2 = 0;
  vl q, j;
  FR(i, r) {
    if (check1 < i.second)
      check1 = i.second;
  }
  FR(i, r) {
    if (i.second == check1)
      q.pb(i.first);
  }
  FR(i, c) {
    if (check2 < i.second)
      check2 = i.second;
  }
  FR(i, c) {
    if (i.second == check2)
      j.pb(i.first);
  }
  ll ans = check1 + check2 - 1, f = 0;
  FR(i, q) {
    FR(z, j) {
      if (pairs[{i, z}] == 0) {
        f = 1;
        break;
      }
    }
  }
  cout << ans + f << endl;
}
int main() {
  fastio
      // freopen("input.txt", "rt", stdin);
      // freopen("output.txt", "wt", stdout);
      ll test = 1;
  // cin >> test;
  while (test--) {
    solve();
  }
}

Error: TLE

Fixed submission:

/////////////////////////////////TEST CASES////////////////////////////////////
/*
 */
/////////////////////////////////////CODE//////////////////////////////////////
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORD(i, a, b) for (ll i = a; i > b; i--)
#define fastio ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define PI 3.14159265
typedef long long ll;
#define vl vector<ll>
#define IN(inp)                                                                \\
  ll inp;                                                                      \\
  cin >> inp;
#define pb push_back
#define all(a) a.begin(), a.end()
#define FR(i, a) for (auto i : a)
#define what(A) cout << #A << " is " << A << endl;
ll MAX = 100000000000;
ll MOD = 1000000007;
void solve() {
  IN(h) IN(w) IN(m);
  map<ll, ll> r, c;
  map<pair<ll, ll>, ll> check;
  FOR(i, 0, m) {
    IN(in1) IN(in2);
    r[in1]++;
    c[in2]++;
    check[{in1, in2}]++;
  }
  ll ans = 0, r1, c1;
  FR(i, r) {
    FR(j, c) {
      if (j.second + i.second >=
          ans + (check[{i.first, j.first}] == 1 ? 1 : 0)) {
        ans = i.second + j.second - (check[{i.first, j.first}] == 1 ? 1 : 0);
      }
    }
  }
  cout << ans;
}
int main() {
  fastio
      // freopen("input.txt", "rt", stdin);
      // freopen("output.txt", "wt", stdout);
      ll test = 1;
  // cin >> test;
  while (test--) {
    solve();
  }
}