제출 #235827

#제출 시각아이디문제언어결과실행 시간메모리
235827balbit구슬과 끈 (APIO14_beads)C++14
0 / 100
8 ms5120 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define int ll #define pii pair<int, int> #define f first #define s second #define REP(i,n) for (int i = 0; i<n; ++i) #ifdef BALBIT #define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<": ", _do(__VA_ARGS__) template<typename T> void _do(T && x){cerr<<x<<endl;} template<typename T, typename ...S> void _do(T && x, S&&...y){cerr<<x<<", "; _do(y...);} #define IOS() #else #define IOS() ios::sync_with_stdio(0), cin.tie(0) #define endl '\n' #define bug(...) #endif // BALBIT #define SZ(x) (int)(x.size()) #define ALL(x) (x).begin(), (x).end() #define pb push_back const int maxn = 2e5+5; vector<pii> g[maxn]; ll dp[maxn][2]; void dfs(int v, int p, ll w) { vector<ll> mo; ll sg = 0; for (pii u : g[v]) { if (u.f == p) continue; dfs(u.f,v,u.s); mo.pb(dp[u.f][0] - dp[u.f][1] + u.s); sg += dp[u.f][1]; } sort(ALL(mo), greater<ll>()); dp[v][0] = sg; if (SZ(mo) >= 2) { dp[v][0] = max(dp[v][0], sg + mo[0] + mo[1]); } dp[v][1] = dp[v][0]; if (SZ(mo) >= 1) { dp[v][1] = max(dp[v][1], sg + mo[0] + w); } } signed main(){ IOS(); int n; cin>>n; for (int i = 0; i<n-1; ++i) { int a,b,w; cin>>a>>b>>w; --a;--b;g[a].pb({b,w});g[b].pb({a,w}); } dfs(0,-1,0); cout<<dp[0][0]<<endl; } /* 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...