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>
#include <functional>
using namespace std;
const long long inf = 1LL << 40;
class edge {
public:
int to; long long cost;
edge() : to(-1), cost(0LL) {};
edge(int to_, long long cost_) : to(to_), cost(cost_) {};
};
pair<long long, long long> solve(int pos, int pre, const vector<vector<edge> > &G) {
// returns = (no mid-blue, one mid-blue)
long long sumcost = 0, delta = -inf;
for(edge e : G[pos]) {
if(e.to == pre) continue;
pair<long long, long long> res = solve(e.to, pos, G);
sumcost += max(res.first, res.second + e.cost);
delta = max(delta, e.cost - max(res.second + e.cost - res.first, 0LL));
}
return make_pair(sumcost, sumcost + delta);
}
int main() {
// step #1. read input
cin.tie(0);
ios_base::sync_with_stdio(false);
int N;
cin >> N;
vector<vector<edge> > G(N);
for(int i = 0; i < N - 1; ++i) {
int a, b; long long w;
cin >> a >> b >> w; --a, --b;
G[a].push_back(edge(b, w));
G[b].push_back(edge(a, w));
}
// step #2. calculation
long long ans = 0;
for(int i = 0; i < N; ++i) {
pair<long long, long long> res = solve(i, -1, G);
ans = max(ans, res.first);
}
// step #3. print the answer
cout << ans << endl;
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... |