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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
const ll mod = 1000000007;
const ll INF = 1e+14;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define _GLIBCXX_DEBUG

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> retu[h];
  vector<int> gyou[w];
  map<P, bool> mp;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    retu[a].push_back(b);
    gyou[b].push_back(a);
    mp[P{a, b}] = 1;
  }

  int retusize = 0;
  int gyousize = 0;

  rep(i, h) {
    int p = retu[i].size();
    retusize = max(p, retusize);
  }
  rep(i, w) {
    int p = gyou[i].size();
    gyousize = max(p, gyousize);
  }

  int ans = gyousize + retusize;
  vector<int> maxretu;
  vector<int> maxgyou;
  rep(i, h) {
    if (retu[i].size() == retusize)
      maxretu.push_back(i);
  }
  rep(i, w) {
    if (gyou[i].size() == gyousize)
      maxgyou.push_back(i);
  }
  bool judge = false;
  int mr = maxretu.size();
  int mg = maxgyou.size();
  rep(i, mr) {
    rep(j, mg) {
      int p = maxretu[i];
      int q = maxgyou[j];
      if (!mp[P{p, q}])
        judge = true;
      if (judge)
        break;
    }
    if (judge)
      break;
  }
  if (!judge)
    ans--;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
const ll mod = 1000000007;
const ll INF = 1e+14;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
#define _GLIBCXX_DEBUG

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> retu[h];
  vector<int> gyou[w];
  vector<P> bomb;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    retu[a].push_back(b);
    gyou[b].push_back(a);
    bomb.push_back({a, b});
  }

  int retusize = 0;
  int gyousize = 0;

  rep(i, h) {
    int p = retu[i].size();
    retusize = max(p, retusize);
  }
  rep(i, w) {
    int p = gyou[i].size();
    gyousize = max(p, gyousize);
  }

  int ans = gyousize + retusize;
  vector<int> maxretu;
  vector<int> maxgyou;
  rep(i, h) {
    if (retu[i].size() == retusize)
      maxretu.push_back(i);
  }
  rep(i, w) {
    if (gyou[i].size() == gyousize)
      maxgyou.push_back(i);
  }
  bool judge = false;
  int mr = maxretu.size();
  int mg = maxgyou.size();
  rep(i, mr) {
    rep(j, mg) {
      int p = maxretu[i];
      int q = maxgyou[j];
      bool judge1 = true;
      rep(k, m) {
        if (bomb[k].first == p && bomb[k].second == q)
          judge1 = false;
      }
      if (judge1)
        judge = true;
      if (judge)
        break;
    }
    if (judge)
      break;
  }
  if (!judge)
    ans--;
  cout << ans << endl;
}