Problem p00777 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1196&lang=jp
typedef long long ll;
#define INF 1 << 30
#define LINF 1LL << 60
struct edge {
  int from;
  int to;
  ll cost;
  edge() {}
  edge(int f, int t, int c) : from(f), to(t), cost(c) {}
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  while (cin >> n, n) {
    ll sum_d = 0;
    vector<ll> p(n - 1), d(n - 1);
    for (int i = 1; i < n; i++) {
      cin >> p[i - 1];
      p[i - 1]--;
    }
    for (int i = 1; i < n; i++) {
      cin >> d[i - 1];
      sum_d += d[i - 1];
    }
    if (n == 3) {
      cout << sum_d << endl;
      continue;
    }
    vector<vector<edge>> G(n);
    for (int i = 1; i < n; i++) {
      G[p[i - 1]].push_back(edge(p[i - 1], i, d[i - 1]));
      G[i].push_back(edge(i, p[i - 1], d[i - 1]));
    }

    int from;
    ll leaf_cost = 0;
    vector<int> leaf(n, 0);
    for (int i = 0; i < n; i++) {
      if (G[i].size() == 1) {
        leaf_cost += G[i][0].cost;
        G[i].clear();
        leaf[i] = 1;
      } else
        from = i;
    }
    for (int i = 0; i < n; i++) {
      for (int j = G[i].size() - 1; j >= 0; j--) {
        if (leaf[G[i][j].to] == 1) {
          G[i].erase(G[i].begin() + j);
        }
      }
    }
    /* diameter */
    int s, t;
    vector<ll> dist(n, INF);
    dist[from] = 0;
    queue<ll> q;
    q.push(from);
    while (!q.empty()) {
      ll n = q.front();
      q.pop();
      for (auto e : G[n]) {
        // if (leaf[e.to] == 1) continue;
        if (dist[e.to] > dist[e.from] + e.cost) {
          dist[e.to] = dist[e.from] + e.cost;
          q.push(e.to);
        }
      }
    }
    int Max = -1;
    for (int i = 0; i < n; i++) {
      if (dist[i] == INF)
        continue;
      if (dist[i] >= Max)
        Max = dist[i], s = i;
    }
    fill(dist.begin(), dist.end(), INF);
    dist[s] = 0;
    q.push(s);
    while (!q.empty()) {
      ll n = q.front();
      q.pop();
      for (auto e : G[n]) {
        // if (leaf[e.to] == 1) continue;
        if (dist[e.to] > dist[e.from] + e.cost) {
          dist[e.to] = dist[e.from] + e.cost;
          q.push(e.to);
        }
      }
    }

    Max = -1;
    for (int i = 0; i < n; i++) {
      if (dist[i] == INF)
        continue;
      if (dist[i] > Max)
        Max = dist[i], t = i;
    }

    ll Max_path_cost = dist[t];
    ll Other_path_cost = sum_d - leaf_cost;

    cout << sum_d + 2 * Other_path_cost - Max_path_cost << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1196&lang=jp
typedef long long ll;
#define INF 1 << 30
#define LINF 1LL << 60
struct edge {
  int from;
  int to;
  ll cost;
  edge() {}
  edge(int f, int t, int c) : from(f), to(t), cost(c) {}
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  while (cin >> n, n) {
    ll sum_d = 0;
    vector<ll> p(n - 1), d(n - 1);
    for (int i = 1; i < n; i++) {
      cin >> p[i - 1];
      p[i - 1]--;
    }
    for (int i = 1; i < n; i++) {
      cin >> d[i - 1];
      sum_d += d[i - 1];
    }
    vector<vector<edge>> G(n);
    for (int i = 1; i < n; i++) {
      G[p[i - 1]].push_back(edge(p[i - 1], i, d[i - 1]));
      G[i].push_back(edge(i, p[i - 1], d[i - 1]));
    }

    ll leaf_cost = 0;
    vector<int> leaf(n, 0);
    for (int i = 0; i < n; i++) {
      if (G[i].size() == 1) {
        leaf_cost += G[i][0].cost;
        G[i].clear();
        leaf[i] = 1;
      }
    }

    /* diameter */
    int s, t;
    vector<ll> dist(n, LINF);
    dist[0] = 0;
    queue<ll> q;
    q.push(0);
    while (!q.empty()) {
      ll n = q.front();
      q.pop();
      for (auto e : G[n]) {
        if (leaf[e.to] == 1)
          continue;
        if (dist[e.to] > dist[e.from] + e.cost) {
          dist[e.to] = dist[e.from] + e.cost;
          q.push(e.to);
        }
      }
    }
    int Max = 0;
    for (int i = 0; i < n; i++) {
      if (leaf[i] == 1)
        continue;
      if (dist[i] > Max)
        Max = dist[i], s = i;
    }
    fill(dist.begin(), dist.end(), LINF);
    dist[s] = 0;
    q.push(s);
    while (!q.empty()) {
      ll n = q.front();
      q.pop();
      for (auto e : G[n]) {
        if (leaf[e.to] == 1)
          continue;
        if (dist[e.to] > dist[e.from] + e.cost) {
          dist[e.to] = dist[e.from] + e.cost;
          q.push(e.to);
        }
      }
    }

    Max = 0;
    for (int i = 0; i < n; i++) {
      if (leaf[i] == 1)
        continue;
      if (dist[i] > Max)
        Max = dist[i], t = i;
    }

    ll Max_path_cost = dist[t];
    ll Other_path_cost = sum_d - leaf_cost;

    cout << sum_d + 2 * Other_path_cost - Max_path_cost << endl;
  }
  return 0;
}