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

#include <bits/stdc++.h>

using namespace std;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(vector<vector<T>> x) {
  cerr << "\\n{";
  int f = x.size();
  for (int i = 0; i < f; i++) {
    cerr << "(";
    int g = x[i].size();
    for (auto j : x[i]) {
      cerr << j;
      (--g != 0) ? cerr << "," : cerr << "";
    }
    cerr << ")\\n";
  }
  cerr << "}";
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void __print(vector<bool> x) {
  cerr << "{";
  int f = x.size();
  for (auto i : x) {
    cerr << i;
    (--f != 0) ? cerr << "," : cerr << "";
  }
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifndef ONLINE_JUDGE
#define trace(x...) cerr << "[" << #x << "] = [", _print(x)
#else
#define trace(x...)
#endif

template <typename... T> void read(T &...args) { ((cin >> args), ...); }

template <typename... T> void writ(T &&...args) {
  ((cout << args << " "), ...);
}
#define ull unsigned long long
#define ll long long
#define MOD 1000000007
#define INF (ll)1e18
#define IINF (int)1e9
#define EPS 1e-9

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w, m;
  cin >> h >> w >> m;
  set<pair<int, int>> chk;
  vector<pair<int, int>> r_c(h), c_c(w);
  for (int i = 0; i < h; i++)
    r_c[i].second = i;
  for (int i = 0; i < w; i++)
    c_c[i].second = i;
  for (int i = 0; i < m; i++) {
    int h_t, w_t;
    cin >> h_t >> w_t;
    h_t--;
    w_t--;
    r_c[h_t].first++;
    c_c[w_t].first++;
    chk.insert({h_t, w_t});
  }
  int ma = 0;
  sort(r_c.rbegin(), r_c.rend());
  sort(c_c.rbegin(), c_c.rend());
  for (auto i : r_c) {
    for (auto j : c_c) {
      int flag = 0;
      if (chk.count({i.second, j.second}))
        flag = 1;
      ma = max(ma, i.first + j.first - flag);
      if (!flag) {
        break;
      }
    }
  }
  cout << ma << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(vector<vector<T>> x) {
  cerr << "\\n{";
  int f = x.size();
  for (int i = 0; i < f; i++) {
    cerr << "(";
    int g = x[i].size();
    for (auto j : x[i]) {
      cerr << j;
      (--g != 0) ? cerr << "," : cerr << "";
    }
    cerr << ")\\n";
  }
  cerr << "}";
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void __print(vector<bool> x) {
  cerr << "{";
  int f = x.size();
  for (auto i : x) {
    cerr << i;
    (--f != 0) ? cerr << "," : cerr << "";
  }
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}

#ifndef ONLINE_JUDGE
#define trace(x...) cerr << "[" << #x << "] = [", _print(x)
#else
#define trace(x...)
#endif

template <typename... T> void read(T &...args) { ((cin >> args), ...); }

template <typename... T> void writ(T &&...args) {
  ((cout << args << " "), ...);
}
#define ull unsigned long long
#define ll long long
#define MOD 1000000007
#define INF (ll)1e18
#define IINF (int)1e9
#define EPS 1e-9

vector<vector<int>> adj_row, adj_col;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w, m;
  cin >> h >> w >> m;
  adj_row.resize(h + 1);
  adj_col.resize(w + 1);
  set<pair<int, int>> chk;
  set<int> r, c;
  int ma_row = 0, ma_col = 0;
  for (int i = 0; i < m; i++) {
    int h_t, w_t;
    cin >> h_t >> w_t;
    adj_row[h_t].push_back(w_t);
    adj_col[w_t].push_back(h_t);
    if (adj_row[h_t].size() >= ma_row) {
      ma_row = adj_row[h_t].size();
    }
    if (adj_col[w_t].size() > ma_col) {
      ma_col = adj_col[w_t].size();
    }
    r.insert(h_t);
    c.insert(w_t);
    chk.insert({h_t, w_t});
  }
  int ma = 0;
  for (auto i : r) {
    for (auto j : c) {
      if (adj_row[i].size() == ma_row && adj_col[j].size() == ma_col) {
        if (chk.find({i, j}) != chk.end()) {
          ma = ma_col + ma_row - 1;
        } else {
          ma = ma_col + ma_row;
          goto b;
        }
      }
    }
  }
b:
  cout << ma << endl;
  return 0;
}