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>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/trie_policy.hpp>
// #include <ext/rope>
using namespace std;
// using namespace __gnu_cxx;
// using namespace __gnu_pbds;
void Hollwo_Pelw();
signed main(){
#ifndef hollwo_pelw_local
if (fopen(".inp", "r"))
assert(freopen(".inp", "r", stdin)), assert(freopen(".out", "w", stdout));
#else
using namespace chrono;
auto start = steady_clock::now();
#endif
cin.tie(0), cout.tie(0) -> sync_with_stdio(0);
int testcases = 1;
// cin >> testcases;
for (int test = 1; test <= testcases; test++){
// cout << "Case #" << test << ": ";
Hollwo_Pelw();
}
#ifdef hollwo_pelw_local
auto end = steady_clock::now();
cout << "\nExecution time : " << duration_cast<milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
const int N = 2e5 + 5;
int n, dp[N][4];
vector<pair<int, int>> adj[N];
void solve(int u, int p) {
dp[u][0] = 0;
for (auto [v, w] : adj[u]) if (v != p) {
solve(v, u);
for (int i = 4; i --; ) {
if (i < 3) {
dp[u][i + 1] = max({
dp[u][i + 1],
dp[u][i] + w + max(dp[v][0], dp[v][2]),
});
}
dp[u][i] = max({
dp[u][i],
dp[u][i] + max(dp[v][0], dp[v][2]),
dp[u][i] + w + max(dp[v][1], dp[v][3])
});
}
}
}
void Hollwo_Pelw(){
cin >> n;
memset(dp, -0x3f, sizeof dp);
for (int i = 1, u, v, w; i < n; i++) {
cin >> u >> v >> w;
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
solve(1, 0);
cout << max({dp[1][0], dp[1][2]});
}
# | 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... |