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

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

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

struct Edge {
  int to, cost;

  Edge(int t, int c = 0) : to(t), cost(c) {}
  bool operator>(const Edge &rhs) const { return cost > rhs.cost; }
  bool operator<(const Edge &rhs) const { return cost < rhs.cost; }
};
typedef vector<vector<Edge>> Graph;

void add_edge(Graph &graph, int u, int v, int cost) {
  graph[u].push_back(Edge(v, cost));
  graph[v].push_back(Edge(u, cost));
}

PII diameter_(int prv, int now, const Graph &g) {
  PII res(0, now);
  for (auto &e : g[now]) {
    if (prv == e.to)
      continue;
    PII tmp = diameter_(now, e.to, g);
    tmp.first += e.cost;
    if (res.first < tmp.first)
      res = tmp;
  }
  return res;
}
PII diameter(const Graph &g, int &r, int rt) {
  PII u = diameter_(-1, rt, g);
  PII v = diameter_(-1, u.second, g);
  r = v.first;
  return PII(u.second, v.second);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  while (cin >> N, N) {
    int S = 0;
    VI ps(N), ds(N);
    REP(i, N - 1) {
      cin >> ps[i];
      --ps[i];
    }
    REP(i, N - 1) {
      cin >> ds[i];
      S += ds[i];
    }
    if (N == 3) {
      cout << S << endl;
      continue;
    }

    VI in(N);
    REP(i, N - 1) {
      in[i + 1]++;
      in[ps[i]]++;
    }

    int ans = 0;
    Graph G(N);
    int rt = -1;
    REP(i, N - 1) {
      if (in[i + 1] != 1 && in[ps[i]] != 1) {
        rt = i + 1;
        add_edge(G, i + 1, ps[i], ds[i]);
        ans += ds[i];
      }
    }

    int n = 0;
    REP(i, N) if (in[i] > 1)++ n;
    int r = 0;
    if (n > 1)
      diameter(G, r, rt);
    cout << ans * 2 - r + S << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;

struct Edge {
  int to, cost;

  Edge(int t, int c = 0) : to(t), cost(c) {}
  bool operator>(const Edge &rhs) const { return cost > rhs.cost; }
  bool operator<(const Edge &rhs) const { return cost < rhs.cost; }
};
typedef vector<vector<Edge>> Graph;

void add_edge(Graph &graph, int u, int v, int cost) {
  graph[u].push_back(Edge(v, cost));
  graph[v].push_back(Edge(u, cost));
}

PII diameter_(int prv, int now, const Graph &g) {
  PII res(0, now);
  for (auto &e : g[now]) {
    if (prv == e.to)
      continue;
    PII tmp = diameter_(now, e.to, g);
    tmp.first += e.cost;
    if (res.first < tmp.first)
      res = tmp;
  }
  return res;
}
PII diameter(const Graph &g, int &r, int rt) {
  PII u = diameter_(-1, rt, g);
  PII v = diameter_(-1, u.second, g);
  r = v.first;
  return PII(u.second, v.second);
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N;
  while (cin >> N, N) {
    int S = 0;
    VI ps(N), ds(N);
    REP(i, N - 1) {
      cin >> ps[i];
      --ps[i];
    }
    REP(i, N - 1) {
      cin >> ds[i];
      S += ds[i];
    }

    VI in(N);
    REP(i, N - 1) {
      in[i + 1]++;
      in[ps[i]]++;
    }
    Graph G(N);
    REP(i, N - 1) { add_edge(G, i + 1, ps[i], ds[i]); }

    int ans = 0;
    Graph G_(N);
    int rt = -1;
    REP(i, N - 1) {
      if (in[i + 1] != 1 && in[ps[i]] != 1) {
        rt = i + 1;
        add_edge(G_, i + 1, ps[i], ds[i]);
        ans += ds[i];
      }
    }

    int r;
    diameter(G_, r, rt);
    cout << ans * 2 - r + S << endl;
  }

  return 0;
}