Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define fi(a, b) for (int i = a; i < b; i++)
#define fj(a, b) for (int j = a; j < b; j++)
#define ff first
#define ss second
#define ll long long
#define ld long double
#define ull unsigned long long
#define bp(x) __builtin_popcount(x)
#define pr(x)                                                                  \
  for (auto it : x)                                                            \
    cout << it << " ";                                                         \
  cout << endl;
#define endl "\n"
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef vector<long long> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vll;

void nikal_pehli_fursat_mai() {
  int n, m, w;
  cin >> n >> m >> w;
  vi x(n, 0), y(m, 0);
  map<pii, int> arr;
  int mx = 0, my = 0;
  vi v1, v2;
  fi(0, w) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    arr[{u, v}]++;
    x[u]++;
    y[v]++;
    mx = max(mx, x[u]);
    my = max(my, y[v]);
  }
  fi(0, n) {
    if (mx == x[i])
      v1.push_back(i);
  }
  fi(0, m) {
    if (my == y[i])
      v2.push_back(i);
  }
  int ans = 0;
  fi(0, v1.size()) {
    fj(0, v2.size()) { ans = max(ans, mx + my - arr[{v1[i], v2[j]}]); }
  }
  cout << ans << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  int tc = 1;
  // cin>>tc;
  while (tc--) {
    nikal_pehli_fursat_mai();
  }
}


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

#define fi(a, b) for (int i = a; i < b; i++)
#define fj(a, b) for (int j = a; j < b; j++)
#define ff first
#define ss second
#define ll long long
#define ld long double
#define ull unsigned long long
#define bp(x) __builtin_popcount(x)
#define pr(x)                                                                  \
  for (auto it : x)                                                            \
    cout << it << " ";                                                         \
  cout << endl;
#define endl "\n"
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
typedef vector<long long> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vll;

void nikal_pehli_fursat_mai() {
  int n, m, w;
  cin >> n >> m >> w;
  vi x(n, 0), y(m, 0);
  map<pii, int> arr;
  int mx = 0, my = 0;
  vi v1, v2;
  fi(0, w) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    arr[{u, v}]++;
    x[u]++;
    y[v]++;
    mx = max(mx, x[u]);
    my = max(my, y[v]);
  }
  fi(0, n) {
    if (mx == x[i])
      v1.push_back(i);
  }
  fi(0, m) {
    if (my == y[i])
      v2.push_back(i);
  }
  int ans = 0;
  fi(0, v1.size()) {
    fj(0, v2.size()) {
      if (arr[{v1[i], v2[j]}]) {
        ans = mx + my - 1;
      } else {
        ans = mx + my;
        cout << ans << endl;
        return;
      }
    }
  }
  cout << ans << endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  int tc = 1;
  // cin>>tc;
  while (tc--) {
    nikal_pehli_fursat_mai();
  }
}