이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#define ll long long
using namespace std;
ll n, a, b, c, dp[100000][2];
vector <array<ll, 2> > adj[100000];
void dfs(ll u, ll p, ll w) {
ll res = 0, sz = 0, z = -1e18;
vector <ll> V;
for (auto [v, x] : adj[u]) {
if (v == p) continue;
dfs(v, u, x);
res += dp[v][0];
V.push_back(dp[v][1]+x-dp[v][0]);
}
sort(V.begin(), V.end());
dp[u][0] = max(dp[u][0], res);
dp[u][1] = max(dp[u][1], res);
if (!V.empty()) dp[u][0] = max(dp[u][0], res + w + V.back());
if (V.size() >= 2) {
dp[u][0] = max(dp[u][0], res + V.back() + V[V.size()-2]);
dp[u][1] = max(dp[u][1], res + V.back() + V[V.size()-2]);
}
}
int main() {
cin >> n;
for (int i=0; i<n-1; ++i) {
cin >> a >> b >> c;
--a, --b;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
dfs(0, -1, -1e18);
cout << max(dp[0][0], dp[0][1]) << '\n';
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'void dfs(long long int, long long int, long long int)':
beads.cpp:15:13: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
15 | for (auto [v, x] : adj[u]) {
| ^
beads.cpp:13:15: warning: unused variable 'sz' [-Wunused-variable]
13 | ll res = 0, sz = 0, z = -1e18;
| ^~
beads.cpp:13:23: warning: unused variable 'z' [-Wunused-variable]
13 | ll res = 0, sz = 0, z = -1e18;
| ^
# | 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... |