이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf = 2012345678;
struct edge {
int to, cost;
edge() : to(-1), cost(0) {};
edge(int to_, int cost_) : to(to_), cost(cost_) {};
};
int n, dp[200009][2]; vector<edge> g[200009];
pair<int, int> solve(int pos, int pre) {
if (pre != -1) {
int sum = 0, mx = -inf;
for (edge e : g[pos]) {
if (e.to == pre) continue;
pair<int, int> res = solve(e.to, pos);
sum += max(res.first, res.second + e.cost);
if (res.first > res.second + e.cost) {
mx = max(mx, e.cost);
}
else {
mx = max(mx, res.first + e.cost - res.second);
}
}
return make_pair(sum, sum + mx);
}
int sum = 0, mx1 = -inf, mx2 = -inf;
for (edge e : g[pos]) {
pair<int, int> res = solve(e.to, pos);
sum += max(res.first, res.second);
int d = (res.first > res.second ? e.cost : res.first + e.cost - res.second);
if (d > mx1) {
mx2 = mx1;
mx1 = d;
}
else if (d > mx2) {
mx2 = d;
}
}
return make_pair(sum + mx1 + mx2, -inf);
}
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));
}
int ret = 0;
for (int i = 0; i < n; i++) {
if (g[i].size() <= 1) continue;
pair<int, int> res = solve(i, -1);
ret = max(ret, res.first);
}
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... |