이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int const maxn = 202020;
int const inf = 0x3f3f3f3f;
struct Edge {
int u, w, id;
};
struct FEdge {
int a, b, w;
};
vector<Edge> v[maxn*3];
vector<FEdge> eg;
int dp[3*maxn][2];
inline int invalid(int x, int p) {
return v[x].size() == 1;
}
int solve(int id, int t = 0) {
int x = eg[id].b;
int p = eg[id].a;
if (dp[id][t] >= 0) return dp[id][t];
int d = 0;
if (t == 0) {
for (Edge const& e : v[x]) {
if (e.u == p) continue;
if (!invalid(e.u, x)) {
d += max(solve(e.id, 1) + e.w, solve(e.id, 0));
}
}
}
else {
int sum = solve(id, 0);
for (Edge const& e : v[x]) {
if (e.u == p) continue;
if (!invalid(e.u, x))
d = max(d, sum - max(solve(e.id, 1) + e.w, solve(e.id, 0))
+ e.w + solve(e.id, 0));
else
d = max(d, sum + e.w);
}
}
return dp[id][t] = d;
}
int main() {
memset(dp, -1, sizeof(dp));
ios::sync_with_stdio(false), cin.tie(nullptr);
int n;
cin >> n;
for (int i = 0; i < n-1; i++) {
int a, b, w;
cin >> a >> b >> w;
v[a].push_back({b, w, i*2});
v[b].push_back({a, w, i*2+1});
eg.push_back({a, b, w});
eg.push_back({b, a, w});
}
for (int i = 1; i <= n; i++) {
v[i+n].push_back({i, -inf, 2*n - 3 + i});
eg.push_back({i+n, i, -inf});
}
int ans = 0;
for (int i = 2*n-2; i < 3*n-2; i++) {
ans = max(ans, solve(i));
}
cout << ans << "\n";
}
# | 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... |