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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define ar array
#define db double
#define pow pw
const db pi = 3.14159265358979323846;
int pw(int a, int b) {
  int ans = 1;
  while (b) {
    if (b % 2)
      ans *= a;
    a *= a;
    b /= 2;
  }
  return (ans);
}
const int mxn = 3e5 + 7, INF = 1e9 + 7;
int m, n, k;
int r[mxn], c[mxn];
map<pair<int, int>, int> a;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> m >> n >> k;
  for (int i = 0; i < k; i++) {
    int u, v;
    cin >> u >> v;
    r[u]++;
    c[v]++;
    a[{u, v}] = 1;
  }
  int t1 = 0, t2 = 0, ans = 0;
  vector<int> d, d2;
  for (int i = 1; i <= m; i++)
    t1 = max(t1, r[i]);
  for (int i = 1; i <= m; i++)
    if (r[i] == t1)
      d.push_back(i);
  for (int i = 1; i <= n; i++)
    t2 = max(t2, c[i]);
  for (int i = 1; i <= n; i++)
    if (c[i] == t2)
      d2.push_back(i);
  for (int g : d)
    for (int h : d2) {
      if (a[{g, h}] == 0) {
        ans = max(ans, r[g] + c[h] - a[{g, h}]);
        cout << ans;
        return 0;
      } else
        ans = max(ans, r[g] + c[h] - a[{g, h}]);
    }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define ar array
#define db double
#define pow pw
const db pi = 3.14159265358979323846;
int pw(int a, int b) {
  int ans = 1;
  while (b) {
    if (b % 2)
      ans *= a;
    a *= a;
    b /= 2;
  }
  return (ans);
}
const int mxn = 3e5 + 7, INF = 1e9 + 7;
int m, n, k;
int r[mxn], c[mxn];
map<pair<int, int>, int> a;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> m >> n >> k;
  for (int i = 0; i < k; i++) {
    int u, v;
    cin >> u >> v;
    r[u]++;
    c[v]++;
    a[{u, v}] = 1;
  }
  int t1 = 0, t2 = 0, ans = 0;
  vector<int> d, d2;
  for (int i = 1; i <= m; i++)
    t1 = max(t1, r[i]);
  for (int i = 1; i <= m; i++)
    if (r[i] == t1)
      d.push_back(i);
  for (int i = 1; i <= n; i++)
    t2 = max(t2, c[i]);
  for (int i = 1; i <= n; i++)
    if (c[i] == t2)
      d2.push_back(i);
  for (int g : d)
    for (int h : d2) {
      ans = max(ans, r[g] + c[h] - a[{g, h}]);
    }
  cout << ans;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define INF 1e9 + 7
typedef long long int ll;
const int maxn = 300000;
ll mod = INF;
ll H, W, M;
ll r[maxn], c[maxn];
ll R[maxn] = {0}, C[maxn] = {0};
set<pair<int, int>> s;

int main() {

  cin >> H >> W >> M;
  for (int i = 0; i < M; i++) {
    cin >> r[i] >> c[i];
    r[i]--;
    c[i]--;
    R[r[i]]++;
    C[c[i]]++;
    s.insert(make_pair(r[i], c[i]));
  }

  ll rmax = 0, cmax = 0;
  for (int i = 0; i < H; i++) {
    rmax = max(rmax, R[i]);
  }
  for (int i = 0; i < W; i++) {
    cmax = max(cmax, C[i]);
  }
  vector<int> rnum, cnum;
  for (int i = 0; i < H; i++) {
    if (R[i] == rmax)
      rnum.push_back(i);
  }
  for (int i = 0; i < W; i++) {
    if (C[i] == cmax)
      cnum.push_back(i);
  }

  bool found = false;

  for (int i = 0; i < rnum.size(); i++) {
    for (int j = 0; j < cnum.size(); j++) {
      decltype(s)::iterator it = s.find(make_pair(rnum[i], cnum[j]));
      if (it == s.end()) {
        found = true;
        break;
      }
    }
    if (found)
      break;
  }

  if (!found)
    cout << rmax + cmax - 1 << endl;
  else
    cout << rmax + cmax << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define INF 1e9 + 7
typedef long long int ll;
const int maxn = 300000;
ll mod = INF;
ll H, W, M;
ll r[maxn], c[maxn];
ll R[maxn] = {0}, C[maxn] = {0};

int main() {

  cin >> H >> W >> M;
  for (int i = 0; i < M; i++) {
    cin >> r[i] >> c[i];
    r[i]--;
    c[i]--;
    R[r[i]]++;
    C[c[i]]++;
  }

  ll rmax = 0, cmax = 0;
  for (int i = 0; i < H; i++) {
    rmax = max(rmax, R[i]);
  }
  for (int i = 0; i < W; i++) {
    cmax = max(cmax, C[i]);
  }
  vector<int> rnum, cnum;
  for (int i = 0; i < H; i++) {
    if (R[i] == rmax)
      rnum.push_back(i);
  }
  for (int i = 0; i < W; i++) {
    if (C[i] == cmax)
      cnum.push_back(i);
  }

  // cout << "R and C"<<endl;

  // for(int i=0;i<H;i++){
  //     cout << R[i]<<" ";
  // }
  // cout << endl;
  // for(int i=0;i<W;i++){
  //     cout << C[i]<<" ";
  // }
  // cout << endl;

  bool found = false;

  // cout << "row collumn"<<endl;
  for (int i = 0; i < rnum.size(); i++) {
    for (int j = 0; j < cnum.size(); j++) {
      bool key = false;
      int a = rnum[i], b = cnum[j];
      // cout << a<< " "<<b<<endl;
      for (int k = 0; k < M; k++) {
        if (a == r[k] && b == c[k]) {
          key = true;
          break;
        }
        if (k == M - 1) {
          found = true;
          break;
        }
      }
      if (found)
        break;
    }
    if (found)
      break;
  }

  if (!found)
    cout << rmax + cmax - 1 << endl;
  else
    cout << rmax + cmax << endl;

  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
struct edge {
  ll to, cost;
};

int main() {
  int H, W;
  cin >> H >> W;
  int M;
  cin >> M;
  vector<int> h(M), w(M);
  REP(i, M) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
  }
  vector<ll> x(H, 0), y(W, 0);
  REP(i, M) {
    x[h[i]]++;
    y[w[i]]++;
  }
  ll ans = 0;
  ll max_x = *max_element(all(x));
  ll max_y = *max_element(all(y));
  vector<ll> p, q;
  REP(i, H) if (x[i] == max_x) p.push_back(i);
  REP(i, W) if (y[i] == max_y) q.push_back(i);
  ans = max_x + max_y;
  bool flag = false;
  ll cnt = 0;
  REP(i, M) {
    if (binary_search(all(p), h[i]) && binary_search(all(q), w[i])) {
      cnt++;
    }
  }
  if (p.size() * q.size() == cnt)
    cout << ans - 1 << endl;
  else
    cout << ans << endl;
}

Error: TLE

Fixed submission:

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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }
int char_to_int(char val) { return val - '0'; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
bool vector_finder(std::vector<ll> vec, int number) {
  bool f = binary_search(all(vec), number);
  if (f)
    return true;
  else
    return false;
}
struct edge {
  ll to, cost;
};

int main() {
  int H, W;
  cin >> H >> W;
  int M;
  cin >> M;
  vector<int> h(M), w(M);
  REP(i, M) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
  }
  vector<ll> x(H, 0), y(W, 0);
  REP(i, M) {
    x[h[i]]++;
    y[w[i]]++;
  }
  ll ans = 0;
  ll max_x = *max_element(all(x));
  ll max_y = *max_element(all(y));
  vector<ll> p, q;
  REP(i, H) if (x[i] == max_x) p.push_back(i);
  REP(i, W) if (y[i] == max_y) q.push_back(i);
  ans = max_x + max_y;
  bool flag = false;
  ll cnt = 0;
  REP(i, M) {
    if (vector_finder(p, h[i]) && vector_finder(q, w[i])) {
      cnt++;
    }
  }
  if (p.size() * q.size() == cnt)
    cout << ans - 1 << endl;
  else
    cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define ll long long int
#define endl '\\n'
#define INF 1000000000
#define MOD 1000000007
#define MAX 200001
#define pb push_back
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w, m, x, y;
  cin >> h >> w >> m;
  map<int, int> a, b;
  vector<set<int>> v1(h + 1);
  h = 0;
  w = 0;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    v1[x].insert(y);
    a[x]++;
    h = max(h, a[x]);
    b[y]++;
    w = max(w, b[y]);
  }
  x = h;
  y = w;
  vector<int> c1, c2;
  for (auto it = a.begin(); it != a.end(); it++) {
    if (it->second == x) {
      c1.pb(it->first);
    }
  }
  for (auto it = b.begin(); it != b.end(); it++) {
    if (it->second == y) {
      c2.pb(it->first);
    }
  }
  x = h + w - 1;
  bool f = 0;
  for (int i = 0; i < c1.size(); i++) {
    for (int j = 0; j < c2.size(); j++) {
      if (v1[c1[i]].find(c2[j]) == v1[c1[i]].end()) {
        x++;
        f = 1;
        break;
      }
    }
    if (f == 1)
      break;
  }
  cout << x << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define endl '\\n'
#define INF 1000000000
#define MOD 1000000007
#define MAX 200001
#define pb push_back
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int h, w, m, x, y;
  cin >> h >> w >> m;
  map<int, int> a, b;
  vector<set<int>> v1(h + 1);
  h = 0;
  w = 0;
  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    v1[x].insert(y);
    a[x]++;
    h = max(h, a[x]);
    b[y]++;
    w = max(w, b[y]);
  }
  x = h;
  y = w;
  vector<int> c1, c2;
  for (auto it = a.begin(); it != a.end(); it++) {
    if (it->second == x) {
      c1.pb(it->first);
    }
  }
  for (auto it = b.begin(); it != b.end(); it++) {
    if (it->second == y) {
      c2.pb(it->first);
    }
  }
  x = 0;
  for (int i = 0; i < c1.size(); i++) {
    for (int j = 0; j < c2.size(); j++) {
      if (v1[c1[i]].find(c2[j]) == v1[c1[i]].end()) {
        y = a[c1[i]] + b[c2[j]];
      } else {
        y = a[c1[i]] + b[c2[j]] - 1;
      }
      x = max(x, y);
    }
  }
  cout << x << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded


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

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  map<P, ll> v;
  vector<ll> H(h, 0);
  vector<ll> W(w, 0);
  vector<ll> H1;
  vector<ll> W1;

  ll maxh = 0;
  ll maxw = 0;
  rep(i, m) {
    P temp;

    cin >> temp.first >> temp.second;
    temp.first--;
    temp.second--;
    v[temp]++;
    H[temp.first]++;
    W[temp.second]++;
    maxh = max(maxh, H[temp.first]);
    maxw = max(maxw, W[temp.second]);
  }
  rep(i, h) {
    if (H[i] == maxh) {
      //    cout<<i<<endl;
      H1.push_back(i);
    }
  }
  rep(i, w) {
    if (W[i] == maxw) {
      W1.push_back(i);
      //  cout<<i<<endl;
    }
  }
  ll ans = maxh + maxw;
  //  cout<<maxh<<' '<<maxw<<endl;

  ll t = -1;
  if (H1.size() * W1.size() > m) {
    cout << ans;
    return 0;
  }
  for (ll i : H1)
    for (ll j : W1) {

      //  cout<<i<<' '<<j<<endl;
      if (v[make_pair(i, j)] <= 0) {
        t = 0;
      }
    }
  ans += t;
  cout << ans;
}


Error: TLE

Fixed submission:


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

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  map<P, ll> v;
  vector<ll> H(h, 0);
  vector<ll> W(w, 0);
  vector<ll> H1;
  vector<ll> W1;

  ll maxh = 0;
  ll maxw = 0;
  rep(i, m) {
    P temp;

    cin >> temp.first >> temp.second;
    temp.first--;
    temp.second--;
    v[temp]++;
    H[temp.first]++;
    W[temp.second]++;
    maxh = max(maxh, H[temp.first]);
    maxw = max(maxw, W[temp.second]);
  }
  rep(i, h) {
    if (H[i] == maxh) {
      //    cout<<i<<endl;
      H1.push_back(i);
    }
  }
  rep(i, w) {
    if (W[i] == maxw) {
      W1.push_back(i);
      //  cout<<i<<endl;
    }
  }
  ll ans = maxh + maxw;
  //  cout<<maxh<<' '<<maxw<<endl;

  ll t = -1;
  for (ll i : H1)
    for (ll j : W1) {
      //  cout<<i<<' '<<j<<endl;
      if (v[make_pair(i, j)] <= 0) {
        t = 0;
      }
    }
  ans += t;
  cout << ans;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int H, W, M;
  cin >> H >> W >> M;
  vector<int> cntH(H), cntW(W);
  vector<int> h(M), w(M);
  set<P> st;
  REP(i, M) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
    cntH[h[i]]++;
    cntW[w[i]]++;
    st.emplace(h[i], w[i]);
  }
  int maxH = 0, maxW = 0;
  REP(i, H) { maxH = max(maxH, cntH[i]); }
  REP(j, W) { maxW = max(maxW, cntW[j]); }
  set<int> vh, vw;
  int res = 0;
  REP(i, H) {
    if (cntH[i] == maxH) {
      vh.emplace(i);
    }
  }
  REP(j, W) {
    if (cntW[j] == maxW) {
      vw.emplace(j);
    }
  }
  vector<vector<int>> existW(H);
  REP(i, M) { existW[h[i]].emplace_back(w[i]); }
  res = 0;
  for (auto &a : vh) {
    int cnt = 0;
    for (auto &b : existW[a]) {
      cnt += vw.count(b);
    }
    res = max(res, maxH + maxW - (cnt == vw.size()));
  }
  cout << res << endl;

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int H, W, M;
  cin >> H >> W >> M;
  vector<int> cntH(H), cntW(W);
  vector<int> h(M), w(M);
  set<P> st;
  REP(i, M) {
    cin >> h[i] >> w[i];
    h[i]--;
    w[i]--;
    cntH[h[i]]++;
    cntW[w[i]]++;
    st.emplace(h[i], w[i]);
  }
  int maxH = 0, maxW = 0;
  REP(i, H) { maxH = max(maxH, cntH[i]); }
  REP(j, W) { maxW = max(maxW, cntW[j]); }
  set<int> vh, vw;
  int res = 0;
  REP(i, H) {
    if (cntH[i] == maxH) {
      vh.emplace(cntH[i]);
    }
  }
  REP(j, W) {
    if (cntW[j] == maxW) {
      vw.emplace(cntW[j]);
    }
  }
  res = 0;
  REP(i, W) {
    if (vh.count(h[i])) {
      if (vw.count(w[i])) {
        res = max(res, maxH + maxW - 1);
      } else
        res = max(res, maxH + maxW);
    }
  }
  cout << res << endl;

  return 0;
}
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;
  map<pair<int, int>, int> baku;
  map<int, int> bh;
  map<int, int> bw;
  map<int, int> bhmax;
  map<int, int> bwmax;
  map<int, int> bh_flag;
  map<int, int> bw_flag;
  int tmp1, tmp2;
  for (int i = 0; i < m; i++) {
    cin >> tmp1 >> tmp2;
    baku[{tmp1, tmp2}] = 1;
    if (bh.count(tmp1))
      bh.at(tmp1)++;
    else
      bh[tmp1] = 1;
    if (bw.count(tmp2))
      bw.at(tmp2)++;
    else
      bw[tmp2] = 1;
  }
  int ansh = 0, answ = 0;
  for (auto b : baku) {
    if (bh_flag.count(b.first.first))
      continue;
    if (ansh < bh.at(b.first.first)) {
      ansh = bh.at(b.first.first);
    }
    bh_flag[b.first.first] = 1;
  }
  vector<int> bhh;
  vector<int> bww;
  for (auto b : baku) {
    if (bh.at(b.first.first) == ansh) {
      bhmax[b.first.first] = 1;
    }
  }
  for (auto b : baku) {
    if (bw_flag.count(b.first.second))
      continue;
    if (answ < bw.at(b.first.second)) {
      answ = bw.at(b.first.second);
    }
    bw_flag[b.first.second] = 1;
  }
  for (auto b : baku) {
    if (bw.at(b.first.second) == answ) {
      bwmax[b.first.second] = 1;
    }
  }
  int flag = -1;
  int count = 0;
  for (auto bhi : bhmax) {
    for (auto bwi : bwmax) {
      count++;
      if (!baku.count({bhi.first, bwi.first}) || count > m) {
        flag = 0;
        break;
      }
    }
  }
  cout << ansh + answ + flag << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w, m;
  cin >> h >> w >> m;
  map<pair<int, int>, int> baku;
  map<int, int> bh;
  map<int, int> bw;
  map<int, int> bh_flag;
  map<int, int> bw_flag;
  int tmp1, tmp2;
  for (int i = 0; i < m; i++) {
    cin >> tmp1 >> tmp2;
    baku[{tmp1, tmp2}] = 1;
    if (bh.count(tmp1))
      bh.at(tmp1)++;
    else
      bh[tmp1] = 1;
    if (bw.count(tmp2))
      bw.at(tmp2)++;
    else
      bw[tmp2] = 1;
  }
  int ansh = 0, answ = 0;
  for (auto b : baku) {
    if (bh_flag.count(b.first.first))
      continue;
    if (ansh < bh.at(b.first.first)) {
      ansh = bh.at(b.first.first);
    }
    bh_flag[b.first.first] = 1;
  }
  vector<int> bhh;
  vector<int> bww;
  for (auto b : baku) {
    if (bh.at(b.first.first) == ansh) {
      bhh.push_back(b.first.first);
    }
  }
  int bhhsize = bhh.size();
  for (auto b : baku) {
    if (bw_flag.count(b.first.second))
      continue;
    if (answ < bw.at(b.first.second)) {
      answ = bw.at(b.first.second);
    }
    bw_flag[b.first.second] = 1;
  }
  for (auto b : baku) {
    if (bw.at(b.first.second) == answ) {
      bww.push_back(b.first.second);
    }
  }
  int flag = -1;
  int sizea = bhh.size();
  int sizeb = bww.size();
  for (int i = 0; i < sizea; i++) {
    for (int j = 0; j < sizeb; j++) {
      if (!baku.count({bhh.at(i), bww.at(j)})) {
        flag = 0;
        break;
      }
    }
  }
  cout << ansh + answ + flag << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
const int MAXN = 3e5 + 5;
int cnt1[MAXN], cnt2[MAXN];
map<pair<int, int>, int> mp;
int main() {
  int H, W, M;
  scanf("%d %d %d", &H, &W, &M);
  int h, w;
  vector<pair<int, int>> node;
  for (int i = 1; i <= M; i++) {
    scanf("%d %d", &h, &w);
    node.push_back({h, w});
    cnt1[h]++;
    cnt2[w]++;
    mp[{h, w}]++;
  }
  int mx1 = 0, mx2 = 0;
  for (int i = 1; i <= H; i++)
    mx1 = max(mx1, cnt1[i]);
  for (int i = 1; i <= W; i++)
    mx2 = max(mx2, cnt2[i]);

  int vc1 = 0, vc2 = 0;
  for (int i = 1; i <= H; i++)
    if (cnt1[i] == mx1)
      vc1++;
  for (int i = 1; i <= W; i++)
    if (mx2 == cnt2[i])
      vc2++;
  int numnode = 0;
  for (auto vv : node) {
    if (cnt1[vv.first] == mx1 && cnt2[vv.second] == mx2) {
      numnode++;
    }
  }
  if (numnode == (long long)vc1 * vc2)
    printf("%d\\n", mx1 + mx2 - 1);
  else
    printf("%d\\n", mx1 + mx2);
  // int T;
  // cin >> T;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
const int MAXN = 3e5 + 5;
int cnt1[MAXN], cnt2[MAXN];
map<pair<int, int>, int> mp;
int main() {
  int H, W, M;
  scanf("%d %d %d", &H, &W, &M);
  int h, w;

  for (int i = 1; i <= M; i++) {
    scanf("%d %d", &h, &w);
    cnt1[h]++;
    cnt2[w]++;
    mp[{h, w}]++;
  }
  int mx1 = 0, mx2 = 0;
  for (int i = 1; i <= H; i++)
    mx1 = max(mx1, cnt1[i]);
  for (int i = 1; i <= W; i++)
    mx2 = max(mx2, cnt2[i]);
  vector<int> vc1, vc2;
  for (int i = 1; i <= H; i++)
    if (cnt1[i] == mx1)
      vc1.push_back(i);
  for (int i = 1; i <= W; i++)
    if (mx2 == cnt2[i])
      vc2.push_back(i);
  int ans = 0;
  for (auto v1 : vc1) {
    for (auto v2 : vc2) {
      if (mp.count({v1, v2}))
        ans = max(ans, cnt1[v1] + cnt2[v2] - 1);
      else
        ans = max(ans, cnt1[v1] + cnt2[v2]);
    }
  }
  printf("%d\\n", ans);
  // int T;
  // cin >> T;
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<pair<ll, ll>> b(m);
  // vector<vector<int>> bomb(h,vector<int>(w,0));
  vector<ll> height(h, 0);
  vector<ll> width(w, 0);
  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    // b[i] = make_pair(x,y);
    height[x]++;
    width[y]++;
    b[i] = make_pair(x, y);
  }

  ll maxh = 0, maxw = 0;
  rep(i, h) { maxh = max(maxh, height[i]); }
  rep(i, w) { maxw = max(maxw, width[i]); }

  ll hnum = 0, wnum = 0;
  rep(i, h) {
    if (height[i] == maxh) {
      hnum++;
    }
  }
  rep(i, w) {
    if (width[i] == maxw) {
      wnum++;
    }
  }

  ll cnt = 0;
  for (auto p : b) {
    ll x, y;
    tie(x, y) = p;
    if (height[x] == maxh && width[y] == maxw)
      cnt++;
  }

  if (hnum * wnum == cnt) {
    cout << (maxh + maxw - 1) << endl;
  } else {
    cout << (maxh + maxw) << endl;
  }

  // cout << maxi << " " << maxj << endl;
  // cout << maxh << " " << maxw << endl;

  return (0);
}

Error: 0

Fixed submission:

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

int main() {
  ll h, w, m;
  cin >> h >> w >> m;
  vector<pair<ll, ll>> b(m);
  // vector<vector<int>> bomb(h,vector<int>(w,0));
  vector<ll> height(h, 0);
  vector<ll> width(w, 0);
  rep(i, m) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    // b[i] = make_pair(x,y);
    height[x]++;
    width[y]++;
    b[i] = make_pair(x, y);
  }

  ll maxh = 0, maxw = 0;
  rep(i, h) { maxh = max(maxh, height[i]); }
  rep(i, w) { maxw = max(maxw, width[i]); }

  vector<ll> hlist, wlist;
  rep(i, h) {
    if (height[i] == maxh) {
      hlist.push_back(i);
    }
  }
  rep(i, w) {
    if (width[i] == maxw) {
      wlist.push_back(i);
    }
  }

  vector<pair<ll, ll>> list;

  for (auto x : hlist) {
    for (auto y : wlist) {
      list.push_back(make_pair(x, y));
    }
  }

  for (auto p : list) {
    auto res = find(b.begin(), b.end(), p);
    if (res == b.end()) {
      cout << (maxh + maxw) << endl;
      return (0);
    }
  }

  cout << (maxh + maxw - 1) << endl;

  // cout << maxi << " " << maxj << endl;
  // cout << maxh << " " << maxw << endl;

  return (0);
}