Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

A - Breadth-First Search by Foxpower

Problem Statement

Fox Ciel went to JAG Kingdom by bicycle, but she forgot a place where she parked her bicycle. So she needs to search it from a bicycle-parking area before returning home.

The parking area is formed as a unweighted rooted tree $T$ with $n$ vertices, numbered $1$ through $n$. Each vertex has a space for parking one or more bicycles. Ciel thought that she parked her bicycle near the vertex $1$, so she decided to search it from there by the breadth-first search. That is, she searches it at the vertices in the increasing order of their distances from the vertex $1$. If multiple vertices have the same distance, she gives priority to the vertices in the order of searching at their parents. If multiple vertices have the same parent, she searches at the vertex with minimum number at first.

Unlike a computer, she can't go to a next vertex by random access. Thus, if she goes to the vertex $j$ after the vertex $i$, she needs to walk the distance between the vertices $i$ and $j$. BFS by fox power perhaps takes a long time, so she asks you to calculate the total moving distance in the worst case starting from the vertex $1$.

Input

The input is formatted as follows.

$n$
$p_2$ $p_3$ $p_4$ $\cdots$ $p_n$

The first line contains an integer $n$ ($1 \le n \le 10^5$), which is the number of vertices on the unweighted rooted tree $T$.
The second line contains $n-1$ integers $p_i$ ($1 \le p_i < i$), which are the parent of the vertex $i$.
The vertex $1$ is a root node, so $p_1$ does not exist.

Output

Print the total moving distance in the worst case in one line.

Sample Input 1

4
1 1 2

Output for the Sample Input 1

6

Sample Input 2

4
1 1 3

Output for the Sample Input 2

4

Sample Input 3

11
1 1 3 3 2 4 1 3 2 9

Output for the Sample Input 3

25

Solution in C++ (judge verdict: Accepted)

#include <bits/stdc++.h>
using namespace std;
typedef long long lli;

const lli MAXN = 100005;
const lli LG_NMAX = 64;

vector<lli> child[MAXN];

struct Edge {
  int v, w;
};
typedef vector<vector<Edge> > Graph;

class LCA {
private:
  const static int LG_VMAX = 64;
  int n;
  vector<vector<int> > dpar;
  vector<int> depth;

public:
  LCA(const vector<int> &par, int root) {
    n = par.size();

    dpar = vector<vector<int> >(n, vector<int>(LG_VMAX, 0));
    for (int v = 0; v < n; ++v) dpar[v][0] = par[v];
    dpar[root][0] = root;
    for (int d = 1; d < LG_VMAX; ++d) {
      for (int v = 0; v < n; ++v) {
        dpar[v][d] = dpar[dpar[v][d-1]][d-1];
      }
    }

    depth = vector<int>(n, -1);
    depth[root] = 0;
    for (int v = 0; v < n; ++v) {
      if (depth[v] != -1) continue;
      stack<int> stk;
      stk.push(v);
      while (depth[dpar[stk.top()][0]] == -1) {
        stk.push(dpar[stk.top()][0]);
      }
      while (stk.size()) {
        depth[stk.top()] = depth[dpar[stk.top()][0]] + 1;
        stk.pop();
      }
    }
  }

  LCA(const Graph &g, int root) {
    n = g.size();
    
    vector<int> par(n, -1);
    par[root] = root;
    queue<int> que;
    que.push(root);
    while (que.size()) {
      int v = que.front();
      que.pop();
      for (int i = 0; i < g[v].size(); ++i) {
        int nv = g[v][i].v;
        if (par[nv] != -1) continue;
        par[nv] = v;
        que.push(nv);
      }
    }
    LCA(par, root);
  }

  int get(int a, int b) const {
    if (depth[a] != depth[b]) {
      if (depth[a] > depth[b]) swap(a, b);
      for (int d = LG_VMAX - 1; d >= 0; --d) {
        if (depth[a] <= depth[dpar[b][d]]) {
          b = dpar[b][d];
        }
      }
    }
    if (a == b) return a;
    for (int d = LG_VMAX - 1; d >= 0; --d) {
      if (dpar[a][d] != dpar[b][d]) {
        a = dpar[a][d];
        b = dpar[b][d];
      }
    }
    return dpar[a][0];
  }

  const vector<int>& getDepth() const {
    return depth;
  }
};

int main() {
  lli N;
  while (cin >> N) {
    fill(child, child + MAXN, vector<lli>());
    vector<int> par(N, 0);
    for (lli i = 1; i < N; ++i) {
      cin >> par[i];
      par[i]--;
      child[par[i]].push_back(i);
    }
    for (lli i = 0; i < N; ++i) {
      sort(child[i].begin(), child[i].end());
    }

    LCA lca(par, 0);
    const vector<int> &depth = lca.getDepth();
    lli res = 0, prev = 0;
    queue<lli> que;
    que.push(0);
    while (que.size()) {
      const lli v = que.front();
      que.pop();
      res += depth[prev] + depth[v] - depth[lca.get(prev, v)] * 2;
      prev = v;
      for (lli i = 0; i < child[v].size(); ++i) {
        que.push(child[v][i]);
      }
    }
    cout << res << endl;
  }
  return 0;
}