Problem p01780 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define maxn 100005
#define MOD 1000000007
#define hashm 17982571
using namespace std;
int n, l, tot = 0, cnt = 0;
long long ans = 0;
int f[maxn], p[maxn], b[2 * maxn], dis[maxn], a[maxn], dp[20][2 * maxn];
vector<int> e[maxn];
void dfs(int now, int fa, int dep) {
  dis[now] = dep;
  int tt = tot++;
  f[tt] = now;
  p[now] = cnt;
  b[cnt++] = tt;
  for (int i = 0; i < e[now].size(); i++) {
    dfs(e[now][i], now, dep + 1);
    b[cnt++] = tt;
  }
  return;
}
void rmq(int n) {
  for (int i = 0; i < n; i++)
    dp[i][0] = b[i];
  int m = int(log(n * 1.0) / log(2.0));
  for (int i = 1; i <= m; i++)
    for (int j = 0; j < n - (1 << i) + 1; j++)
      dp[j][i] = min(dp[j][i - 1], dp[j + (1 << (i - 1))][i - 1]);
  return;
}
int lca(int l, int r) {
  if (l > r)
    swap(l, r);
  int m = (int)(log(r - l + 1.0) / log(2.0));
  return f[min(dp[l][m], dp[r - (1 << m) + 1][m])];
}
void bfs() {
  queue<int> q;
  q.push(1);
  l = 1;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < e[x].size(); i++) {
      q.push(e[x][i]);
      int yy = lca(p[l], p[e[x][i]]);
      ans += dis[l] + dis[e[x][i]] - 2 * dis[yy];
      l = e[x][i];
    }
  }
}
int main() {
  int i, j;
  scanf("%d", &n);
  for (i = 2; i <= n; i++) {
    scanf("%d", &j);
    e[j].push_back(i);
  }
  dfs(1, 0, 0);
  rmq(cnt - 1);
  bfs();
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define maxn 100005
#define MOD 1000000007
#define hashm 17982571
using namespace std;
int n, l, tot = 0, cnt = 0;
long long ans = 0;
int f[maxn], p[maxn], b[2 * maxn], dis[maxn], a[maxn], dp[2 * maxn][20];
vector<int> e[maxn];
void dfs(int now, int fa, int dep) {
  dis[now] = dep;
  int tt = tot++;
  f[tt] = now;
  p[now] = cnt;
  b[cnt++] = tt;
  for (int i = 0; i < e[now].size(); i++) {
    dfs(e[now][i], now, dep + 1);
    b[cnt++] = tt;
  }
  return;
}
void rmq(int n) {
  for (int i = 0; i < n; i++)
    dp[i][0] = b[i];
  int m = int(log(n * 1.0) / log(2.0));
  for (int i = 1; i <= m; i++)
    for (int j = 0; j < n - (1 << i) + 1; j++)
      dp[j][i] = min(dp[j][i - 1], dp[j + (1 << (i - 1))][i - 1]);
  return;
}
int lca(int l, int r) {
  if (l > r)
    swap(l, r);
  int m = (int)(log(r - l + 1.0) / log(2.0));
  return f[min(dp[l][m], dp[r - (1 << m) + 1][m])];
}
void bfs() {
  queue<int> q;
  q.push(1);
  l = 1;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    for (int i = 0; i < e[x].size(); i++) {
      q.push(e[x][i]);
      int yy = lca(p[l], p[e[x][i]]);
      ans += dis[l] + dis[e[x][i]] - 2 * dis[yy];
      l = e[x][i];
    }
  }
}
int main() {
  int i, j;
  scanf("%d", &n);
  for (i = 2; i <= n; i++) {
    scanf("%d", &j);
    e[j].push_back(i);
  }
  dfs(1, 0, 0);
  rmq(cnt - 1);
  bfs();
  printf("%lld\n", ans);
  return 0;
}