Submission #650205

#TimeUsernameProblemLanguageResultExecution timeMemory
650205ymmBeads and wires (APIO14_beads)C++17
0 / 100
3 ms4948 KiB
#include <bits/stdc++.h> #define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x) #define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x) typedef long long ll; typedef std::pair<int, int> pii; typedef std::pair<ll , ll > pll; using namespace std; const int N = 200'010; vector<pii> A[N]; int n; tuple<int,int,int> dfs(int v, int p, int w) { int sum = 0, sum1 = 0; vector<int> vec; // 2 - 0 + w for (auto [u, e] : A[v]) { if (u == p) continue; auto [x, y, z] = dfs(u, v, e); vec.push_back(z - x + e); sum += x; sum1 += y; } sort(vec.rbegin(), vec.rend()); int x = 0, y = 0, z = 0; if (vec.size() >= 1 && p >= 0) x = sum + w + vec[0]; if (vec.size() >= 2) z = sum + vec[0] + vec[1]; y = max(x, z); x = max(x, sum); y = max(y, sum1); z = max(z, sum1); return {x, y, z}; } int main() { cin.tie(0) -> sync_with_stdio(false); cin >> n; Loop (i,1,n) { int v, u, w; cin >> v >> u >> w; --v; --u; A[v].push_back({u, w}); A[u].push_back({v, w}); } cout << get<1>(dfs(0, -1, 0)) << '\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...