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

// Romene

#include <bits/stdc++.h>

#include <string>

#include <sstream>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define f first
#define s second
#define all(c) c.begin(), c.end()
#define ulli unsigned long long int
#define lli long long int
#define llv vector<ll>
#define llc vector<char>
#define lls vector<string>
#define llb vector<bool>
#define mp make_pair
#define endl "\\n"
#define debg cout << "HI" << endl;
#define ins insert
#define foo(i, n) for (ll i = 0; i < n; i++)
#define foa(i, a) for (ll i = 0; i < a.size(); i++)
#define foe(i, a, b) for (ll i = a; i <= b; i++)
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define INF 2147483647
#define len length
#define sp setprecision(16)
#define mod 1000000007

ll powr(ll b, ll m) {
  ll ans = 1;
  while (m > 0) {
    if (m % 2 == 1) {
      ans = (ans * b) % mod;
    }
    b = (b * b) % mod;
    m = m / 2;
  }
  return ans;
}
ll gcd1(ll a, ll b) {
  if (a == 0)
    return b;
  else {
    return gcd1(b % a, a);
  }
}
ll lcm(ll a, ll b) { return (a * b) / gcd1(a, b); }
ll mod1(ll a) {
  if (a < 0)
    return -1 * a;
  else {
    return a;
  }
}

bool isPrime(int num) {
  if (num == 1)
    return 0;
  for (int i = 2; i * i <= (num); i++) {
    if (num % i == 0) {
      return false;
      break;
    }
  }

  return true;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int f1 = 1;

  for (int y3 = 0; y3 < f1; y3++) {
    int n, m, t;
    cin >> n >> m >> t;
    vector<ll> v, v1, a(n, 0), b(m, 0);
    map<pair<ll, ll>, ll> mp1;
    ll a1, b1, max1 = 0, max2 = 0;
    for (int i = 0; i < t; i++) {
      cin >> a1 >> b1;
      a[a1 - 1]++;
      b[b1 - 1]++;
      mp1[{a1 - 1, b1 - 1}] = 1ll;
      max1 = max(max1, a[a1 - 1]);
      max2 = max(max2, b[b1 - 1]);
    }

    for (int i = 0; i < n; i++) {
      if (max1 == a[i]) {
        v.pb(i);
      }
    }

    for (int i = 0; i < m; i++) {
      if (max2 == b[i]) {
        v1.pb(i);
      }
    }
    ll ans = 0, flag = 0;
    for (int i = 0; i < m; i++) {
      ans = max(ans, a[v[0]] + b[i] - mp1[{v[0], i}]);
    }
    for (int i = 0; i < n; i++) {
      ans = max(ans, b[v1[0]] + a[i] - mp1[{i, v1[0]}]);
    }
    cout << ans;
  }

  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}


Error: TLE

Fixed submission:

// Romene

#include <bits/stdc++.h>

#include <string>

#include <sstream>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define f first
#define s second
#define all(c) c.begin(), c.end()
#define ulli unsigned long long int
#define lli long long int
#define llv vector<ll>
#define llc vector<char>
#define lls vector<string>
#define llb vector<bool>
#define mp make_pair
#define endl "\\n"
#define debg cout << "HI" << endl;
#define ins insert
#define foo(i, n) for (ll i = 0; i < n; i++)
#define foa(i, a) for (ll i = 0; i < a.size(); i++)
#define foe(i, a, b) for (ll i = a; i <= b; i++)
#define max3(a, b, c) max(max(a, b), c)
#define min3(a, b, c) min(min(a, b), c)
#define INF 2147483647
#define len length
#define sp setprecision(16)
#define mod 1000000007

ll powr(ll b, ll m) {
  ll ans = 1;
  while (m > 0) {
    if (m % 2 == 1) {
      ans = (ans * b) % mod;
    }
    b = (b * b) % mod;
    m = m / 2;
  }
  return ans;
}
ll gcd1(ll a, ll b) {
  if (a == 0)
    return b;
  else {
    return gcd1(b % a, a);
  }
}
ll lcm(ll a, ll b) { return (a * b) / gcd1(a, b); }
ll mod1(ll a) {
  if (a < 0)
    return -1 * a;
  else {
    return a;
  }
}

bool isPrime(int num) {
  if (num == 1)
    return 0;
  for (int i = 2; i * i <= (num); i++) {
    if (num % i == 0) {
      return false;
      break;
    }
  }

  return true;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int f1 = 1;

  for (int y3 = 0; y3 < f1; y3++) {
    int n, m, t;
    cin >> n >> m >> t;
    vector<ll> v, v1, a(n, 0), b(m, 0);
    map<pair<ll, ll>, ll> mp1;
    ll a1, b1, max1 = 0, max2 = 0;
    for (int i = 0; i < t; i++) {
      cin >> a1 >> b1;
      a[a1 - 1]++;
      b[b1 - 1]++;
      mp1[{a1 - 1, b1 - 1}] = 1ll;
      max1 = max(max1, a[a1 - 1]);
      max2 = max(max2, b[b1 - 1]);
    }

    for (int i = 0; i < n; i++) {
      if (max1 == a[i]) {
        v.pb(i);
      }
    }

    for (int i = 0; i < m; i++) {
      if (max2 == b[i]) {
        v1.pb(i);
      }
    }
    ll ans = 0, flag = 0;
    /// cout << v1[0];
    if (t == m * n)
      ans = t;
    else {
      for (int i = 0; i < v.size(); i++) {
        ll count;
        for (int j = 0; j < v1.size(); j++) {
          count = a[v[i]] + b[v1[j]];

          if (mp1[{v[i], v1[j]}]) {
            count--;
          }

          ans = max(ans, count);
          if (count == t)
            break;
        }
      }
    }
    cout << ans;
  }

  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}