alexjercan/bugnet huggingface dataset:
Problem p00426 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int n, m, ans, pw;
vector<int> cup[3];
void move(int k, int t) {
  if (k > n)
    return;
  int pk, o;
  rep(i, 3) if (!cup[i].empty() && cup[i][cup[i].size() - 1] == k) pk = i;
  rep(i, 3) if (i != pk && i != t) o = i;
  cup[pk].pop_back();
  if (pk == t) {
    move(k + 1, t);
    cup[pk].push_back(k);
    return;
  }
  ans++;
  if (abs(pk - t) == 1) {
    move(k + 1, o);
    cup[t].push_back(k);
  } else {
    move(k + 1, t);
    cup[o].push_back(k);
  }
  move(k, t);
}
int main() {
  while (cin >> n >> m, n) {
    rep(i, 3) cup[i].clear();
    rep(i, 3) {
      int k, l;
      cin >> k;
      rep(j, k) cin >> l, cup[i].push_back(l);
      reverse(cup[i].begin(), cup[i].end());
    }
    ans = 0;
    pw = 1;
    rep(i, n) pw *= 3;
    move(1, 0);
    ans = min(ans, pw - 1 - ans);
    cout << (ans < 0 || ans > m ? -1 : ans) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define is insert
using namespace std;
typedef vector<vector<int>> st;
int main() {
  int n, m, k, t;
  while (cin >> n >> m, n) {
    st s(3);
    vector<st> F;
    set<st> V;
    rep(i, 3) {
      cin >> k;
      rep(j, k) cin >> t, s[i].pb(t);
    }
    F.pb(s);
    V.is(s);
    if ((s[0].empty() || s[2].empty()) && s[1].empty()) {
      cout << 0 << endl;
      continue;
    }
    int step = 0;
    while (!F.empty() && step++ < m) {
      vector<st> nF;
      rep(i, F.size()) rep(tray, 3) rep(d, 2) {
        st cs = F[i], ns;
        if (cs[tray].empty())
          continue;
        int nt = tray + (d ? 1 : -1);
        if (0 <= nt && nt < 3 &&
            cs[tray][cs[tray].size() - 1] >
                (cs[nt].empty() ? 0 : cs[nt][cs[nt].size() - 1])) {
          ns = cs;
          ns[nt].pb(cs[tray][cs[tray].size() - 1]);
          ns[tray].erase(ns[tray].end() - 1);
          if (V.find(ns) == V.end()) {
            nF.pb(ns), V.is(ns);
            if ((ns[0].empty() || ns[2].empty()) && ns[1].empty()) {
              cout << step << endl;
              goto NEXT;
            }
          }
        }
      }
      F = nF;
    }
    cout << -1 << endl;
  NEXT:;
  }
  return 0;
}
Problem p00426 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <stdlib.h>
/*http://climpet.hatenablog.com/entry/2013/01/01/225142#f5を参考*/
int main(int argc, char *argv[]) {
  int pow3[16], c[16];
  int n, m, i, j, k, d, x, y, p;

  pow3[0] = 1;
  for (i = 1; i <= 15; i++) {
    pow3[i] = pow3[i - 1] *
              3; /*i枚#0から#2または#2から#0へ移動するのに必要なステップ数*/
  }

  while (scanf("%d%d", &n, &m), n) {
    for (i = 0; i < 3; ++i) {
      for (scanf("%d", &j); j > 0; --j) {
        scanf("%d", &k);
        c[n - k] =
            i; /*番号付け替え,ハノイの塔に変換。大きいコップを小さい円盤にする*/
      }
    }
    p = 0; // 全ての円盤が0の位置にあると仮定する
    x = 0;
    for (i = n - 1; i >= 0; --i) { //
      d = abs(p - c[i]); // 移動させる円盤の距離を求める。
      /*n = 3の時、i =
       * 2で円盤2を#1に移動させるには円盤1と円盤0を#2に移動させる*/
      x += pow3[i] * d;
      if (d == 1) {
        p = 2 - p; /*反対側に移動,距離がd =
                      1ならば0~i-1番の円盤を反対側に移動する。*/
      }
    }
    y = pow3[n] - 1 - x;
    if (x > y) {
      x = y;
    }
    printf("%d\\n", x > m ? -1 : x);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <stdlib.h>
/*http://climpet.hatenablog.com/entry/2013/01/01/225142#f5を参考*/
int main(int argc, char *argv[]) {
  int pow3[16], c[16];
  int n, m, i, j, k, d, x, y, p;

  pow3[0] = 1;
  for (i = 1; i <= 15; i++) {
    pow3[i] = pow3[i - 1] *
              3; /*i枚#0から#2または#2から#0へ移動するのに必要なステップ数*/
  }

  while (scanf("%d%d", &n, &m), n) {
    for (i = 0; i < 3; --i) {
      for (scanf("%d", &j); j > 0; --j) {
        scanf("%d", &k);
        c[n - k] =
            i; /*番号付け替え,ハノイの塔に変換。大きいコップを小さい円盤にする*/
      }
    }
    p = 0; // 全ての円盤が0の位置にあると仮定する
    x = 0;
    for (i = n - 1; i >= 0; i--) { //
      d = abs(p - c[i]); // 移動させる円盤の距離を求める。
      /*n = 3の時、i =
       * 2で円盤2を#1に移動させるには円盤1と円盤0を#2に移動させる*/
      x += pow3[i] * d;
      if (d == 1) {
        p = 2 - p; /*反対側に移動,距離がd =
                      1ならば0~i-1番の円盤を反対側に移動する。*/
      }
    }
    y = pow3[n] - 1 - x;
    if (x > y) {
      x = y;
    }
    printf("%d\\n", x > m ? -1 : x);
  }
  return 0;
}
Problem p00426 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int mp[15000000];
int Hash(string s) {
  int cnt = 0, t = 1;
  rep(i, s.size()) {
    cnt += (int)(s[i] - '0') * t;
    t *= 3;
  }
  return cnt;
}
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    memset(mp, 0, sizeof(mp));
    string in = "000000000000000";
    rep(i, 3) {
      int len;
      scanf("%d", &len);
      rep(j, len) {
        int a;
        scanf("%d", &a);
        in[--a] = i + '0';
      }
    }
    queue<string> que;
    mp[Hash(in)] = 1;
    que.push(in);
    while (!que.empty()) {
      string u = que.front();
      que.pop();
      if (count(u.begin(), u.begin() + n, '0') == n ||
          count(u.begin(), u.begin() + n, '2') == n) {
        printf("%d\\n", mp[Hash(u)] - 1);
        goto g;
      }
      if (mp[Hash(u)] > m)
        continue;
      rep(k, 2) rep(t, 2) {
        int i, j;
        if (t) {
          i = k, j = k + 1;
        } else {
          i = k + 1;
          j = k;
        }
        int fi = (int)u.rfind(i + '0', n - 1),
            fj = (int)u.rfind(j + '0', n - 1);
        if (fi == -1 || fi < fj)
          continue;
        string v = u;
        v[fi] = j + '0';
        if (!mp[Hash(v)]) {
          mp[Hash(v)] = mp[Hash(u)] + 1;
          que.push(v);
        }
      }
    }
    puts("-1");
  g:;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int mp[5000000];
int Hash(string s) {
  int cnt = 0, t = 1;
  rep(i, s.size()) {
    cnt += (int)(s[i] - '0') * t;
    t *= 3;
  }
  return cnt;
}
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    memset(mp, 0, sizeof(mp));
    string in = "000000000000000";
    rep(i, 3) {
      int len;
      scanf("%d", &len);
      rep(j, len) {
        int a;
        scanf("%d", &a);
        in[--a] = i + '0';
      }
    }
    queue<string> que;
    mp[Hash(in)] = 1;
    que.push(in);
    while (!que.empty()) {
      string u = que.front();
      que.pop();
      if (count(u.begin(), u.begin() + n, '0') == n ||
          count(u.begin(), u.begin() + n, '2') == n) {
        printf("%d\\n", mp[Hash(u)] - 1);
        goto g;
      }
      if (mp[Hash(u)] > m)
        continue;
      rep(k, 2) rep(t, 2) {
        int i, j;
        if (t) {
          i = k, j = k + 1;
        } else {
          i = k + 1;
          j = k;
        }
        int fi = (int)u.rfind(i + '0', n - 1),
            fj = (int)u.rfind(j + '0', n - 1);
        if (fi == -1 || fi < fj)
          continue;
        string v = u;
        v[fi] = j + '0';
        if (!mp[Hash(v)]) {
          mp[Hash(v)] = mp[Hash(u)] + 1;
          que.push(v);
        }
      }
    }
    puts("-1");
  g:;
  }
}
Problem p00426 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)

using namespace std;

int calc(vector<int> cup, int m) {
  int cnt = 0;
  while (1) {
    int i = cnt % 2, j = i + 1;
    int p = 1;
    while (p <= max(cup[i], cup[j]))
      p <<= 1;
    p >>= 1;
    if (!p)
      break;
    cup[i] ^= p;
    cup[j] ^= p;
    cnt++;
    if (cnt > m)
      return cnt;
  }

  return cnt;
}

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<int> cup(3);
    loop(3, i) {
      int c;
      cin >> c;
      while (c--) {
        int v;
        cin >> v;
        cup[i] |= 1 << v--;
      }
    }
    int ans = calc(cup, m);
    swap(cup[0], cup[2]);
    ans = min(ans, calc(cup, m));
    cout << (ans <= m ? ans : -1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)

using namespace std;

int calc(int a, int b, int c) {
  if (!a && !b)
    return 0;
  if (a & 1)
    return calc(a >> 1, b >> 1, c >> 1) + 2 + 2 * calc((a | b | c) >> 1, 0, 0);
  if (b & 1)
    return calc(c >> 1, b >> 1, a >> 1) + 1 + calc((a | b | c) >> 1, 0, 0);
  return calc(a >> 1, b >> 1, c >> 1);
}

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    int cup[3] = {};
    loop(3, i) {
      int c;
      cin >> c;
      while (c--) {
        int v;
        cin >> v;
        cup[i] |= 1 << v--;
      }
    }
    int ans = min(calc(cup[0], cup[1], cup[2]), calc(cup[2], cup[1], cup[0]));
    cout << (ans <= m ? ans : -1) << endl;
  }
  return 0;
}
Problem p00426 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)

using namespace std;

int calc(vector<int> cup, int m) {
  int cnt = 0;
  while (1) {
    int i = cnt % 2, j = i + 1;
    int p = 1;
    while (p <= max(cup[i], cup[j]))
      p <<= 1;
    p >>= 1;
    if (!p)
      break;
    cup[i] ^= p;
    cup[j] ^= p;
    cnt++;
    if (cnt > m)
      return cnt;
  }

  return cnt;
}

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<int> cup(3);
    loop(3, i) {
      int c;
      cin >> c;
      while (c--) {
        int v;
        cin >> v;
        cup[i] |= 1 << v + 1;
      }
    }
    int ans = calc(cup, m);
    swap(cup[0], cup[2]);
    ans = min(ans, calc(cup, m));
    cout << (ans <= m ? ans : -1) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)

using namespace std;

int bg(int cup) {
  int ans = 0;
  loop(20, i) if (cup & 1 << i) ans = i;
  return ans;
}

int calc(vector<int> cup, int m, int prev) {
  if (m < 0)
    return -1;
  if (!cup[0] && !cup[1] || !cup[1] && !cup[2])
    return m;

  int i, j;
  if (!prev)
    i = 1, j = 2;
  else
    i = 0, j = 1;

  if (bg(cup[i]) && bg(cup[i]) > bg(cup[j])) {
    cup[j] ^= 1 << bg(cup[i]);
    cup[i] ^= 1 << bg(cup[i]);
    return calc(cup, m - 1, !prev);
  } else if (bg(cup[j]) && bg(cup[j]) > bg(cup[i])) {
    cup[i] ^= 1 << bg(cup[j]);
    cup[j] ^= 1 << bg(cup[j]);
    return calc(cup, m - 1, !prev);
  }
  return -1;
}

int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<int> cup(3);
    loop(3, i) {
      int c;
      cin >> c;
      while (c--) {
        int v;
        cin >> v;
        cup[i] |= 1 << v;
      }
    }
    int ans = max({calc(cup, m, 0), calc(cup, m, 1)});
    cout << (ans < 0 ? -1 : m - ans) << endl;
  }
  return 0;
}
Problem p00426 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;

typedef pair<int, int> P;
bool use[15000000];

int main() {
  int n, m, now;

  for (;;) {
    cin >> n >> m;
    if (!n && !m)
      break;

    now = 0;
    for (int i = 0; i < 3; i++) {
      int num;
      cin >> num;
      for (int j = 0; j < num; j++) {
        int tmp;
        cin >> tmp;
        now += pow(3, tmp - 1) * i;
      }
    }

    int lim = pow(3, n);
    for (int i = 0; i < lim; i++)
      use[i] = false;
    use[now] = true;

    int ans = -1;

    queue<P> q;
    q.push(P(now, 0));

    while (q.size()) {
      P p = q.front();
      q.pop();
      stack<int> s[3];
      now = p.first;
      // cout << now << endl;
      for (int i = 1; i <= n; i++) {
        s[now % 3].push(i);
        now /= 3;
      }

      if ((int)s[0].size() == n || (int)s[2].size() == n) {
        ans = p.second;
        break;
      }
      if (p.second > m)
        break;

      int d1[] = {0, 1, 1, 2}, d2[] = {1, 0, 2, 1};

      for (int i = 0; i < 4; i++) {
        if (s[d1[i]].size() &&
            (s[d2[i]].empty() || s[d1[i]].top() > s[d2[i]].top())) {
          int hoge = pow(3, s[d1[i]].top() - 1);
          now = p.first - hoge * d1[i] + hoge * d2[i];
          if (!use[now]) {
            use[now] = true;
            q.push(P(now, p.second + 1));
          }
        }
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <stack>
using namespace std;

int n, m, now;
stack<int> s[3];
bool use[15000000];

int rec(int time) {
  int res = -1, tmp, val;
  if ((int)s[0].size() == n || (int)s[2].size() == n)
    return time;
  if (time > m)
    return -1;
  int d1[] = {0, 1, 1, 2}, d2[] = {1, 0, 2, 1};

  for (int i = 0; i < 4; i++) {
    // cout << time << " " << i << " " << s[0].size() << " " << s[1].size() <<
    // endl;
    if (s[d1[i]].size() &&
        (s[d2[i]].empty() || s[d1[i]].top() > s[d2[i]].top())) {
      tmp = s[d1[i]].top();
      int hoge = pow(3, tmp - 1);
      now -= hoge * d1[i] - hoge * d2[i];
      // cout << now << endl;
      if (!use[now]) {
        use[now] = true;
        s[d1[i]].pop();
        s[d2[i]].push(tmp);

        val = rec(time + 1);
        if (res < 0)
          res = val;
        else if (val >= 0)
          res = min(res, val);

        s[d2[i]].pop();
        s[d1[i]].push(tmp);
        use[now] = false;
      }
      now += hoge * d1[i] - hoge * d2[i];
    }
  }
  return res;
}

int main() {
  for (;;) {
    cin >> n >> m;
    if (!n && !m)
      break;

    now = 0;
    for (int i = 0; i < 3; i++) {
      while (s[i].size())
        s[i].pop();
      int num;
      cin >> num;
      for (int j = 0; j < num; j++) {
        int tmp;
        cin >> tmp;
        s[i].push(tmp);
        now += pow(3, tmp - 1) * i;
      }
    }

    int lim = pow(3, n);
    for (int i = 0; i < lim; i++)
      use[i] = false;
    use[now] = true;

    cout << rec(0) << endl;
  }
}
Problem p00426 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;

int n, m;

int solve(int A, int B, int C) {
  if (!A && !B)
    return 0;
  if (C & 1)
    return solve(A >> 1, B >> 1, C >> 1);
  if (B & 1)
    return solve(C >> 1, B >> 1, A >> 1) + solve((A | B | C) >> 1, 0, 0) + 1;
  if (A & 1)
    return solve(A >> 1, B >> 1, C >> 1) + 2 * solve((A | B | C) >> 1, 0, 0) +
           2;
}

int main() {
  while (cin >> n >> m, n || m) {
    int cup[3] = {0};
    for (int i = 0; i < 3; i++) {
      int k, e;
      cin >> k;
      for (int j = 0; j < k; j++) {
        cin >> e;
        cup[i] |= 1 << e - 1;
      }
    }
    int ans = min(solve(cup[0], cup[1], cup[2]), solve(cup[2], cup[1], cup[0]));
    if (m < ans)
      ans = -1;
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define V vector<int>
using namespace std;

int main() {
  int n, m, k, kk, ans;
  while (cin >> n >> m, (n || m)) {
    V tower(3, 0);
    ans = ((1 << (n)) - 1);
    for (int i = 0; i < 3; i++) {
      cin >> k;
      for (int j = 0; j < k; j++) {
        cin >> kk;
        tower[i] += (1 << (kk - 1));
      }
    }
    map<V, int> done;
    queue<V> Q;
    done[tower] = 0;
    Q.push(tower);
    while (Q.size()) {
      V q = Q.front();
      Q.pop();
      if (done[q] > m)
        break;
      bool f = 0;
      for (int i = 0; i < 3; i++)
        if (i != 1 && q[i] == ans)
          f = 1;
      if (f) {
        cout << done[q] << endl;
        goto end;
      }
      for (int i = 0; i < 2; i++) {
        V next = q;
        int a = q[i], b = q[i + 1];
        for (int j = n; j >= 0; j--) {
          if ((a & (1 << j)) || (b & (1 << j))) {
            if (a > b)
              a -= (1 << j), b += (1 << j);
            else
              a += (1 << j), b -= (1 << j);
            break;
          }
        }
        next[i] = a, next[i + 1] = b;
        if (!done.count(next)) {
          done[next] = done[q] + 1;
          Q.push(next);
        }
      }
    }
    cout << "-1\\n";
  end:;
  }
}
Problem p00426 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>

using namespace std;

int calc(int *_a, int n, int i, int j) {
  int a[3] = {_a[0], _a[1], _a[2]};
  for (int res = 0;; res++) {
    if (i == 0)
      i++, j++;
    else
      i--, j--;
    if (a[0] == (1 << n) - 1 || a[2] == (1 << n) - 1)
      return res;
    int bit = a[i] && a[j] ? min(a[i] & -a[i], a[j] & -a[j])
                           : max(a[i] & -a[i], a[j] & -a[j]);
    a[i] ^= bit;
    a[j] ^= bit;
  }
  return -1;
}

int main() {
  for (int n, m; cin >> n >> m, n | m;) {
    int a[3] = {};
    for (int i = 0; i < 3; i++) {
      int j;
      cin >> j;
      while (j--) {
        int x;
        cin >> x;
        a[i] |= 1 << n - x;
      }
    }

    if (a[0] == (1 << n) - 1 || a[2] == (1 << n) - 1) {
      cout << 0 << endl;
      continue;
    }

    int res = m + 1;
    for (int i = 0, j = 1; i < 2; i++, j++) {
      int bit = a[i] && a[j] ? min(a[i] & -a[i], a[j] & -a[j])
                             : max(a[i] & -a[i], a[j] & -a[j]);
      a[i] ^= bit;
      a[i + 1] ^= bit;
      res = min(res, 1 + calc(a, n, i, i + 1));
      a[i] ^= bit;
      a[i + 1] ^= bit;
    }
    cout << (res <= m ? res : -1) << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <set>

using namespace std;

typedef pair<vector<int>, int> State;

string tobin(int n) {
  string s;
  for (int i = 15; i--;)
    s += '0' + (n >> i & 1);
  return s;
}

int main() {
  for (int n, m; cin >> n >> m, n | m;) {
    vector<int> a(3);
    for (int i = 0; i < 3; i++) {
      int j;
      cin >> j;
      while (j--) {
        int x;
        cin >> x;
        a[i] |= 1 << (n - x);
      }
    }

    queue<pair<vector<int>, int>> q;
    q.push(make_pair(a, 0));
    set<vector<int>> vis;
    int res = -1;
    while (q.size()) {
      vector<int> ca = q.front().first;
      int cc = q.front().second;
      q.pop();
      if (vis.find(ca) != vis.end())
        continue;
      vis.insert(ca);

      if (ca[0] == (1 << n) - 1 || ca[2] == (1 << n) - 1) {
        res = cc;
        break;
      }

      for (int i = 0, j = 1; i < 2; i++, j++) {
        int bit = ca[i] && ca[j] ? min(ca[i] & -ca[i], ca[j] & -ca[j])
                                 : max(ca[i] & -ca[i], ca[j] & -ca[j]);
        ca[i] ^= bit;
        ca[j] ^= bit;
        q.push(make_pair(ca, cc + 1));
        ca[i] ^= bit;
        ca[j] ^= bit;
      }
    }

    cout << (res <= m ? res : -1) << endl;
  }

  return 0;
}
Problem p00426 (C++) — buggy submission, verdict: Memory Limit Exceeded

// 40
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>

using namespace std;

struct S {
  int t;
  int c[3];
  bool operator<(S a) const {
    return lexicographical_compare(c, c + 3, a.c, a.c + 3);
  }
};

int cv(int *a) {
  int r[3] = {};
  for (int j = 0; j < 15; j++) {
    for (int i = 0; i < 3; i++) {
      r[i] = (r[i] * 3) + (a[i] >> j & 1);
    }
  }
  return r[0] * 2 + r[1];
}

int main() {
  for (int n, m; cin >> n >> m, n | m;) {
    S is = {0, {0, 0, 0}};
    for (int i = 0; i < 3; i++) {
      int k;
      cin >> k;
      while (k--) {
        int c;
        cin >> c;
        is.c[i] |= 1 << (n - c);
      }
    }
    queue<S> que;
    que.push(is);
    static bool p[14348907] = {};
    fill_n(p, 14348907, false);
    while (!que.empty() && que.front().t <= m) {
      S c = que.front();
      if (c.c[0] == (1 << n) - 1 || c.c[2] == (1 << n) - 1)
        break;
      que.pop();
      if (p[cv(c.c)]++)
        continue;
      for (int i = 0; i < 3; i++) {
        for (int d = -1; d <= 1; d += 2) {
          int nd = i + d;
          int l = c.c[i] & ~(c.c[i] - 1);
          if (0 <= nd && nd < 3 && c.c[i] &&
              (c.c[nd] == 0 || l < (c.c[nd] & ~(c.c[nd] - 1)))) {
            S n = c;
            n.c[nd] |= l;
            n.c[i] &= ~l;
            n.t++;
            que.push(n);
          }
        }
      }
    }
    if (!que.empty() && que.front().t <= m) {
      cout << que.front().t << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}


Error: MLE

Fixed submission:

// 40
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>

using namespace std;

struct S {
  int t;
  int c[3];
  bool operator<(S a) const {
    return lexicographical_compare(c, c + 3, a.c, a.c + 3);
  }
};

int main() {
  for (int n, m; cin >> n >> m, n | m;) {
    S is = {0, {0, 0, 0}};
    for (int i = 0; i < 3; i++) {
      int k;
      cin >> k;
      while (k--) {
        int c;
        cin >> c;
        is.c[i] |= 1 << (n - c);
      }
    }
    queue<S> que;
    que.push(is);
    set<S> p;
    while (!que.empty() && que.front().t <= m) {
      S c = que.front();
      if (c.c[0] == (1 << n) - 1 || c.c[2] == (1 << n) - 1)
        break;
      que.pop();
      if (p.count(c))
        continue;
      p.insert(c);
      for (int i = 0; i < 3; i++) {
        for (int d = -1; d <= 1; d += 2) {
          int nd = i + d;
          int l = c.c[i] & ~(c.c[i] - 1);
          if (0 <= nd && nd < 3 && c.c[i] &&
              (c.c[nd] == 0 || l < (c.c[nd] & ~(c.c[nd] - 1)))) {
            S n = c;
            n.c[nd] |= l;
            n.c[i] &= ~l;
            n.t++;
            que.push(n);
          }
        }
      }
    }
    if (!que.empty() && que.front().t <= m) {
      cout << que.front().t << endl;
    } else {
      cout << -1 << endl;
    }
  }
  return 0;
}
Problem p00427 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#define rep(i, s, n) for (int i = s; i < n; i++)
#define Z 10005
using namespace std;
int d[Z + 1];
int main() {
  int n, m, k, r, l;
  while (true) {
    cin >> n >> k >> m >> r;
    if (!(n + m + k + r))
      break;
    rep(i, 0, Z) d[i] = 0;
    k = 1, l = n;
    rep(i, 0, Z) d[i] += k / l, k %= l, k *= 10;
    if (m)
      rep(i, 1, n) {
        k = 1, l = i * n;
        rep(j, 0, Z) d[j] += k / l, k %= l, k *= 10;
      }
    for (int i = Z; i > 0; i--)
      d[i - 1] += d[i] / 10, d[i] %= 10;
    cout << d[0] << ".";
    rep(i, 1, r + 1) cout << d[i];
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#define rep(i, s, n) for (int i = s; i < n; i++)
#define Z 100001
using namespace std;
int d[Z + 1];
int main() {
  int n, m, k, r, l;
  while (true) {
    cin >> n >> k >> m >> r;
    if (!(n + m + k + r))
      break;
    rep(i, 0, Z) d[i] = 0;
    k = 1, l = n;
    rep(i, 0, Z) d[i] += k / l, k %= l, k *= 10;
    if (m)
      rep(i, 1, n) {
        k = 1, l = i * n;
        rep(j, 0, Z) d[j] += k / l, k %= l, k *= 10;
      }
    for (int i = Z; i > 0; i--)
      d[i - 1] += d[i] / 10, d[i] %= 10;
    cout << d[0] << ".";
    rep(i, 1, r + 1) cout << d[i];
    cout << endl;
  }
  return 0;
}