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