Submission #734185

#TimeUsernameProblemLanguageResultExecution timeMemory
734185SanguineChameleonBeads and wires (APIO14_beads)C++17
0 / 100
3 ms4948 KiB
#include <bits/stdc++.h> using namespace std; void just_do_it(); int main() { #ifdef KAMIRULEZ freopen("kamirulez.inp", "r", stdin); freopen("kamirulez.out", "w", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(0); just_do_it(); return 0; } const int maxN = 2e5 + 20; vector<pair<int, int>> adj[maxN]; int par_w[maxN]; int dp[maxN][2]; void dfs(int u, int p) { int tot = 0; vector<int> gain; for (auto x: adj[u]) { int v = x.first; int w = x.second; if (v != p) { par_w[v] = w; dfs(v, u); tot += max(dp[v][0], dp[v][1]); gain.push_back(w + dp[v][0] - max(dp[v][0], dp[v][1])); } } sort(gain.begin(), gain.end(), greater<>()); dp[u][0] = max(tot, (int)gain.size() >= 2 ? tot + gain[0] + gain[1] : -1); dp[u][1] = max(dp[u][0], !gain.empty() ? tot + gain[0] + par_w[u] : -1); } void just_do_it() { int n; cin >> n; for (int i = 0; i < n - 1; i++) { int u, v, w; cin >> u >> v >> w; adj[u].emplace_back(v, w); adj[v].emplace_back(u, w); } dfs(1, -1); cout << dp[1][0]; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...