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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = n - 1; i >= (0); i--)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define all(v) (v).begin(), (v).end()
#define eall(v) unique(all(v), v.end())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = min(a, b);
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll INFF = 1e18;

int dp[810];
int p[810];
int d[810];
vector<pair<int, int>> G[810];

void dfs(int u, int p) {
  for (auto v : G[u]) {
    if (v.fi == p)
      continue;
    dp[v.fi] = dp[u] + v.se;
    dfs(v.fi, u);
  }
  return;
}

int main(void) {
  while (1) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;

    rep(i, n - 1) scanf("%d", &p[i]);
    rep(i, n - 1) scanf("%d", &d[i]);
    rep(i, n - 1) p[i]--;

    rep(i, 810) G[i].clear();
    rep(i, n - 1) {
      int u = i + 1, v = p[i];
      G[u].pb(mp(v, d[i])), G[v].pb(mp(u, d[i]));
    }

    ll all = 0;
    rep(i, n - 1) all += d[i];

    ll allleaf = 0;
    set<int> leafse;

    rep(i, n) if (G[i].size() == 1) allleaf += G[i][0].se;

    rep(i, n) if (G[i].size() > 1) leafse.insert(i);

    ll maxe = 0;
    for (auto u : leafse) {
      rep(i, 810) dp[i] = 0;
      dfs(u, -1);
      // u -> v????????¢
      for (auto v : leafse)
        chmax(maxe, dp[v]);
    }

    // printf("all %lld allleaf %lld maxe %lld \\n", all, allleaf, maxe);
    ll ans = all + all * 2 - (allleaf * 2) - maxe;
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = n - 1; i >= (0); i--)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define all(v) (v).begin(), (v).end()
#define eall(v) unique(all(v), v.end())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define chmax(a, b) a = (((a) < (b)) ? (b) : (a))
#define chmin(a, b) a = min(a, b);
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll INFF = 1e18;

int dp[810][810];
int p[810];
int d[810];

int main(void) {
  while (1) {
    int n;
    scanf("%d", &n);
    if (n == 0)
      break;

    rep(i, n - 1) scanf("%d", &p[i]);
    rep(i, n - 1) scanf("%d", &d[i]);
    rep(i, n - 1) p[i]--;

    rep(i, n + 1) rep(j, n + 1) dp[i][j] = INF;
    rep(i, n + 1) dp[i][i] = 0;
    rep(i, n - 1) {
      int u = i + 1, v = p[i];
      dp[u][v] = dp[v][u] = d[i];
    }
    rep(k, n) rep(i, n) rep(j, n) chmin(dp[i][j], dp[i][k] + dp[k][j]);

    /*
    rep(i, 4)rep(j, 4){
            printf("%d %d %d\\n", i,j,dp[i][j]);
    }
    */

    vector<pair<int, int>> G[n + 1];
    rep(i, n - 1) {
      int u = i + 1, v = p[i];
      G[u].pb(mp(v, d[i])), G[v].pb(mp(u, d[i]));
    }

    ll all = 0;
    rep(i, n - 1) all += d[i];

    ll allleaf = 0;
    vector<int> leafse;

    rep(i, n) if (G[i].size() == 1) allleaf += G[i][0].se;

    rep(i, n) if (G[i].size() > 1) leafse.pb(i);

    ll maxe = 0;
    for (auto u : leafse)
      for (auto v : leafse) {
        // printf("%d -> %d = %lld \\n", u, v, dp[u][v]);
        chmax(maxe, dp[u][v]);
      }

    // printf("all %lld allleaf %lld maxe %lld \\n", all, allleaf, maxe);
    ll ans = all + all * 2 - (allleaf * 2) - maxe;
    printf("%lld\\n", ans);
  }
  return 0;
}