This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2e5 + 10;
int n;
vector <pair <int, int> > g[N];
int f[N][2];
void dfs(int u, int p) {
for (auto [v, w]: g[u]) {
if (v == p) continue;
dfs(v, u);
}
int cur[3] = {0, (int)- 2e9 - 10, (int)- 2e9 - 10};
for (auto [v, w]: g[u]) {
if (v == p) continue;
int ncur[3] = {0, (int)- 2e9 - 10, (int)- 2e9 - 10};
for (int i = 0; i < 3; ++i) {
ncur[i] = max(ncur[i], cur[i] + max(f[v][0], f[v][1] + w));
if (i + 1 < 3) ncur[i + 1] = max(ncur[i + 1], cur[i] + f[v][0] + w);
}
for (int i = 0; i < 3; ++i) cur[i] = ncur[i];
}
f[u][0] = max(cur[0], cur[2]);
f[u][1] = cur[1];
}
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v, w;
cin >> u >> v >> w;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
dfs(1, 0);
cout << f[1][0] << '\n';
}
Compilation message (stderr)
beads.cpp: In function 'void dfs(int, int)':
beads.cpp:20:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
20 | for (auto [v, w]: g[u]) {
| ^
beads.cpp:25:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
25 | for (auto [v, w]: g[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... |