alexjercan/bugnet huggingface dataset:
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int NMAX = 3e5 + 10;
const int MOD = 1e9 + 7;
int col[NMAX], row[NMAX];
bool col_vised[NMAX], row_vised[NMAX];

int main(int argc, char const *argv[]) {
  int h, w, m;
  int x, y;
  vector<pair<int, int>> v;
  scanf("%d%d%d", &h, &w, &m);
  for (register int i = 0; i < m; i++)
    scanf("%d%d", &x, &y), col[y]++, row[x]++, v.push_back({x, y});
  if (h == 1 || w == 1)
    return 0 * printf("%d\\n", m);

  int ans = 0;
  int row_max = 0, col_max = 0;
  int col_num = 0, row_num = 0;
  for (register int i = 1; i <= h; i++)
    row_max = max(row_max, row[i]);
  for (register int i = 1; i <= h; i++)
    if (row[i] == row_max)
      row_vised[i] = true, row_num++;

  for (register int i = 1; i <= w; i++)
    col_max = max(col_max, col[i]);
  for (register int i = 1; i <= w; i++)
    if (col[i] == col_max)
      col_vised[i] = true, col_num++;
  int tot = 0;
  for (register int i = 0; i < v.size(); i++)
    tot += row_vised[v[i].first] && col_vised[v[i].second] ? 1 : 0;
  printf("%d\\n",
         tot == col_num * row_num ? col_max + row_max - 1 : col_max + row_max);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int NMAX = 3e5 + 10;
const int MOD = 1e9 + 7;
int col[NMAX], row[NMAX];

int main(int argc, char const *argv[]) {
  int h, w, m;
  int x, y;
  map<pair<int, int>, int> mm;
  scanf("%d%d%d", &h, &w, &m);
  for (register int i = 0; i < m; i++)
    scanf("%d%d", &x, &y), col[y]++, row[x]++, mm[make_pair(x, y)] = 1;
  int ans = 0;
  for (register int i = 1; i <= h; i++)
    for (register int j = 1; j <= w; j++)
      ans = max(ans, row[i] + col[j] - mm[make_pair(i, j)]);
  printf("%d\\n", ans);
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> vh(h + 1, 0);
  vector<int> vw(w + 1, 0);
  unordered_map<string, int> u;
  for (int i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    vh[l]++;
    vw[r]++;
    string temp = to_string(l) + " " + to_string(r);
    u[temp]++;
  }
  int m1 = 0;
  int m2 = 0;
  for (int i = 0; i <= h; i++) {
    if (vh[i] > m1) {
      m1 = vh[i];
    }
  }
  for (int i = 0; i <= w; i++) {
    if (vw[i] > m2) {
      m2 = vw[i];
    }
  }
  vector<int> l1;
  vector<int> r1;
  for (int i = 0; i <= h; i++) {
    if (vh[i] == m1) {
      l1.push_back(i);
    }
  }
  for (int i = 0; i <= w; i++) {
    if (vw[i] == m2) {
      r1.push_back(i);
    }
  }
  int ans = 0;
  for (int i = 0; i < l1.size(); i++) {
    for (int j = 0; j < r1.size(); j++) {

      string temp = to_string(l1[i]) + " " + to_string(r1[j]);
      if (u.find(temp) != u.end()) {
        ans = max(ans, m1 + m2 - 1);
      } else {
        ans = max(ans, m1 + m2);
        break;
      }
    }
  }
  cout << ans << "\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> r(H + 1, 0);
  vector<int> c(W + 1, 0);
  long long int maxr = 0, maxc = 0;
  int A[H + 1][W + 1];
  for (int i = 0; i < M; i++) {
    int row, column;
    cin >> row;
    cin >> column;
    A[row][column] = 1;
    r[row]++;
    if (r[row] > maxr) {
      maxr = r[row];
    }
    c[column]++;
    if (c[column] > maxc) {
      maxc = c[column];
    }
  }
  vector<int> a;
  vector<int> b;
  for (int i = 0; i <= H; i++) {
    if (r[i] == maxr) {
      a.push_back(i);
    }
  }
  for (int j = 0; j <= W; j++) {
    if (c[j] == maxc) {
      b.push_back(j);
    }
  }
  long long int ans;
  for (int i = 0; i < a.size(); i++) {
    for (int j = 0; j < b.size(); j++) {
      if (A[a[i]][b[j]])
        ans = maxr + maxc - 1;
      else {
        ans = maxr + maxc;
        break;
      }
    }
  }
  cout << maxr + maxc << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(first, second) '(' << first << ',' << second << ')'
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;
ll INF = 1001001001001001001;
long double EPS = 1e-9;
long double PI = 3.141592653589793238;
template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<pair<ll, ll>>>;

// MOD確認

ll H, W, M;

pair<ll, ll> Y[300010];
pair<ll, ll> X[300010];

int main() {
  cin >> H >> W >> M;
  map<pair<ll, ll>, ll> mp;
  rep(i, 300010) {
    Y[i].second = i;
    X[i].second = i;
  }
  rep(i, M) {
    ll h, w;
    cin >> h >> w;
    h--;
    w--;
    mp[make_pair(h, w)]++;
    Y[h].first++;
    X[w].first++;
  }
  ll ans = 0;
  sort(X, X + 300010);
  sort(Y, Y + 300010);
  reverse(X, X + 300010);
  reverse(Y, Y + 300010);
  rep(i, 10) rep(j, M) {
    ll num = Y[i].first + X[j].first;
    if (mp[make_pair(Y[i].second, X[j].second)] >= 1) {
      num--;
    }
    ans = max(ans, num);
  }
  rep(i, M) rep(j, 10) {
    ll num = Y[i].first + X[j].first;
    if (mp[make_pair(Y[i].second, X[j].second)] >= 1) {
      num--;
    }
    ans = max(ans, num);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(first, second) '(' << first << ',' << second << ')'
constexpr ll MOD = 1000000007;
// constexpr ll MOD = 998244353;
ll INF = 1001001001001001001;
long double EPS = 1e-9;
long double PI = 3.141592653589793238;
template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<pair<ll, ll>>>;

// MOD確認

ll H, W, M;

pair<ll, ll> Y[300010];
pair<ll, ll> X[300010];

int main() {
  cin >> H >> W >> M;
  map<pair<ll, ll>, ll> mp;
  rep(i, 300010) {
    Y[i].second = i;
    X[i].second = i;
  }
  rep(i, M) {
    ll h, w;
    cin >> h >> w;
    h--;
    w--;
    mp[make_pair(h, w)]++;
    Y[h].first++;
    X[w].first++;
  }
  ll ans = 0;
  sort(X, X + 300010);
  sort(Y, Y + 300010);
  reverse(X, X + 300010);
  reverse(Y, Y + 300010);
  rep(i, 100) rep(j, M) {
    ll num = Y[i].first + X[j].first;
    if (mp[make_pair(Y[i].second, X[j].second)] >= 1) {
      num--;
    }
    ans = max(ans, num);
  }
  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;
#define REP(i, n) for (int i = 0; i < (n); ++i)
int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> row(h), column(w);
  set<pair<int, int>> bomb;
  REP(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    ++row[a];
    ++column[b];
    bomb.emplace(a, b);
  }
  int mh = 0, mw = 0;
  vector<int> max_row, max_column;
  REP(i, h) mh = max(row[i], mh);
  REP(i, w) mw = max(column[i], mw);
  REP(i, h) if (mh == row[i]) max_row.push_back(i);
  REP(j, w) if (mw == column[j]) max_column.push_back(j);
  int ans = mh + mw;
  int faul = 0;
  for (int i : max_row)
    for (int j : max_column) {
      if (bomb.count(P(i, j)))
        continue;
      cout << ans << endl;
      return 0;
    }
  --ans;
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<vector<int>> map(h, vector<int>(w, 0));
  vector<pair<int, int>> bomb(m);
  REP(i, m) {
    cin >> bomb[i].first >> bomb[i].second;
    --bomb[i].first;
    --bomb[i].second;
  }
  REP(i, m) {
    REP(k, h) map[k][bomb[i].second] += 1;
    REP(k, w) map[bomb[i].first][k] += 1;
    map[bomb[i].first][bomb[i].second] -= 1;
  }
  int max = 0;
  for (int i = 0; i < h; ++i) {
    for (int j = 0; j < w; ++j) {
      if (map[i][j] > max)
        max = map[i][j];
    }
  }
  cout << max << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> cnth(h), cntw(w);
  set<pair<int, int>> g;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> y >> x;
    x--, y--;
    cnth.at(y).first++, cntw.at(x).first++;
    g.insert({x, y});
  }

  for (int i = 0; i < h; i++)
    cnth.at(i).second = i;
  for (int j = 0; j < w; j++)
    cntw.at(j).second = j;

  sort(cnth.begin(), cnth.end()), sort(cntw.begin(), cntw.end());
  reverse(cnth.begin(), cnth.end()), reverse(cntw.begin(), cntw.end());

  int ans = 0, ansh = cnth.at(0).first, answ = cntw.at(0).first;
  for (int i = 0; i < h; i++) {
    if (ansh - 1 > cnth.at(i).first)
      break;
    bool ok = false;
    for (int j = 0; j < w; j++) {
      if (answ - 1 > cntw.at(j).first)
        break;
      int k = cnth.at(i).first + cntw.at(j).first;
      if (g.count({cntw.at(j).second, cnth.at(i).second}))
        k--;
      else
        ok = true;
      ans = max(ans, k);
      if (ok)
        break;
    }
    if (ok)
      break;
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<pair<int, int>> cnth(h), cntw(w);
  vector<vector<int>> g(h, vector<int>(w, 0));
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> y >> x;
    x--, y--;
    cnth.at(y).first++, cntw.at(x).first++;
    g.at(y).at(x)++;
  }

  for (int i = 0; i < h; i++)
    cnth.at(i).second = i;
  for (int j = 0; j < w; j++)
    cntw.at(j).second = j;

  sort(cnth.begin(), cnth.end()), sort(cntw.begin(), cntw.end());
  reverse(cnth.begin(), cnth.end()), reverse(cntw.begin(), cntw.end());

  int ans = 0, ansh = cnth.at(0).first, answ = cntw.at(0).first;
  for (int i = 0; i < h; i++) {
    if (ansh > cnth.at(i).first)
      break;
    bool ok = false;
    for (int j = 0; j < w; j++) {
      if (answ > cntw.at(j).first)
        ok = true;
      int k = cnth.at(i).first + cntw.at(j).first;
      if (g.at(cnth.at(i).second).at(cntw.at(j).second) > 0)
        k--;
      else
        ok = true;
      ans = max(ans, k);
      if (ok)
        break;
    }
    if (ok)
      break;
    cout << endl;
  }

  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> h(M), w(M);
  for (int i = 0; i < M; i++) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
  }
  set<pair<int, int>> st;
  for (int i = 0; i < M; i++) {
    st.insert(make_pair(h[i], w[i]));
  }
  vector<int> c1(H, 0);
  vector<int> c2(W, 0);
  for (int i = 0; i < M; i++) {
    c1[h[i]]++;
    c2[w[i]]++;
  }
  vector<pair<int, int>> C1(H);
  for (int i = 0; i < H; i++) {
    C1[i] = make_pair(c1[i], i);
  }
  sort(C1.rbegin(), C1.rend());
  vector<pair<int, int>> C2(W);
  for (int i = 0; i < W; i++) {
    C2[i] = make_pair(c2[i], i);
  }
  sort(C2.rbegin(), C2.rend());
  int mx = C1[0].first + C2[0].first;
  bool ok = false;
  for (int i = 0; i < H; i++) {
    if (C1[i].first == C1[0].first) {
      for (int j = 0; j < W; j++) {
        if (C2[j].first == C2[0].first) {
          int y = C1[i].second;
          int x = C2[j].second;
          if (!st.count(make_pair(y, x))) {
            ok = true;
            break;
          }
        } else {
          break;
        }
      }
      if (ok) {
        break;
      }
    } else {
      break;
    }
  }
  if (ok) {
    cout << mx << endl;
  } else {
    cout << mx - 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int H, W, M;
  cin >> H >> W >> M;
  vector<int> h(M), w(M);
  for (int i = 0; i < M; i++) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
  }
  set<pair<int, int>> st;
  for (int i = 0; i < M; i++) {
    st.insert(make_pair(h[i], w[i]));
  }
  vector<int> c1(H, 0);
  vector<int> c2(W, 0);
  for (int i = 0; i < M; i++) {
    c1[h[i]]++;
    c2[w[i]]++;
  }
  vector<pair<int, int>> C1(H);
  for (int i = 0; i < H; i++) {
    C1[i] = make_pair(c1[i], i);
  }
  sort(C1.rbegin(), C1.rend());
  vector<pair<int, int>> C2(W);
  for (int i = 0; i < W; i++) {
    C2[i] = make_pair(c2[i], i);
  }
  sort(C2.rbegin(), C2.rend());
  int mx = C1[0].first + C2[0].first;
  bool ok = false;
  for (int i = 0; i < H; i++) {
    if (C1[i].first == C1[0].first) {
      for (int j = 0; j < W; j++) {
        if (C2[j].first == C2[0].first) {
          int y = C1[i].second;
          int x = C2[j].second;
          if (!st.count(make_pair(y, x))) {
            ok = true;
            break;
          }
        }
      }
      if (ok) {
        break;
      }
    }
  }
  if (ok) {
    cout << mx << endl;
  } else {
    cout << mx - 1 << endl;
  }
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define sc second
#define st first
#define pii pair<int, int>
#define arr array
#define task "A"
#define task_check "A"

const int maxn = 3e5 + 5;
const int maxN = 1e5 + 5;
const int mod = 1e9 + 7;

using namespace std;

/// Count : 1

int n, m;
ll row[maxn], col[maxn];

int main() {
  /*if(fopen(task".inp", "r")){
      freopen(task".inp", "r", stdin);
      //freopen(task".out", "w", stdout);
  }*/
  int t;
  cin >> n >> m >> t;
  map<ll, ll> have;
  for (int i = 1; i <= t; i++) {
    int u, v;
    cin >> u >> v;
    row[u]++;
    col[v]++;
    have[u * 300001 + v]++;
  }
  ll maxx = 0;
  ll ans = 0;
  vector<int> dx, dy;
  for (int i = 1; i <= n; i++) {
    if (row[i] > maxx) {
      maxx = row[i];
      dx.clear();
    }
    if (row[i] == maxx)
      dx.pb(i);
  }
  ans += maxx;
  maxx = 0;
  for (int i = 1; i <= m; i++) {
    if (col[i] > maxx) {
      maxx = col[i];
      dy.clear();
    }
    if (col[i] == maxx)
      dy.pb(i);
  }
  ans += maxx;
  for (auto j : dx) {
    for (auto j1 : dy) {
      if (!have.count(j * 300001 + j1)) {
        // cout << j <<" "<<j1<<"\\n";
        cout << ans;
        return 0;
      }
    }
  }
  cout << ans - 1;
}


Error: TLE

Fixed submission:

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define sc second
#define st first
#define pii pair<int, int>
#define arr array
#define task "A"
#define task_check "A"

const int maxn = 3e5 + 5;
const int maxN = 1e5 + 5;
const int mod = 1e9 + 7;

using namespace std;

/// Count : 1

int n, m;
ll row[maxn], col[maxn];

int main() {
  /*if(fopen(task".inp", "r")){
      freopen(task".inp", "r", stdin);
      //freopen(task".out", "w", stdout);
  }*/
  int t;
  cin >> n >> m >> t;
  map<int, map<int, int>> have;
  for (int i = 1; i <= t; i++) {
    int u, v;
    cin >> u >> v;
    row[u]++;
    col[v]++;
    have[u][v]++;
  }
  vector<pair<int, int>> s;
  ll maxx = 0;
  for (int i = 1; i <= m; i++) {
    if (col[i] > maxx) {
      maxx = col[i];
      s.clear();
    }
    s.pb({col[i], i});
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    for (auto j : s) {
      ans = max(ans, row[i] + j.st - (have[i][j.sc] > 0));
    }
  }
  cout << ans;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

// given n integers and an integer m such that n>m then there exits a pair
//(a-b) such that (a-b)%m==0

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll M = 1000000007;
void solve() {
  int r, c, m;
  cin >> r >> c >> m;
  int ans1 = 0, ans2 = 0, id1, id2;
  map<int, int> p, q;
  map<pair<int, int>, int> mp1;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    mp1[{x, y}] = -1;
    p[x]++;
    q[y]++;
    if (ans1 < p[x]) {
      ans1 = p[x], id1 = x;
    }
    if (ans2 < q[y]) {
      ans2 = q[y], id2 = y;
    }
  }
  vector<int> v1, v2;
  for (auto i : p) {
    if (i.second == ans1)
      v1.push_back(i.first);
  }
  for (auto i : q) {
    if (i.second == ans2)
      v2.push_back(i.first);
  }
  ll ans = ans1 + ans2 - 1;
  for (auto i : v1) {
    for (auto j : v2) {
      if (mp1[{i, j}] != -1) {
        ans++;
        cout << ans;
        return;
      }
    }
  }
  cout << ans;
}

int main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
    cout << endl;
  }
}


Error: 0

Fixed submission:

// given n integers and an integer m such that n>m then there exits a pair
//(a-b) such that (a-b)%m==0

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll M = 1000000007;
void solve() {
  int r, c, m;
  cin >> r >> c >> m;
  int a[r][c];
  int ans1 = 0, ans2 = 0, id1, id2;
  map<int, int> p, q;
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x >> y;
    a[x][y] = -1;
    p[x]++;
    q[y]++;
    if (ans1 < p[x]) {
      ans1 = p[x], id1 = x;
    }
    if (ans2 < q[y]) {
      ans2 = q[y], id2 = y;
    }
  }
  int ans = ans1 + ans2;
  if (a[id1][id2] == -1)
    ans = ans - 1;
  cout << ans;
}

int main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
    cout << endl;
  }
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> nh(h + 1), nw(w + 1);
  map<P, int> f;
  int mh = 0, mw = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    nh[a]++;
    nw[b]++;
    f[P(a, b)]++;
  }
  rep(i, h + 1) mh = max(nh[i], mh);
  rep(i, w + 1) mw = max(nw[i], mw);

  vector<int> x, y;

  for (int i = h; i >= 0; i--) {
    if (mh == nh[i])
      x.push_back(i);
  }
  for (int i = w; i >= 0; i--) {
    if (mw == nw[i])
      y.push_back(i);
  }

  int ans = mh + mw;
  rep(i, (int)x.size()) {
    rep(j, (int)y.size()) {
      if (f[P(x[i], y[j])] == 0) {
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << ans - 1 << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int h, w, m;
  cin >> h >> w >> m;
  vector<int> nh(h + 1), nw(w + 1);
  vector<vector<int>> f(h + 1, vector<int>(w + 1, 0));
  int mh = 0, mw = 0;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    nh[a]++;
    nw[b]++;
    f[a][b]++;
  }
  rep(i, h + 1) mh = max(nh[i], mh);
  rep(i, w + 1) mw = max(nw[i], mw);

  vector<int> x, y;

  for (int i = h; i >= 0; i--) {
    if (mh == nh[i])
      x.push_back(i);
  }
  for (int i = w; i >= 0; i--) {
    if (mw == nw[i])
      y.push_back(i);
  }

  int ans = mh + mw;
  rep(i, (int)x.size()) {
    rep(j, (int)y.size()) {
      if (f[x[i]][y[i]] == 0) {
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << ans - 1 << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  map<ll, ll> ma{}, maa{};
  set<pair<ll, ll>> s;
  vector<ll> va(300001, 0);
  vector<ll> vb(300001, 0);

  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    s.insert({x, y});
    ma[x]++;
    maa[y]++;
    va[x]++;
    vb[y]++;
  }
  vector<pair<int, int>> p;
  for (auto i = ma.begin(); i != ma.end(); ++i) {
    p.push_back(make_pair(i->second, i->first));
  }
  sort(p.rbegin(), p.rend());
  vector<ll> a, b;

  // first-個数 second-val
  ll mx;
  for (int i = 0; i < p.size(); ++i) {
    if (i != 0 && mx != p[i].first)
      break;
    if (i == 0)
      mx = p[i].first;
    a.push_back(p[i].second);
  }
  vector<pair<int, int>> pp;
  for (auto i = maa.begin(); i != maa.end(); ++i) {
    pp.push_back(make_pair(i->second, i->first));
  }
  sort(pp.rbegin(), pp.rend());
  ll mxx;
  // first-個数 second-val
  for (int i = 0; i < pp.size(); ++i) {
    if (i != 0 && mxx != pp[i].first)
      break;
    if (i == 0)
      mxx = pp[i].first;
    b.push_back(pp[i].second);
  }
  ans = mx + mxx;
  ll cnt = 0;
  for (auto au : s) {
    if (mx + mxx == va[au.first] + vb[au.second])
      cnt++;
  }
  if (cnt == a.size() * b.size())
    ans--;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = 0;

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  map<ll, ll> ma{}, maa{};
  set<pair<ll, ll>> s;
  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    s.insert({x, y});
    ma[x]++;
    maa[y]++;
  }
  vector<pair<int, int>> p;
  for (auto i = ma.begin(); i != ma.end(); ++i) {
    p.push_back(make_pair(i->second, i->first));
  }
  sort(p.rbegin(), p.rend());
  vector<ll> a, b;

  // first-個数 second-val
  ll mx;
  for (int i = 0; i < p.size(); ++i) {
    if (i != 0 && mx != p[i].first)
      break;
    if (i == 0)
      mx = p[i].first;
    a.push_back(p[i].second);
  }
  vector<pair<int, int>> pp;
  for (auto i = maa.begin(); i != maa.end(); ++i) {
    pp.push_back(make_pair(i->second, i->first));
  }
  sort(pp.rbegin(), pp.rend());
  ll mxx;
  // first-個数 second-val
  for (int i = 0; i < pp.size(); ++i) {
    if (i != 0 && mxx != pp[i].first)
      break;
    if (i == 0)
      mxx = pp[i].first;
    b.push_back(pp[i].second);
  }
  rep(i, a.size()) rep(j, b.size()) {
    ll te = mx + mxx;
    if (s.find({a[i], b[j]}) != s.end())
      te--;
    ans = max(ans, te);
  }
  cout << ans << endl;
  return 0;
}