이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 200005;
const ll INF = 1e18;
ll dp[N][2][2];
vector<pair<int, int>> adj[N];
void add(ll &x, ll y) {
x = max(x + y, -INF);
}
void dfs(int u, int p) {
ll sum = 0, best0 = -INF, best1 = -INF, best01 = -INF;
ll best10 = -INF, best11 = -INF;
for (auto e : adj[u]) {
int v, w; tie(v, w) = e;
if (v == p) continue; dfs(v, u);
add(dp[u][0][0], max(dp[v][0][0], dp[v][1][0] + w));
add(dp[u][0][1], max({dp[v][0][0], dp[v][1][0] + w,
dp[v][0][1], dp[v][1][1] + w}));
add(sum, max(dp[v][0][0], dp[v][1][0] + w));
best01 = max(best01, max(best0 + max(dp[v][0][0],
dp[v][0][1]) + w, best1 + dp[v][0][0] + w) -
max(dp[v][0][0], dp[v][1][0] + w));
best0 = max(best0, dp[v][0][0] + w -
max(dp[v][0][0], dp[v][1][0] + w));
best1 = max(best1, dp[v][0][1] + w -
max(dp[v][0][0], dp[v][1][0] + w));
best10 = max(best10, dp[v][0][0] + w
- max(dp[v][0][0], dp[v][1][0] + w));
best11 = max(best11, max(dp[v][0][0], dp[v][0][1])
+ w - max(dp[v][0][0], dp[v][1][0] + w));
}
dp[u][1][0] = dp[u][1][1] = sum;
add(dp[u][1][0], best10);
add(sum, best01);
dp[u][0][1] = max(dp[u][0][1], sum);
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n; cin >> n;
for (int i = 1; i < n; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
dfs(1, 0); cout << max(dp[1][0][0], dp[1][0][1]);
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'void dfs(int, int)':
beads.cpp:22:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
22 | if (v == p) continue; dfs(v, u);
| ^~
beads.cpp:22:31: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
22 | if (v == p) continue; dfs(v, u);
| ^~~
# | 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... |