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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x)                                                                \\
  {                                                                            \\
    for (auto i : x) {                                                         \\
      cout << i << " ";                                                        \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  // 行と列で爆弾の数を管理
  int H, W, M;
  cin >> H >> W >> M;
  int col[W], row[H];
  set<P> bombs;
  memset(col, 0, sizeof(col));
  memset(row, 0, sizeof(row));
  int maxCol = 0;
  int maxRow = 0;
  rep(i, M) {
    int h, w;
    cin >> h >> w;
    --h;
    --w;
    row[h]++;
    col[w]++;
    bombs.insert(make_pair(h, w));
  }
  rep(i, H) maxRow = max(maxRow, row[i]);
  rep(i, W) maxCol = max(maxCol, col[i]);
  set<int> stCol;
  set<int> stRow;
  // 最大のもの
  rep(i, H) {
    if (row[i] == maxRow)
      stRow.insert(i);
  }
  rep(i, W) {
    if (col[i] == maxCol)
      stCol.insert(i);
  }
  int ans = maxCol + maxRow;
  // for (auto p : bombs) {
  //     int h = p.first;
  //     int w = p.second;
  //     int temp = col[w] + row[h] - 1;
  //     if (temp > ans) ans = temp;
  // }
  bool flag = false;
  for (int r : stRow) {
    for (int c : stCol) {
      if (bombs.find(make_pair(r, c)) == bombs.end()) {
        flag = true;
        break;
      }
    }
    if (flag)
      break;
  }
  if (!flag)
    ans--;
  cout << ans << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x)                                                                \\
  {                                                                            \\
    for (auto i : x) {                                                         \\
      cout << i << " ";                                                        \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  // 行と列で爆弾の数を管理
  int H, W, M;
  cin >> H >> W >> M;
  int col[W], row[H];
  set<P> bombs;
  memset(col, 0, sizeof(col));
  memset(row, 0, sizeof(row));
  int maxCol = 0;
  int maxRow = 0;
  rep(i, M) {
    int h, w;
    cin >> h >> w;
    --h;
    --w;
    row[h]++;
    col[w]++;
    bombs.insert(make_pair(h, w));
  }
  rep(i, H) maxRow = max(maxRow, row[i]);
  rep(i, W) maxCol = max(maxCol, col[i]);
  set<int> stCol;
  set<int> stRow;
  // 最大のもの
  rep(i, H) {
    if (row[i] == maxRow)
      stRow.insert(i);
  }
  rep(i, W) {
    if (col[i] == maxCol)
      stCol.insert(i);
  }
  int ans = maxCol + maxRow;
  // for (auto p : bombs) {
  //     int h = p.first;
  //     int w = p.second;
  //     int temp = col[w] + row[h] - 1;
  //     if (temp > ans) ans = temp;
  // }
  bool flag = false;
  for (int r : stRow) {
    for (int c : stCol) {
      if (bombs.find(make_pair(r, c)) == bombs.end())
        flag = true;
    }
  }
  if (!flag)
    ans--;
  cout << ans << '\\n';
  return 0;
}