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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using ull = unsigned long long;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
ll mod = 1e9 + 7;
ll inf = 1e9 + 7;
template <typename T, typename U> T binpow(T a, U n) {
  if (n == 0)
    return 1;
  if (n % 2 == 1)
    return (binpow(a, n - 1) * a) % mod;
  else {
    ll b = binpow(a, n / 2) % mod;
    return (b * b) % mod;
  }
}
template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
template <typename T> istream &operator>>(istream &str, vector<T> &vec) {
  for (auto &w : vec)
    str >> w;
  return str;
}
template <typename T> ostream &operator<<(ostream &str, vector<T> &vec) {
  for (auto &w : vec)
    str << w << ' ';
  return str;
}

int main() {
  ll h, w, ch, cw, dh, dw, m;
  cin >> h >> w >> m;
  /*vector<vector<ll>>vec(h, vector<ll>(w, inf));
  vector<vector<bool>> mapp(h, vector<bool>(w));
  for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
          char c;
          cin >> c;
          if (c == '.')mapp[i][j] = 1;
      }
  }
  for(int i )*/
  vector<int> cols(w), rows(h);
  vector<pair<int, int>> tg(m);
  set<pair<int, int>> mn;
  for (int i = 0; i < m; ++i) {
    cin >> tg[i].first >> tg[i].second;
    mn.insert(tg[i]);
    rows[tg[i].first - 1]++;
    cols[tg[i].second - 1]++;
  }
  int maxcol = max_element(all(cols)) - cols.begin();
  int maxrow = max_element(all(rows)) - rows.begin();
  vector<int> irow, icol;
  for (int i = 0; i < rows.size(); ++i) {
    if (rows[i] == rows[maxrow])
      irow.push_back(i);
  }
  for (int i = 0; i < cols.size(); ++i) {
    if (cols[i] == cols[maxcol])
      icol.push_back(i);
  }
  bool flag = false;
  for (int i = 0; i < irow.size(); ++i) {
    for (int j = 0; j < icol.size(); ++j) {
      if (mn.count({irow[i] + 1, icol[j] + 1}) == 0) {
        flag = true;
        break;
      }
    }
  }
  if (!flag) {
    cout << cols[maxcol] + rows[maxrow] - 1;
  } else {
    cout << cols[maxcol] + rows[maxrow];
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using ull = unsigned long long;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define pb(x) push_back(x)
ll mod = 1e9 + 7;
ll inf = 1e9 + 7;
template <typename T, typename U> T binpow(T a, U n) {
  if (n == 0)
    return 1;
  if (n % 2 == 1)
    return (binpow(a, n - 1) * a) % mod;
  else {
    ll b = binpow(a, n / 2) % mod;
    return (b * b) % mod;
  }
}
template <typename T> T gcd(T a, T b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
template <typename T> istream &operator>>(istream &str, vector<T> &vec) {
  for (auto &w : vec)
    str >> w;
  return str;
}
template <typename T> ostream &operator<<(ostream &str, vector<T> &vec) {
  for (auto &w : vec)
    str << w << ' ';
  return str;
}

int main() {
  ll h, w, ch, cw, dh, dw, m;
  cin >> h >> w >> m;
  /*vector<vector<ll>>vec(h, vector<ll>(w, inf));
  vector<vector<bool>> mapp(h, vector<bool>(w));
  for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
          char c;
          cin >> c;
          if (c == '.')mapp[i][j] = 1;
      }
  }
  for(int i )*/
  vector<int> cols(w), rows(h);
  vector<pair<int, int>> tg(m);
  set<pair<int, int>> mn;
  for (int i = 0; i < m; ++i) {
    cin >> tg[i].first >> tg[i].second;
    mn.insert(tg[i]);
    rows[tg[i].first - 1]++;
    cols[tg[i].second - 1]++;
  }
  int maxcol = max_element(all(cols)) - cols.begin();
  int maxrow = max_element(all(rows)) - rows.begin();
  vector<int> irow, icol;
  for (int i = 0; i < rows.size(); ++i) {
    if (rows[i] == rows[maxrow])
      irow.push_back(i);
  }
  for (int i = 0; i < cols.size(); ++i) {
    if (cols[i] == cols[maxcol])
      icol.push_back(i);
  }
  bool flag = false;
  for (int i = 0; i < irow.size(); ++i) {
    for (int j = 0; j < icol.size(); ++j) {
      if (mn.count({irow[i] + 1, icol[j] + 1}) == 0)
        flag = true;
    }
  }
  if (!flag) {
    cout << cols[maxcol] + rows[maxrow] - 1;
  } else {
    cout << cols[maxcol] + rows[maxrow];
  }
}