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

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

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#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, lessl<int>, rb_tree_tag,                                \\
       tree_order_statistics_node_update>
#define ll long long
#define int long long
#define double long double

#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()

#define ff first
#define ss second
#define mp make_pair

#define pb push_back
#define endl "\\n"

#define f(i, l, r) for (int i = l; i <= r; i++)
#define rf(i, r, l) for (int i = r; i >= l; i--)

#define bp __builtin_popcountll
#define inf 1e18

const int N = 2e3 + 5;
const int M = 1e9 + 7;
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  map<int, int> r, w;
  map<pair<int, int>, int> mm;
  vector<pair<int, int>> v(k);
  int ans = 0;
  int rm = 0, wm = 0;
  for (int i = 0; i < k; i++) {
    int t1, t2;
    cin >> t1 >> t2;
    v[i].ff = t1;
    v[i].ss = t2;
    r[t1]++;
    w[t2]++;
    mm[mp(t1, t2)]++;
    rm = max(rm, r[t1]);
    wm = max(wm, w[t2]);
  }
  ans = rm + wm - 1;

  for (int i = 0; i <= n; i++) {
    if (r.find(i) != r.end()) {
      if (r[i] != rm) {
        r.erase(r.find(i));
      }
    }
  }
  for (int i = 0; i <= m; i++) {
    if (w.find(i) != w.end()) {
      if (w[i] != wm) {
        w.erase(w.find(i));
      }
    }
  }
  for (auto X : r) {
    int tar = X.ff;
    if (X.ss == rm) {
      for (auto jj : w) {
        int j = jj.ff;
        if (jj.ss == wm && mm.find(mp(tar, j)) == mm.end()) {
          ans++;
          cout << ans << endl;
          return;
        }
      }
    }
  }
  cout << ans << endl;
}

signed main() {
  FAST int t = 1;
  // cin>>t;
  for (int tc = 1; tc <= t; tc++) {
    // cout<<"Case #"<<tc<<": ";
    solve();
  }
}


Error: TLE

Fixed submission:

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

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#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, lessl<int>, rb_tree_tag,                                \\
       tree_order_statistics_node_update>
#define ll long long
#define int long long
#define double long double

#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()

#define ff first
#define ss second
#define mp make_pair

#define pb push_back
#define endl "\\n"

#define f(i, l, r) for (int i = l; i <= r; i++)
#define rf(i, r, l) for (int i = r; i >= l; i--)

#define bp __builtin_popcountll
#define inf 1e18

const int N = 2e3 + 5;
const int M = 1e9 + 7;
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  map<int, int> r, w;
  map<pair<int, int>, int> mm;
  vector<pair<int, int>> v(k);
  int ans = 0;
  int rm = 0, wm = 0;
  for (int i = 0; i < k; i++) {
    int t1, t2;
    cin >> t1 >> t2;
    v[i].ff = t1;
    v[i].ss = t2;
    r[t1]++;
    w[t2]++;
    mm[mp(t1, t2)]++;
    rm = max(rm, r[t1]);
    wm = max(wm, w[t2]);
  }
  ans = rm + wm - 1;

  for (auto X : r) {
    int tar = X.ff;
    if (X.ss == rm) {
      for (auto jj : w) {
        int j = jj.ff;
        if (jj.ss == wm && mm.find(mp(tar, j)) == mm.end()) {
          ans++;
          cout << ans << endl;
          return;
        }
      }
    }
  }
  cout << ans << endl;
}

signed main() {
  FAST int t = 1;
  // cin>>t;
  for (int tc = 1; tc <= t; tc++) {
    // cout<<"Case #"<<tc<<": ";
    solve();
  }
}