Problem p00446 (C++)

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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> PP;

struct edge {
  int to, cost;
  edge(int t, int c) : to(t), cost(c) {}
};

int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int ddx[] = {1, 1, 1, 0, -1, -1, -1, 0}, ddy[] = {1, 0, -1, -1, -1, 0, 1, 1};

ll mypow(ll x, ll n, ll m) { // xのn乗をmで割った余り
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return mypow(x * x % m, n / 2, m);
  else
    return x * mypow(x, n - 1, m) % m;
}

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

int n;
vector<int> ta, ha;
set<int> st;
bool used[201];
int ct, ch;

void dfs(int now, int turn) {
  if (ct == 0) {
    cout << ch << endl << 0 << endl;
    return;
  }
  if (ch == 0) {
    cout << 0 << endl << ct << endl;
    return;
  }
  if (turn == 0) {
    bool flg = false;
    REP(i, ta.size()) {
      if (used[ta[i]])
        continue;
      if (ta[i] > now) {
        used[ta[i]] = true;
        flg = true;
        ct--;
        dfs(ta[i], 1);
        break;
      }
    }
    if (!flg) {
      dfs(0, 1);
    }
  } else {
    bool flg = false;
    REP(i, ha.size()) {
      if (used[ha[i]])
        continue;
      if (ha[i] > now) {
        used[ha[i]] = true;
        flg = true;
        ch--;
        dfs(ha[i], 0);
        break;
      }
    }
    if (!flg) {
      dfs(0, 0);
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    cin >> n;
    if (n == 0)
      break;
    REP(i, n) {
      int vv;
      cin >> vv;
      st.insert(vv);
    }
    FOR(i, 1, 2 * n + 1) {
      if (st.find(i) == st.end())
        ha.pb(i);
      else
        ta.pb(i);
    }
    ct = n, ch = n;
    dfs(0, 0);
  }

  return 0;
}


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

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x, n) bitset<n>(x)

typedef long long ll;
typedef pair<int, int> P;
typedef pair<P, int> PP;

struct edge {
  int to, cost;
  edge(int t, int c) : to(t), cost(c) {}
};

int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
int ddx[] = {1, 1, 1, 0, -1, -1, -1, 0}, ddy[] = {1, 0, -1, -1, -1, 0, 1, 1};

ll mypow(ll x, ll n, ll m) { // xのn乗をmで割った余り
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return mypow(x * x % m, n / 2, m);
  else
    return x * mypow(x, n - 1, m) % m;
}

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

int n;
vector<int> ta, ha;
set<int> st;
bool used[201];
int ct, ch;

void dfs(int now, int turn) {
  if (ct == 0) {
    cout << ch << endl << 0 << endl;
    return;
  }
  if (ch == 0) {
    cout << 0 << endl << ct << endl;
    return;
  }
  if (turn == 0) {
    bool flg = false;
    REP(i, ta.size()) {
      if (used[ta[i]])
        continue;
      if (ta[i] > now) {
        used[ta[i]] = true;
        flg = true;
        ct--;
        dfs(ta[i], 1);
        break;
      }
    }
    if (!flg) {
      dfs(0, 1);
    }
  } else {
    bool flg = false;
    REP(i, ha.size()) {
      if (used[ha[i]])
        continue;
      if (ha[i] > now) {
        used[ha[i]] = true;
        flg = true;
        ch--;
        dfs(ha[i], 0);
        break;
      }
    }
    if (!flg) {
      dfs(0, 0);
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    cin >> n;
    ta.clear(), ha.clear();
    st.clear();
    REP(i, 201) used[i] = false;
    ct = 0, ch = 0;
    if (n == 0)
      break;
    REP(i, n) {
      int vv;
      cin >> vv;
      st.insert(vv);
    }
    FOR(i, 1, 2 * n + 1) {
      if (st.find(i) == st.end())
        ha.pb(i);
      else
        ta.pb(i);
    }
    ct = n, ch = n;
    dfs(0, 0);
  }

  return 0;
}