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

#include <bits/stdc++.h>

using namespace std;
#define mp(x, y) make_pair((x), (y))
typedef pair<int, int> ii;
typedef vector<ii> vii;
const int INF = (int)1e9;
vii adj[1234];
int n;

int main() {
  while (cin >> n && n) {
    for (int i = 0; i < 1234; i++)
      adj[i].clear();
    vector<int> u(n - 1), dist(n - 1);
    for (int i = 0; i < n - 1; i++)
      cin >> u[i];
    for (int i = 0; i < n - 1; i++)
      cin >> dist[i];
    int tot = 0;
    for (int i = 0; i < n - 1; i++) {
      int v = i + 2;
      adj[u[i]].push_back(mp(v, dist[i]));
      adj[v].push_back(mp(u[i], dist[i]));
      tot += dist[i] * 3;
    }

    vector<int> valid(n + 1, 1);
    for (int i = 1; i <= n; i++) {
      if ((int)adj[i].size() == 1) {
        valid[i] = 0;
        tot -= adj[i][0].second * 2;
      }
    }

    int st = -1;
    for (int i = 1; i <= n; i++) {
      if (valid[i]) {
        st = i;
        break;
      }
    }
    if (st == -1) {
      cout << tot << endl;
      continue;
    }
    vector<int> distance(n + 1, INF);
    queue<int> q;
    q.push(st);
    distance[st] = 0;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int i = 0; i < (int)adj[u].size(); i++) {
        int v = adj[u][i].first;
        int d = adj[u][i].second;
        if (distance[v] == INF && valid[v]) {
          distance[v] = distance[u] + d;
          q.push(v);
        }
      }
    }
    int nxt = -1;
    int curr = 0;
    for (int i = 1; i <= n; i++) {
      if (distance[i] != INF && distance[i] > curr) {
        curr = distance[i];
        nxt = i;
      }
    }
    if (nxt == -1) {
      cout << tot << endl;
      continue;
    }
    distance.assign(n + 1, INF);
    q.push(nxt);
    distance[nxt] = 0;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      for (int i = 0; i < (int)adj[u].size(); i++) {
        int v = adj[u][i].first;
        int d = adj[u][i].second;
        if (distance[v] == INF && valid[v]) {
          distance[v] = distance[u] + d;
          q.push(v);
        }
      }
    }

    int hi = 0;
    for (int i = 1; i <= n; i++) {
      if (distance[i] != INF)
        hi = max(hi, distance[i]);
    }

    tot -= hi;
    cout << tot << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define mp(x, y) make_pair((x), (y))
typedef pair<int, int> ii;
typedef vector<ii> vii;
const int INF = (int)1e9;
vii adj[1234];
int n;

int main() {
  while (cin >> n && n) {
    for (int i = 0; i < 1234; i++)
      adj[i].clear();
    vector<int> u(n - 1), dist(n - 1);
    for (int i = 0; i < n - 1; i++)
      cin >> u[i];
    for (int i = 0; i < n - 1; i++)
      cin >> dist[i];
    int tot = 0;
    for (int i = 0; i < n - 1; i++) {
      int v = i + 2;
      adj[u[i]].push_back(mp(v, dist[i]));
      adj[v].push_back(mp(u[i], dist[i]));
      tot += dist[i] * 3;
    }

    vector<int> valid(n + 1, 1);
    for (int i = 1; i <= n; i++) {
      if ((int)adj[i].size() == 1) {
        valid[i] = 0;
        tot -= adj[i][0].second * 2;
      }
    }
    int st = -1;
    for (int i = 1; i <= n; i++) {
      if (valid[i]) {
        st = i;
        break;
      }
    }
    if (st == -1) {
      cout << tot << endl;
      continue;
    }
    vector<int> distance(n + 1, 0);
    vector<int> visited(n + 1, 0);
    queue<int> q;
    q.push(st);
    distance[st] = 0;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      if (visited[u])
        continue;
      visited[u] = 1;
      for (int i = 0; i < (int)adj[u].size(); i++) {
        int v = adj[u][i].first;
        if (valid[v] == 0)
          continue;
        int d = adj[u][i].second;
        if (!distance[v] && v != st) {
          distance[v] = distance[u] + d;
          q.push(v);
        }
      }
    }
    int nxt;
    int curr = 0;
    for (int i = 1; i <= n; i++) {
      if (distance[i] > curr) {
        curr = distance[i];
        nxt = i;
      }
    }

    distance.assign(n + 1, 0);
    visited.assign(n + 1, 0);
    q.push(nxt);
    distance[nxt] = 0;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      if (visited[u])
        continue;
      visited[u] = 1;
      for (int i = 0; i < (int)adj[u].size(); i++) {
        int v = adj[u][i].first;
        if (valid[v] == 0)
          continue;
        int d = adj[u][i].second;
        if (!distance[v] && v != nxt) {
          distance[v] = distance[u] + d;
          q.push(v);
        }
      }
    }

    int hi = 0;
    for (int i = 1; i <= n; i++) {
      hi = max(hi, distance[i]);
    }
    tot -= hi;
    cout << tot << endl;
  }
  return 0;
}