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

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
#define pb push_back
#define ld long double
#define f first
#define s second
#define in insert
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const int INF = 1e18 + 5;
const int mod = 1e9 + 7;
const int N = 2e5 + 314;
const long double pi = 3.1415926535897932384626433832795;
void solve() {
  int n, m, t;
  cin >> n >> m >> t;
  map<int, multiset<int>> p1;
  map<int, multiset<int>> p2;
  map<pair<int, int>, int> u;
  int mx1 = 0, mx2 = 0, id1 = 0, id2 = 0;
  for (int i = 0; i < t; i++) {
    int h, w;
    cin >> h >> w;
    u[{h, w}]++;
    p1[h].in(w);
    p2[w].in(h);
    mx1 = max(sz(p1[h]), mx1);
    mx2 = max(sz(p2[w]), mx2);
  }
  int aa = mx1 + mx2;
  vi a, b;
  map<pair<int, int>, int> ans;
  for (auto i : p1) {
    if (sz(i.s) == mx1)
      a.pb(i.f);
  }
  for (auto i : p2) {
    if (sz(i.s) == mx2)
      b.pb(i.f);
  }
  for (int i = 0; i < sz(a); i++) {
    for (int j = 0; j < sz(b); j++) {
      if (!u[{a[i], b[j]}]) {
        cout << aa;
        return;
      }
    }
  }
  cout << aa - 1;
}
signed main() {
  speed;
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
#define pb push_back
#define ld long double
#define f first
#define s second
#define in insert
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define speed                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
const int INF = 1e18 + 5;
const int mod = 1e9 + 7;
const int N = 2e5 + 314;
const long double pi = 3.1415926535897932384626433832795;
void solve() {
  int n, m, t;
  cin >> n >> m >> t;
  map<int, multiset<int>> p1;
  map<int, multiset<int>> p2;
  map<pair<int, int>, int> cnt;
  map<pair<int, int>, int> u;
  int mx1 = 0, mx2 = 0, id1 = 0, id2 = 0;
  for (int i = 0; i < t; i++) {
    int h, w;
    cin >> h >> w;
    u[{h, w}]++;
    p1[h].in(w);
    p2[w].in(h);
    mx1 = max(mx1, sz(p1[h]));
    mx2 = max(sz(p2[w]), mx2);
  }
  int a = 0, b = 0, ans = mx1 + mx2;
  set<int> x, y;
  for (auto i : p1) {
    if (sz(i.s) == mx1)
      x.in(i.f);
  }
  for (auto i : p2) {
    if (sz(i.s) == mx2)
      y.in(i.f);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (!u[{i, j}]) {
        if (sz(p1[i]) + sz(p2[j]) == ans) {
          cout << ans;
          return;
        }
      }
    }
  }
  cout << ans - 1;
}
signed main() {
  speed;
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
}