Submission #964502

#TimeUsernameProblemLanguageResultExecution timeMemory
96450212345678Beads and wires (APIO14_beads)C++17
0 / 100
2 ms6796 KiB
#include <bits/stdc++.h> using namespace std; const int nx=2e5+5; #define ll long long ll n, u, v, w, dp[2][nx]; vector<pair<ll, ll>> d[nx]; void dfs(int u, int p, int pw) { vector<int> df; for (auto [v, w]:d[u]) if (v!=p) dfs(v, u, w), dp[0][u]+=dp[1][v], df.push_back(w+dp[0][v]-dp[1][v]); sort(df.begin(), df.end()); reverse(df.begin(), df.end()); if (w!=-1&&df.size()>=1) dp[1][u]=max(dp[0][u], dp[0][u]+df[0]+pw); if (df.size()>=2) dp[0][u]=max(dp[0][u], dp[0][u]+df[0]+df[1]); } int main() { cin.tie(NULL)->sync_with_stdio(false); cin>>n; for (int i=1; i<n; i++) cin>>u>>v>>w, d[u].push_back({v, w}), d[v].push_back({u, w}); dfs(1, 1, -1); cout<<dp[0][1]; } /* 10 4 10 2 1 2 21 1 3 13 6 7 1 7 9 5 2 4 3 2 5 8 1 6 55 6 8 34 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...