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

#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long
#define dd double
#define pb push_back
#define ff first
#define ss second
#define Mp make_pair
const ll Mod = 1000000007;
const ll INF = 999999999999999999;
const ll NN = (ll)(1e6 + 5);
ll min(ll x, ll y) {
  if (x < y)
    return x;
  return y;
}
ll max(ll x, ll y) {
  if (x > y)
    return x;
  return y;
}
ll power(ll x, unsigned ll y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return power(x, y / 2) * power(x, y / 2);
  else
    return x * power(x, y / 2) * power(x, y / 2);
}
ll hcf(ll a, ll b) {
  if (b == 0)
    return a;
  return hcf(b, a % b);
}
int main() {
  fio;
  /*#ifndef ONLINE_JUDGE
       freopen("input.txt", "r", stdin);
       freopen("output.txt", "w", stdout);
       freopen("error.txt", "w", stderr);
  #endif*/
  ll TT = 1;
  // cin>>TT;
  while (TT--) {
    ll n, m, x, y, k, i;
    cin >> n >> m >> k;
    map<ll, ll> r, c;
    map<pair<ll, ll>, ll> cnt;
    for (i = 0; i < k; i++) {
      cin >> x >> y;
      cnt[{x, y}]++;
      r[x]++;
      c[y]++;
    }
    ll mxc = 0, mxr = 0, ans = 0;
    set<ll> row, col;
    for (auto u : r)
      if (mxr < u.ss)
        mxr = u.ss;
    for (auto u : r)
      if (u.ss == mxr)
        row.insert(u.ff);
    for (auto u : c)
      if (mxc < u.ss)
        mxc = u.ss;
    for (auto u : c)
      if (u.ss == mxc)
        col.insert(u.ff);
    ll f = 0;
    for (auto u : row) {
      for (auto uu : col)
        if (!cnt[{u, uu}]) {
          f = 1;
          break;
        }
      if (f)
        break;
    }
    ans = mxc + mxr - 1;
    if (f)
      ans++;
    cout << ans;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long
#define dd double
#define pb push_back
#define ff first
#define ss second
#define Mp make_pair
const ll Mod = 1000000007;
const ll INF = 999999999999999999;
const ll NN = (ll)(1e6 + 5);
ll min(ll x, ll y) {
  if (x < y)
    return x;
  return y;
}
ll max(ll x, ll y) {
  if (x > y)
    return x;
  return y;
}
ll power(ll x, unsigned ll y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return power(x, y / 2) * power(x, y / 2);
  else
    return x * power(x, y / 2) * power(x, y / 2);
}
ll hcf(ll a, ll b) {
  if (b == 0)
    return a;
  return hcf(b, a % b);
}
int main() {
  fio;
  /*#ifndef ONLINE_JUDGE
       freopen("input.txt", "r", stdin);
       freopen("output.txt", "w", stdout);
       freopen("error.txt", "w", stderr);
  #endif*/
  ll TT = 1;
  // cin>>TT;
  while (TT--) {
    ll n, m, x, y, k, i;
    cin >> n >> m >> k;
    map<ll, ll> r, c;
    map<pair<ll, ll>, ll> cnt;
    for (i = 0; i < k; i++) {
      cin >> x >> y;
      cnt[{x, y}]++;
      r[x]++;
      c[y]++;
    }
    ll mxc = 0, mxr = 0, ans = 0;
    set<ll> row, col;
    for (auto u : r)
      if (mxr < u.ss)
        mxr = u.ss;
    for (auto u : r)
      if (u.ss == mxr)
        row.insert(u.ff);
    for (auto u : c)
      if (mxc < u.ss)
        mxc = u.ss;
    for (auto u : c)
      if (u.ss == mxc)
        col.insert(u.ff);
    ll f = 0;
    for (auto u : row) {
      for (auto uu : col)
        if (!cnt[{u, uu}])
          f = 1;
    }
    ans = mxc + mxr - 1;
    if (f)
      ans++;
    cout << ans;
  }
}