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

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__ ",", __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000

map<int, double> memo;

double solve(int x, int y, int z, int turn, int joker) {
  if (x + y + z == 0) {
    if (joker == 1)
      return 0;
    else
      return 1;
  }
  int idx = (((x * 100 + y) * 100 + z) * 3 + turn) * 3 + joker;
  if (memo.count(idx) == 1)
    return memo[idx];
  //  dbg(x,y,z,turn,joker);
  double ans = 0;
  if (turn == 0) {
    if (x + y + (joker == 0) == 0)
      return solve(x, y, z, 1, joker);
    int total = x + z + (joker == 2);
    if (total == 0) {
      if (y == 0) {
        if (joker == 0)
          ans = 1;
        else
          ans = 0;
      } else
        ans = solve(0, y - 1, 0, 1, joker);
    } else {
      if (x > 0)
        ans += x * solve(x - 1, y, z, 1, joker);
      if (z > 0)
        ans += z * solve(x, y + 1, z - 1, 1, joker);
      if (joker == 2)
        ans += solve(x, y, z, 1, 0);
      ans /= (double)total;
    }
  } else if (turn == 1) {
    if (y + z + (joker == 1) == 0)
      return solve(x, y, z, 2, joker);
    int total = x + y + (joker == 0);
    if (total == 0) {
      if (z == 0) {
        if (joker == 1)
          ans = 0;
        else
          ans = 1;
      } else {
        if (joker == 1)
          ans = solve(0, 0, z - 1, 2, joker);
        else
          ans =
              (solve(0, 0, z, 2, 1) + z * solve(0, 0, z - 1, 2, 2)) / (z + 1.0);
      }
    } else {
      if (x > 0)
        ans += x * solve(x - 1, y, z + 1, 2, joker);
      if (y > 0)
        ans += y * solve(x, y - 1, z, 2, joker);
      if (joker == 0)
        ans += solve(x, y, z, 2, 1);
      ans /= (double)total;
    }
  } else if (turn == 2) {
    if (x + z + (joker == 2) == 0)
      return solve(x, y, z, 0, joker);
    if (y + z + (joker == 1) == 0)
      ans = 1;
    else if (z > 0)
      ans = solve(x, y, z - 1, 0, joker);
    else if (y > 0)
      ans = solve(x + 1, y - 1, z, 0, joker);
    else if (joker == 1)
      ans = solve(x, y, z, 0, 2);
  }
  //  else assert(false);
  return memo[idx] = ans;
}

int main() {
  int n;
  cin >> n;
  vector<int> vec(n, 0);
  int joker = -1;
  rep(i, 3) {
    int m;
    cin >> m;
    rep(j, m) {
      int d;
      cin >> d;
      if (d == 0)
        joker = i;
      else {
        d--;
        vec[d] ^= (1 << i);
      }
    }
  }
  //  assert(joker!=-1);

  int x = 0, y = 0, z = 0;
  rep(i, n) {
    if (vec[i] == 5)
      x++;
    else if (vec[i] == 3)
      y++;
    else if (vec[i] == 6)
      z++;
    //    else if(vec[i]!=0) assert(false);
  }

  printf("%.8f\\n", solve(x, y, z, 0, joker));

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__ ",", __VA_ARGS__)
void _dbg(string) { cout << endl; }
template <class H, class... T> void _dbg(string s, H h, T... t) {
  int l = s.find(',');
  cout << s.substr(0, l) << " = " << h << ", ";
  _dbg(s.substr(l + 1), t...);
}
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 1120000000

map<int, double> memo;
set<int> searching;

double solve(int x, int y, int z, int turn, int joker) {
  if (x + y + z == 0) {
    if (joker == 1)
      return 0;
    else
      return 1;
  }
  int idx = (((x * 100 + y) * 100 + z) * 3 + turn) * 3 + joker;
  if (memo.count(idx) == 1)
    return memo[idx];
  //  dbg(x,y,z,turn,joker);
  if (searching.count(idx)) {
    while (1) {
      puts("hoge");
    }
  }
  searching.insert(idx);
  double ans = 0;
  if (turn == 0) {
    int total = x + z + (joker == 2);
    if (total == 0) {
      if (y == 0) {
        if (joker == 0)
          ans = 1;
        else
          ans = 0;
      } else
        ans = solve(0, y - 1, 0, 1, joker);
    } else {
      if (x > 0)
        ans += x * solve(x - 1, y, z, 1, joker);
      if (z > 0)
        ans += z * solve(x, y + 1, z - 1, 1, joker);
      if (joker == 2)
        ans += solve(x, y, z, 1, 0);
      ans /= (double)total;
    }
  } else if (turn == 1) {
    int total = x + y + (joker == 0);
    if (total == 0) {
      if (z == 0) {
        if (joker == 1)
          ans = 0;
        else
          ans = 1;
      } else {
        if (joker == 1)
          ans = solve(0, 0, z - 1, 2, joker);
        else
          ans =
              (solve(0, 0, z, 2, 1) + z * solve(0, 0, z - 1, 2, 2)) / (z + 1.0);
      }
    } else {
      if (x > 0)
        ans += x * solve(x - 1, y, z + 1, 2, joker);
      if (y > 0)
        ans += y * solve(x, y - 1, z, 2, joker);
      if (joker == 0)
        ans += solve(x, y, z, 2, 1);
      ans /= (double)total;
    }
  } else if (turn == 2) {
    if (y + z + (joker == 1) == 0)
      ans = 1;
    else if (z > 0)
      ans = solve(x, y, z - 1, 0, joker);
    else if (y > 0)
      ans = solve(x + 1, y - 1, z, 0, joker);
    else if (joker == 1)
      ans = solve(x, y, z, 0, 2);
  }
  //  else assert(false);
  searching.erase(idx);
  return memo[idx] = ans;
}

int main() {
  int n;
  cin >> n;
  vector<int> vec(n, 0);
  int joker = -1;
  rep(i, 3) {
    int m;
    cin >> m;
    rep(j, m) {
      int d;
      cin >> d;
      if (d == 0)
        joker = i;
      else {
        d--;
        vec[d] ^= (1 << i);
      }
    }
  }
  //  assert(joker!=-1);

  int x = 0, y = 0, z = 0;
  rep(i, n) {
    if (vec[i] == 5)
      x++;
    else if (vec[i] == 3)
      y++;
    else if (vec[i] == 6)
      z++;
    //    else if(vec[i]!=0) assert(false);
  }

  printf("%.8f\\n", solve(x, y, z, 0, joker));

  return 0;
}