Problem p00777 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const int INF = numeric_limits<int>::max() / 2;

struct Edge {
  int src, dst;
  int weight;
  Edge(int src, int dst, int weight) : src(src), dst(dst), weight(weight) {}
};

auto operator<(const Edge &e, const Edge &f) {
  if (e.weight != f.weight)
    return e.weight > f.weight;
  else if (e.src != f.src)
    return e.src < f.src;
  else
    return e.dst < f.dst;
}

using Edges = vector<Edge>;
using Graph = vector<Edges>;

pair<int, int> sweep(int p, int v, const Graph &g) {
  pair<int, int> r(0, v);
  for (auto e : g[v]) {
    if (e.dst != p) {
      pair<int, int> t = sweep(v, e.dst, g);
      t.first += e.weight;
      if (r.first < t.first)
        r = t;
    }
  }
  return r;
}
int double_sweep(const Graph &g, int s) {
  pair<int, int> r = sweep(-1, s, g);
  pair<int, int> t = sweep(-1, r.second, g);
  return t.first;
}

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

  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int len = 0, cut = 0;
    vector<int> p(n), d(n);
    for (int i = 1; i < n; i++) {
      cin >> p[i];
      p[i]--;
    }
    for (int i = 1; i < n; i++) {
      cin >> d[i];
      len += d[i];
    }
    vector<int> v[1000];
    vector<bool> leaf(n, false);
    Graph g(n);
    for (int i = 1; i < n; i++) {
      int s = i, g = p[i];
      v[s].push_back(g);
      v[g].push_back(s);
    }
    for (int i = 0; i < n; i++)
      if (v[i].size() == 1)
        leaf[i] = true;
    for (int i = 1; i < n; i++) {
      if (leaf[i] || leaf[p[i]])
        continue;
      else {
        Edge e(i, p[i], d[i]), e2(p[i], i, d[i]);
        g[i].push_back(e);
        g[p[i]].push_back(e2);
        cut += d[i];
      }
    }

    int st;
    for (int i = 0; i < n; i++)
      if (!leaf[i])
        st = i;
    int r = double_sweep(g, st);
    //??´???=r
    cout << len + cut * 2 - r << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const int INF = numeric_limits<int>::max() / 2;

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

  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    int len = 0, cut = 0;
    vector<int> p(n), d(n);
    for (int i = 1; i < n; i++) {
      cin >> p[i];
      p[i]--;
    }
    for (int i = 1; i < n; i++) {
      cin >> d[i];
      len += d[i];
    }
    vector<int> v[1000];
    vector<bool> leaf(n, false);
    VVI dis(n, vector<int>(n, INF));
    for (int i = 0; i < n; i++)
      dis[i][i] = 0;
    for (int i = 1; i < n; i++) {
      int s = i, g = p[i];
      v[s].push_back(g);
      v[g].push_back(s);
    }
    for (int i = 0; i < n; i++)
      if (v[i].size() == 1)
        leaf[i] = true;
    for (int i = 1; i < n; i++) {
      if (leaf[i] || leaf[p[i]])
        continue;
      else {
        dis[i][p[i]] = d[i];
        dis[p[i]][i] = d[i];
        cut += d[i];
      }
    }
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
        }
      }
    }
    int ma = -1, st, vec;
    for (int i = 0; i < n; i++)
      if (!leaf[i])
        st = i;
    for (int i = 0; i < n; i++)
      if (!leaf[i] && dis[st][i] > ma) {
        ma = dis[st][i];
        vec = i;
        // cout<<ma<<endl;
      }
    int r = -1;
    for (int i = 0; i < n; i++)
      if (!leaf[i]) {
        r = max(r, dis[vec][i]);
      }
    //??´???=r
    cout << len + cut * 2 - r << endl;
  }
}