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

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#ifdef DEBUG
void debug() { cerr << "\\n"; }
template <class T> void debug(const T &x) { cerr << x << "\\n"; }
template <class T, class... Args> void debug(const T &x, const Args &...args) {
  cerr << x << " ";
  debug(args...);
}
template <class T> void debugVector(const vector<T> &v) {
  for (const T &x : v) {
    cerr << x << " ";
  }
  cerr << "\\n";
}
#else
template <class T, class... Args> void debug(const T &x, const Args &...args) {}
template <class T> void debugVector(const vector<T> &v) {}
#endif

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

void solve() {
  int N;
  cin >> N;
  vector<ll> a(N);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  string s;
  cin >> s;
  reverse(ALL(a));
  reverse(ALL(s));
  vector<ll> base;
  for (int i = 0; i < N; i++) {
    ll v = a[i];
    sort(RALL(base));
    for (ll e : base) {
      chmin(v, v ^ e);
    }
    if (v) {
      if (s[i] == '0') {
        base.emplace_back(v);
      } else {
        cout << 1 << "\\n";
        return;
      }
    }
  }
  cout << 0 << "\\n";
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    solve();
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
void print() { cout << "\\n"; }
template <class T> void print(const T &x) { cout << x << "\\n"; }
template <class T, class... Args> void print(const T &x, const Args &...args) {
  cout << x << " ";
  print(args...);
}
template <class T> void printVector(const vector<T> &v) {
  for (const T &x : v) {
    cout << x << " ";
  }
  cout << "\\n";
}
using ll = long long;

#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
const double PI = acos(-1);
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

ll cnt;

bool f(int i, ll x, string &s, vector<ll> &a, vector<map<ll, bool>> &mp) {
  cnt++;
  int n = a.size();
  if (i == n) {
    return (x == 0);
  }
  if (mp[i].count(x)) {
    return mp[i][x];
  }
  bool f1, f2;
  f1 = f(i + 1, x, s, a, mp);
  f2 = f(i + 1, x ^ a[i], s, a, mp);
  if (s[i] == '0') {
    return mp[i][x] = (f1 || f2);
  } else {
    return mp[i][x] = (!f1 || !f2);
  }
}

void solve() {
  int n;
  cin >> n;
  vector<ll> a(n);
  string s;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  cin >> s;
  if (s.back() == '1') {
    print(1);
    return;
  }
  vector<map<ll, bool>> mp(n);
  cout << f(0, 0, s, a, mp) << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}