Submission #972366

#TimeUsernameProblemLanguageResultExecution timeMemory
972366pccBeads and wires (APIO14_beads)C++17
28 / 100
1054 ms6748 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define pll pair<ll,ll> #define pii pair<int,int> #define fs first #define sc second #define tlll tuple<ll,ll,ll> const int mxn = 2e5+10; vector<pii> tree[mxn]; int dp[mxn][2]; int N; int ans = 0; void dfs(int now,int par){ dp[now][0] = 0; dp[now][1] = -1e9; vector<int> v; for(auto [nxt,w]:tree[now]){ if(nxt == par)continue; dfs(nxt,now); dp[now][0] += max(dp[nxt][0],dp[nxt][1]+w); v.push_back(dp[nxt][0]+w-max(dp[nxt][0],dp[nxt][1]+w)); } sort(v.rbegin(),v.rend()); if(!v.empty())dp[now][1] = dp[now][0]+v[0]; return; } int main(){ ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>N; for(int i = 1;i<N;i++){ int a,b,c; cin>>a>>b>>c; tree[a].push_back(pii(b,c)); tree[b].push_back(pii(a,c)); } for(int i = 1;i<=N;i++){ dfs(i,i); ans = max(ans,dp[i][0]); } cout<<ans<<'\n'; 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...