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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

struct edge {
  long long to, cost;
};
typedef pair<long long, long long> P;

const long long INF = 1LL << 30;

//???????????¢????????¢,??????
pair<long long, long long> dfs(vector<vector<long long>> &dis,
                               vector<long long> &cnt, long long &x) {
  const long long m = dis.size();
  vector<P> vp;
  vector<int> vis(m, 0);
  stack<P> s;
  s.push(P(0, x));

  while (!s.empty()) {
    P p = s.top();
    s.pop();
    long long now = p.second;
    long long cost = p.first;

    if (vis[now])
      continue;

    vp.push_back(P(cost, now));

    vis[now] = 1;
    for (long long i = 0; i < m; i++) {
      if (dis[now][i] != INF && cnt[i] != 1)
        s.push(P(cost + dis[now][i], i));
    }
  }

  sort(vp.begin(), vp.end());
  /*
          for(int i=0;i<vp.size();i++){
                  cout<<vp[i].first<<" "<<vp[i].second<<endl;
          }
  */
  for (int i = vp.size() - 1; i >= 0; i++) {
    if (cnt[vp[i].second] != 1)
      return vp[i];
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<vector<long long>> dis(n, vector<long long>(n, INF));
    for (int i = 0; i < n; i++)
      dis[i][i] = 0;

    vector<long long> p(n - 1), d(n - 1), cnt(n, 0);
    for (int i = 0; i < n - 1; i++) {
      cin >> p[i];
      p[i]--;
      // cnt[p[i]]++;
    }
    for (int i = 0; i < n - 1; i++)
      cin >> d[i];

    for (int i = 0; i < n - 1; i++) {
      dis[i + 1][p[i]] = dis[p[i]][i + 1] = d[i];
      cnt[i + 1]++;
      cnt[p[i]]++;
    }

    long long res = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (dis[i][j] != INF) {
          if (cnt[i] == 1 || cnt[j] == 1)
            res += dis[i][j];
          else
            res += 3 * dis[i][j];
        }
      }
    }

    P x, y;
    for (long long i = 0; i < n; i++) {
      if (cnt[i] != 1) {
        x = dfs(dis, cnt, i);
        break;
      }
    }

    y = dfs(dis, cnt, x.second);

    res -= y.first;
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

struct edge {
  long long to, cost;
};
typedef pair<int, int> P;

const long long INF = 1LL << 30;

long long dfs(vector<vector<long long>> dis, int x, int y) {
  const int m = dis.size();
  vector<bool> vis(m, false);
  stack<P> s;
  s.push(P(x, 0));

  while (!s.empty()) {
    P p = s.top();
    s.pop();
    int now = p.first;
    int cost = p.second;

    if (now == y)
      return cost;
    if (vis[now])
      continue;

    vis[now] = true;
    for (int i = 0; i < m; i++) {
      if (dis[now][i] != INF)
        s.push(P(i, cost + dis[now][i]));
    }
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    vector<vector<long long>> dis(n, vector<long long>(n, INF));
    for (int i = 0; i < n; i++)
      dis[i][i] = 0;

    vector<long long> p(n - 1), d(n - 1), cnt(n, 0);
    for (int i = 0; i < n - 1; i++) {
      cin >> p[i];
      p[i]--;
      // cnt[p[i]]++;
    }
    for (int i = 0; i < n - 1; i++)
      cin >> d[i];

    for (int i = 0; i < n - 1; i++) {
      dis[i + 1][p[i]] = dis[p[i]][i + 1] = d[i];
      cnt[i + 1]++;
      cnt[p[i]]++;
    }

    long long res = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (dis[i][j] != INF) {
          if (cnt[i] == 1 || cnt[j] == 1)
            res += dis[i][j];
          else
            res += 3 * dis[i][j];
        }
      }
    }

    long long longest = 0;
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        // cout<<i<<" "<<j<<" "<<dfs(dis,i,j)<<endl;
        if (cnt[i] != 1 && cnt[j] != 1)
          longest = max(longest, dfs(dis, i, j));
      }
    }

    res -= longest;
    cout << res << endl;
  }
}