Problem p03444 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define dump(x) cerr << #x << " = " << (x) << endl
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

const int maxn = 2010;

V<int> g[maxn];
bool one[maxn];
int a[maxn], dep[maxn];

int dfs(int v, int d) {
  int num = 0;
  dep[v] = d;
  for (int to : g[v]) {
    int res = dfs(to, d + 1);
    num += res;
  }
  if (num == 0 && a[v] != v)
    ++num;
  if (num == 1)
    one[v] = 1;
  return num;
}

int main() {
  int N;
  cin >> N;
  V<int> p(N, -1);

  for (int i = 1; i < N; ++i) {
    cin >> p[i];
    g[p[i]].pb(i);
  }

  rep(i, N) cin >> a[i];

  V<int> ope;

  auto rot = [&](int v) {
    ope.pb(v);
    V<int> vec;
    while (v != -1) {
      vec.pb(v);
      v = p[v];
    }
    reverse(ALL(vec));
    int sz = vec.size();

    int x = a[0];
    rep(i, sz - 1) { a[vec[i]] = a[vec[i + 1]]; }
    a[vec.back()] = x;

    reverse(ALL(vec));
    for (int v : vec) {
      bool t = (a[v] != v);
      for (auto to : g[v]) {
        if (one[to])
          t = 1;
      }
      if (!t) {
        one[v] = 0;
      }
    }
  };

  while (true) {
    bool ok = 1;
    rep(i, N) if (i != a[i]) ok = 0;
    if (ok) {
      break;
    }

    memset(one, 0, sizeof(one));
    dfs(0, 0);

    V<int> cmp(N, -1);
    V<int> la(N, -1);
    {
      V<int> cand(N, 1);
      rep(i, N) cand[i] = one[i];
      rep(i, N) if (one[i] && p[i] != -1) cand[p[i]] = 0;
      int now = 0;
      rep(i, N) if (cand[i]) {
        int v = i;
        la[now] = v;
        while (v != -1 && one[v]) {
          cmp[v] = now;
          v = p[v];
        }
        now++;
      }
    }

    V<int> used(N, -1);
    rep(i, N) if (one[i]) used[a[i]] = 0;

    while (true) {
      {
        bool en = 1;
        rep(i, N) if (one[i]) { en = 0; }
        if (en)
          break;
      }
      int x = a[0];
      if (used[x] == 1)
        break;
      if (one[x]) {
        int v = la[cmp[x]];
        dump(v);
        while (v != -1) {
          int av = a[v];
          if (used[av] != 1 ||
              (cmp[av] != cmp[x] || cmp[av] == cmp[x] && dep[av] < dep[x])) {
            used[x] = 1;
            rot(v);
            break;
          }
          v = p[v];
        }
      } else {
        int mdep = -1, cand = -1;
        rep(i, N) {
          int x = a[i];
          if (one[x] && used[x] != 1) {
            if (mdep < dep[i]) {
              mdep = dep[i];
              cand = i;
            }
          }
        }
        if (cand == -1)
          assert(false);
        rot(cand);
      }
    }
  }

  cout << ope.size() << endl;
  for (int x : ope) {
    cout << x << endl;
  }

  return 0;
}

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

typedef pair<int, int> pii;
typedef long long ll;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (int i = m; i < (n); i++)
#define ALL(c) (c).begin(), (c).end()
#define dump(x) cerr << #x << " = " << (x) << endl
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }

template <class T, class U> void chmin(T &t, const U &u) {
  if (t > u)
    t = u;
}
template <class T, class U> void chmax(T &t, const U &u) {
  if (t < u)
    t = u;
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  rep(i, v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

const int maxn = 2010;

V<int> g[maxn];
bool one[maxn];
int a[maxn], dep[maxn];

int dfs(int v, int d) {
  int num = 0;
  dep[v] = d;
  for (int to : g[v]) {
    int res = dfs(to, d + 1);
    num += res;
  }
  if (num == 0 && a[v] != v)
    ++num;
  if (num == 1)
    one[v] = 1;
  return num;
}

int main() {
  int N;
  cin >> N;
  V<int> p(N, -1);

  for (int i = 1; i < N; ++i) {
    cin >> p[i];
    g[p[i]].pb(i);
  }

  rep(i, N) cin >> a[i];

  V<int> ope;

  auto rot = [&](int v) {
    ope.pb(v);
    V<int> vec;
    while (v != -1) {
      vec.pb(v);
      v = p[v];
    }
    reverse(ALL(vec));
    int sz = vec.size();

    int x = a[0];
    rep(i, sz - 1) { a[vec[i]] = a[vec[i + 1]]; }
    a[vec.back()] = x;

    reverse(ALL(vec));
    for (int v : vec) {
      bool t = (a[v] != v);
      for (auto to : g[v]) {
        if (one[to])
          t = 1;
      }
      if (!t) {
        one[v] = 0;
      }
    }
  };

  while (true) {
    bool ok = 1;
    rep(i, N) if (i != a[i]) ok = 0;
    if (ok) {
      break;
    }

    memset(one, 0, sizeof(one));
    dfs(0, 0);

    V<int> cmp(N, -1);
    V<int> la(N, -1);
    {
      V<int> cand(N, 1);
      rep(i, N) cand[i] = one[i];
      rep(i, N) if (one[i] && p[i] != -1) cand[p[i]] = 0;
      int now = 0;
      rep(i, N) if (cand[i]) {
        int v = i;
        la[now] = v;
        while (v != -1 && one[v]) {
          cmp[v] = now;
          v = p[v];
        }
        now++;
      }
    }

    V<int> used(N, -1);
    rep(i, N) if (one[i]) used[a[i]] = 0;

    while (true) {
      {
        bool en = 1;
        rep(i, N) if (one[i]) { en = 0; }
        if (en)
          break;
      }
      int x = a[0];
      if (used[x] == 1)
        break;
      if (one[x]) {
        int v = la[cmp[x]];
        dump(v);
        while (v != -1) {
          int av = a[v];
          if (used[av] != 1 ||
              (cmp[av] != cmp[x] || cmp[av] == cmp[x] && dep[av] < dep[x])) {
            used[x] = 1;
            rot(v);
            break;
          }
          v = p[v];
        }
      } else {
        int mdep = -1, cand = -1;
        rep(i, N) {
          int x = a[i];
          if (one[x] && used[x] != 1) {
            if (mdep < dep[i]) {
              mdep = dep[i];
              cand = i;
            }
          }
        }
        if (cand == -1)
          break;
        rot(cand);
      }
    }
  }

  cout << ope.size() << endl;
  for (int x : ope) {
    cout << x << endl;
  }

  return 0;
}