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>
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][2];
void dfs(int v, int p) {
vector<int> dp(3, -INF);
dp[0] = 0;
for (auto [to, c] : g[v]) {
if (to != p) {
dfs(to, v);
vector<int> dpp(3);
for (int j = 0; j < 2; j++) {
dpp[j + 1] = max(dpp[j + 1], dp[j] + res[to][0] + c);
}
for (int j = 0; j < 3; j++) {
dpp[j] = max(dpp[j], dp[j] + max(res[to][1] + c, res[to][0]));
}
swap(dp, dpp);
}
}
res[v][0] = max(dp[0], dp[2]);
res[v][1] = dp[1];
}
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 << res[0][0] << endl;
return 0;
}
Compilation message (stderr)
beads.cpp: In function 'void dfs(long long int, long long int)':
beads.cpp:19:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
19 | 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... |