제출 #538694

#제출 시각아이디문제언어결과실행 시간메모리
538694S2speed구슬과 끈 (APIO14_beads)C++17
0 / 100
3 ms2644 KiB
#include<bits/stdc++.h> using namespace std; #define all(x) x.begin() , x.end() typedef long long ll; typedef pair<ll , ll> pll; const ll maxn = 1e5 + 16 , inf = 2e16; ll dp[maxn][2] , w[maxn]; vector<pll> adj[maxn]; void DFS(ll r , ll par){ ll res = 0 , mx1 = -inf , mx2 = -inf; for(auto p : adj[r]){ ll i = p.first , w = p.second; if(i == par) continue; DFS(i , r); ll h = max(dp[i][0] , dp[i][1] + w) , o = dp[i][0] + w - h; res += h; if(o > mx2){ mx2 = o; } if(o > mx1){ swap(mx1 , mx2); } } dp[r][1] = res + mx1; dp[r][0] = max(res , res + mx1 + mx2); return; } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n; cin>>n; for(ll i = 1 ; i < n ; i++){ ll v , u , w; cin>>v>>u>>w; v--; u--; adj[v].push_back({u , w}); adj[u].push_back({v , w}); } DFS(0 , -1); cout<<dp[0][0]<<'\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...