Submission #366517

#TimeUsernameProblemLanguageResultExecution timeMemory
366517YJUBeads and wires (APIO14_beads)C++14
0 / 100
4 ms4972 KiB
#include<bits/stdc++.h> #pragma GCC optimize("unroll-loops,no-stack-protector,Ofast") using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll,ll> pll; const ll MOD=1e9+7; const ll MOD2=998244353; const ll N=2e5+5; const ld pi=acos(-1); const ll INF=(1LL<<60); #define SQ(i) ((i)*(i)) #define REP(i,n) for(ll i=0;i<n;i++) #define REP1(i,n) for(ll i=1;i<=n;i++) #define pb push_back #define mp make_pair #define X first #define Y second #define setp setprecision #define lwb lower_bound #define SZ(_a) (ll)_a.size() vector<pll> v[N]; ll dp[N][2],n,x,y,c; void DFS(ll nd,ll pa,ll pac){ vector<ll> vec; ll sum=0; for(auto i:v[nd]){ if(i.X!=pa){ DFS(i.X,nd,i.Y); sum+=dp[i.X][0]; vec.pb(dp[i.X][1]-dp[i.X][0]+i.Y); } } sort(vec.rbegin(),vec.rend()); dp[nd][1]=(SZ(vec)>1?sum+vec[0]+vec[1]:0LL); dp[nd][0]=max(dp[nd][1],(SZ(vec)>0?sum+vec[0]+pac:0LL)); } int main(){ ios_base::sync_with_stdio(0);cin.tie(0); cin>>n; REP1(i,n-1){ cin>>x>>y>>c; v[x].pb(mp(y,c)); v[y].pb(mp(x,c)); } DFS(1,0,0); cout<<dp[1][1]<<"\n"; return 0; } /* 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...