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

#include <algorithm>
#include <iostream>
#include <queue>

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

using namespace std;

typedef vector<int> V;
typedef vector<V> VV;

const int inf = 2e8;

int n, m, s;
int d[14348907];

V seq() {
  V v(n);
  rep(i, n) { v[i] = i; }
  return v;
}

int pack(const VV &v) {
  V p(n);
  rep(i, 3) {
    for (int x : v[i]) {
      p[x] = i;
    }
  }

  int k = 0;
  for (int i = n - 1; i >= 0; --i) {
    k *= 3;
    k += p[i];
  }
  return k;
}
VV unpack(int k) {
  VV v(3, V());
  rep(i, n) {
    v[k % 3].push_back(i);
    k /= 3;
  }
  return v;
}

VV read() {
  VV v(3, V());
  rep(i, 3) {
    int k;
    cin >> k;
    rep(j, k) {
      int x;
      cin >> x;
      v[i].push_back(x - 1);
    }
  }
  return v;
}

VV swap_cup(VV v, int a, int b) {
  if (!v[a].empty() || !v[b].empty()) {
    if (!v[a].empty() && !v[b].empty()) {
      if (v[a].back() > v[b].back()) {
        swap(a, b);
      }
    } else if (v[b].empty()) {
      swap(a, b);
    }
    v[a].push_back(v[b].back());
    v[b].pop_back();
  }
  return v;
}

int main() {
  while (1) {
    cin >> n >> m;
    if (!n) {
      break;
    }
    s = pack(read());

    int f = pack({seq(), {}, {}});
    int g = pack({{}, {}, seq()});

    fill_n(d, sizeof(d) / sizeof(d[0]), inf);

    queue<int> q;
    d[s] = 0;
    q.push(s);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      VV k = unpack(v);
      bool end = false;
      rep(i, 2) {
        int t = pack(swap_cup(k, 1, i * 2));
        if (d[t] == inf) {
          d[t] = d[v] + 1;
          q.push(t);
          if (t == f || t == g) {
            end = true;
            break;
          }
        }
      }
      if (end) {
        break;
      }
    }

    int ans = min(d[f], d[g]);
    cout << (ans <= m ? ans : -1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>

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

using namespace std;

typedef vector<int> V;
typedef vector<V> VV;

const int inf = 2e8;

int n, m, s;
int d[14348907];

V seq() {
  V v(n);
  rep(i, n) { v[i] = i; }
  return v;
}

int pack(const VV &v) {
  V p(n);
  rep(i, 3) {
    for (int x : v[i]) {
      p[x] = i;
    }
  }

  int k = 0;
  for (int i = n - 1; i >= 0; --i) {
    k *= 3;
    k += p[i];
  }
  return k;
}
VV unpack(int k) {
  VV v(3, V());
  rep(i, n) {
    v[k % 3].push_back(i);
    k /= 3;
  }
  return v;
}

VV read() {
  VV v(3, V());
  rep(i, 3) {
    int k;
    cin >> k;
    rep(j, k) {
      int x;
      cin >> x;
      v[i].push_back(x - 1);
    }
  }
  return v;
}

VV swap_cup(VV v, int a, int b) {
  if (!v[a].empty() || !v[b].empty()) {
    if (!v[a].empty() && !v[b].empty()) {
      if (v[a].back() > v[b].back()) {
        swap(a, b);
      }
    } else if (v[b].empty()) {
      swap(a, b);
    }
    v[a].push_back(v[b].back());
    v[b].pop_back();
  }
  return v;
}

int main() {
  while (1) {
    cin >> n >> m;
    if (!n) {
      break;
    }
    s = pack(read());

    fill_n(d, sizeof(d) / sizeof(d[0]), inf);

    queue<int> q;
    d[s] = 0;
    q.push(s);
    while (!q.empty()) {
      int v = q.front();
      q.pop();
      VV k = unpack(v);
      rep(i, 2) {
        int t = pack(swap_cup(k, 1, i * 2));
        if (d[t] == inf) {
          d[t] = d[v] + 1;
          q.push(t);
        }
      }
    }

    int ans = min(d[pack({seq(), {}, {}})], d[pack({{}, {}, seq()})]);
    cout << (ans <= m ? ans : -1) << endl;
  }
  return 0;
}