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

// Deep Godhani
// DAIICT

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

#define N 100000000000
#define PI acos(-1)
#define INF LONG_LONG_MAX
#define NINF LONG_LONG_MIN
#define ll long long int
#define ld long double
#define precision(x, d) cout << fixed << setprecision(d) << x
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \\
  srand(time(NULL))

map<pair<ll, ll>, ll> check;
map<pair<ll, ll>, ll> dp;

ll func(vector<pair<ll, ll>> &r, vector<pair<ll, ll>> &c, ll rstart,
        ll cstart) {
  ll rowCount = r[rstart].first;
  ll colCount = c[cstart].first;

  ll x = r[rstart].second;
  ll y = c[cstart].second;
  ll m = 0;

  if (dp[{rstart, cstart}] != 0) {
    return dp[{rstart, cstart}];
  }
  if (check[{x, y}] == 1) {
    if (rstart + 1 < (ll)r.size())
      m = max(m, dp[{rstart + 1, cstart}] = func(r, c, rstart + 1, cstart));
    if (cstart + 1 < (ll)c.size())
      m = max(m, dp[{rstart, cstart + 1}] = func(r, c, rstart, cstart + 1));
  } else {
    return dp[{rstart, cstart}] = rowCount + colCount;
  }

  return dp[{rstart, cstart}] = max(rowCount + colCount - 1, m);
}

void solve() {
  ll h, w, m;
  cin >> h >> w >> m;

  vector<pair<ll, ll>> v;
  check.clear();
  dp.clear();
  for (int i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    v.push_back({x, y});
    check[{x, y}] = 1;
  }

  map<ll, ll> row, col;
  for (int i = 0; i < (ll)v.size(); i++) {
    row[v[i].first]++;
    col[v[i].second]++;
  }

  vector<pair<ll, ll>> r, c;
  for (auto it = row.begin(); it != row.end(); it++) {
    r.push_back({it->second, it->first});
  }

  for (auto it = col.begin(); it != col.end(); it++) {
    c.push_back({it->second, it->first});
  }

  sort(r.begin(), r.end(), greater<pair<ll, ll>>());
  sort(c.begin(), c.end(), greater<pair<ll, ll>>());

  ll ans = func(r, c, 0, 0);
  cout << ans << endl;
}

int main() {
  FAST;
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}


Error: TLE

Fixed submission:

// Deep Godhani
// DAIICT

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

#define N 100000000000
#define PI acos(-1)
#define INF LONG_LONG_MAX
#define NINF LONG_LONG_MIN
#define ll long long int
#define ld long double
#define precision(x, d) cout << fixed << setprecision(d) << x
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);                   \\
  srand(time(NULL))

map<pair<ll, ll>, ll> check;

ll func(vector<pair<ll, ll>> &r, vector<pair<ll, ll>> &c, ll rstart,
        ll cstart) {
  ll rowCount = r[rstart].first;
  ll colCount = c[cstart].first;

  ll x = r[rstart].second;
  ll y = c[cstart].second;
  ll m = 0;

  if (check[{x, y}] == 1) {
    if (rstart + 1 < (ll)r.size())
      m = max(m, func(r, c, rstart + 1, cstart));
    if (cstart + 1 < (ll)c.size())
      m = max(m, func(r, c, rstart, cstart + 1));
  } else {
    return rowCount + colCount;
  }

  return max(rowCount + colCount - 1, m);
}

void solve() {
  ll h, w, m;
  cin >> h >> w >> m;

  vector<pair<ll, ll>> v;
  check.clear();
  for (int i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    v.push_back({x, y});
    check[{x, y}] = 1;
  }

  map<ll, ll> row, col;
  for (int i = 0; i < (ll)v.size(); i++) {
    row[v[i].first]++;
    col[v[i].second]++;
  }

  vector<pair<ll, ll>> r, c;
  for (auto it = row.begin(); it != row.end(); it++) {
    r.push_back({it->second, it->first});
  }

  for (auto it = col.begin(); it != col.end(); it++) {
    c.push_back({it->second, it->first});
  }

  sort(r.begin(), r.end(), greater<pair<ll, ll>>());
  sort(c.begin(), c.end(), greater<pair<ll, ll>>());

  ll ans = func(r, c, 0, 0);
  cout << ans << endl;
}

int main() {
  FAST;
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}