Submission #935036

#TimeUsernameProblemLanguageResultExecution timeMemory
935036Darren0724Beads and wires (APIO14_beads)C++17
0 / 100
4 ms9820 KiB
#include <bits/stdc++.h> using namespace std; #define LCBorz ios_base::sync_with_stdio(false); cin.tie(0); #define int long long #define all(x) x.begin(), x.end() #define endl '\n' const int N=200005; const int INF=1e18; const int mod=998244353; int n; vector<pair<int,int>> adj[N]; vector dp(2,vector(N,(int)0)); void dfs(int k,int pa,int cost){ vector<int> t; int cnt=0; for(auto [j,c]:adj[k]){ if(j==pa)continue; dfs(j,k,c); t.push_back(j); cnt+=dp[0][j]; } sort(all(t),[&](int a,int b){return dp[1][a]-dp[0][a]>dp[1][b]-dp[0][b];}); int sz=t.size(); dp[0][k]=cnt; dp[1][k]=cnt+cost; if(sz>=1){ int tmp=cnt+dp[1][t[0]]-dp[0][t[0]]; dp[0][k]=max(dp[0][k],tmp+cost); } if(sz>=2){ int tmp=cnt+dp[1][t[0]]-dp[0][t[0]]+dp[1][t[1]]-dp[0][t[1]]; dp[0][k]=max(dp[0][k],tmp); dp[1][k]=max(dp[1][k],tmp+cost); } } int32_t main() { LCBorz; int n;cin>>n; for(int i=1;i<n;i++){ int a,b,c;cin>>a>>b>>c; adj[a].push_back({b,c}); adj[b].push_back({a,c}); } dfs(1,1,-INF); cout<<dp[0][1]<<endl; return 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...