이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define ld double
#define ll __int128
mt19937 rnd(51);
const int N = 2e5 + 10, INF = 1e18;
vector<pair<int,int>> g[N];
int res[N][3];
void dfs(int v, int p) {
bool was = 0;
vector<vector<int>> dp(3, vector<int>(3, -INF));
dp[0][0] = 0;
for (auto [to, c] : g[v]) {
if (to != p) {
dfs(to, v);
was = 1;
vector<vector<int>> dpp(3, vector<int>(3, -INF));
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 3; k++) {
dpp[j][k] = max({dpp[j][k], dp[j][k] + res[to][2] + c, dp[j][k] + res[to][1]});
if (j + 1 < 3) {
if (k != 1) dpp[j + 1][k] = max(dpp[j + 1][k], dp[j][k] + res[to][1] + c);
}
if (k == 0) {
if (j == 0) {
dpp[j][1] = max(dpp[j][1], dp[j][k] + res[to][0]);
}
if (j + 1 < 3) {
dpp[j + 1][2] = max(dpp[j + 1][2], dp[j][k] + res[to][0] + c);
}
} else if (k == 2) {
if (j == 1) {
dpp[j + 1][2] = max(dpp[j + 1][2], dp[j][k] + res[to][0] + c);
}
}
}
}
swap(dp, dpp);
}
}
if (!was) {
res[v][0] = -INF;
res[v][1] = 0;
res[v][2] = -INF;
} else {
res[v][0] = max({dp[2][0], dp[2][1], dp[2][2], dp[0][1]});
res[v][1] = dp[0][0];
res[v][2] = max({dp[1][0], dp[1][2]});
}
}
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 0; i < n - 1; i++) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
g[a].pb({b, c});
g[b].pb({a, c});
}
dfs(0, -1);
cout << max(res[0][0], res[0][1]) << endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
beads.cpp: In function 'void dfs(long long int, long long int)':
beads.cpp:20:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
20 | for (auto [to, c] : g[v]) {
| ^
# | 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... |