Problem p00426 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
#define AB 1
#define BC 3
#define INF 1 << 29

using namespace std;

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

int toA(int, int, int);
int toC(int, int, int);

int toA(int A, int B, int C) {
  if (!B && !C)
    return 0;

  if (A & 1) {
    return toA(A >> 1, B >> 1, C >> 1);
  } // end if
  if (B & 1) {
    return toC(A >> 1, B >> 1, C >> 1) + 1 + toA(0, 0, (A | B | C) >> 1);
  } // end if
  if (C & 1) {
    return toA(A >> 1, B >> 1, C >> 1) + 1 + toC((A | B | C) >> 1, 0, 0) + 1 +
           toA(0, 0, (A | B | C) >> 1);
  } // end if
}

int toC(int A, int B, int C) {
  if (!A && !B)
    return 0;

  if (C & 1) {
    return toC(A >> 1, B >> 1, C >> 1);
  } // end if
  if (B & 1) {
    return toA(A >> 1, B >> 1, C >> 1) + 1 + toC((A | B | C) >> 1, 0, 0);
  } // end if
  if (A & 1) {
    return toC(A >> 1, B >> 1, C >> 1) + 1 + toA(0, 0, (A | B | C) >> 1) + 1 +
           toC((A | B | C) >> 1, 0, 0);
  } // end if
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.cup", "r", stdin );

  int n, m;
  while (scanf("%d %d", &n, &m), n, m) {
    int T[3] = {0};
    rep(i, 3) {
      int in;
      scanf("%d", &in);
      while (in--) {
        int cup;
        scanf("%d", &cup);
        T[i] |= 1 << (cup - 1);
      } // end rep
    }   // end rep

    int res = min(toA(T[0], T[1], T[2]), toC(T[0], T[1], T[2]));
    cout << (res > m ? -1 : res) << endl;
  } // end loop

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>   // require fabs
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit atof atoi
#include <cstring> // require memset
#include <ctime>   // require srand
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
#define AB 1
#define BC 3
#define INF 1 << 30

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
int n, m;

stack<int> T[3];

bool is_success(void) {
  return (T[1].top() == 0 && (T[0].top() == 0 || T[2].top() == 0));
}

int dfs(int curr, int prev) {
  int ans = INF;

  if (curr > m) {
    return ans;
  } // end if

  if (is_success()) {
    ans = min(ans, curr);
    return ans;
  } // end if

  rep(i, 3) {
    if (T[i].top() == 0)
      continue;
    int cup = T[i].top();
    T[i].pop();
    if (i == 0 && prev != AB) {
      if (cup > T[1].top()) {
        T[1].push(cup);
        ans = min(ans, dfs(curr + 1, AB));
        T[1].pop();
      } // end if
    }   // end if
    if (i == 2 && prev != BC) {
      if (cup > T[1].top()) {
        T[1].push(cup);
        ans = min(ans, dfs(curr + 1, BC));
        T[1].pop();
      } // end if
    }   // end if
    if (i == 1) {
      if (prev != AB && (cup > T[0].top())) {
        T[0].push(cup);
        ans = min(ans, dfs(curr + 1, AB));
        T[0].pop();
      } // end if
      if (prev != BC && (cup > T[2].top())) {
        T[2].push(cup);
        ans = min(ans, dfs(curr + 1, BC));
        T[2].pop();
      } // end if
    }   // end if
    T[i].push(cup);
  } // end rep

  return ans;
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.cup", "r", stdin );
  while (scanf("%d %d", &n, &m), n, m) {
    rep(i, 3) {
      T[i] = stack<int>();
      T[i].push(0);
    } // end rep

    rep(i, 3) {
      int in;
      scanf("%d", &in);
      while (in--) {
        int cup;
        scanf("%d", &cup);
        T[i].push(cup);
      } // end rep
    }   // end rep

    int res = min(dfs(0, AB), dfs(0, BC));
    if (res == INF) {
      cout << -1 << endl;
    } else {
      cout << res << endl;
    } // end if
  }   // end loop

  return 0;
}