This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
struct edge {
int to, cost;
edge() : to(-1), cost(0) {};
edge(int to_, int cost_) : to(to_), cost(cost_) {};
};
int n, pc[200009]; long long dp[200009][2]; vector<edge> g[200009];
void solve(int pos, int pre) {
long long best1 = -(1LL << 60), best2 = -(1LL << 60), sum = 0;
for (edge e : g[pos]) {
if (e.to == pre) continue;
pc[e.to] = e.cost;
solve(e.to, pos);
sum += dp[e.to][1];
long long d = dp[e.to][0] - dp[e.to][1] + e.cost;
if (d > best1) {
best2 = best1;
best1 = d;
}
else if (d > best2) {
best2 = d;
}
}
dp[pos][0] = max(sum, sum + best1 + best2);
dp[pos][1] = max(dp[pos][0], pc[pos] + sum + best1);
}
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b, c;
cin >> a >> b >> c; a--, b--;
g[a].push_back(edge(b, c));
g[b].push_back(edge(a, c));
}
solve(0, -1);
cout << dp[0][0] << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |