Submission #366551

#TimeUsernameProblemLanguageResultExecution timeMemory
366551YJUBeads and wires (APIO14_beads)C++14
100 / 100
466 ms46940 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 n,x,y,c,ans; multiset<ll,greater<ll> > ms[N]; ll pac[N],dp[N],sum[N]; void DFS(ll nd,ll pa){ for(auto i:v[nd]){ if(i.X!=pa){ pac[i.X]=i.Y; DFS(i.X,nd); sum[nd]+=dp[i.X]; ms[nd].insert(sum[i.X]-dp[i.X]+i.Y); } } dp[nd]=max(sum[nd],(SZ(ms[nd])>0?sum[nd]+*ms[nd].begin()+pac[nd]:0LL)); } void move(ll rt,ll to,ll ed){ //remove sum[rt]-=dp[to]; ms[rt].erase(ms[rt].find(sum[to]-dp[to]+ed)); pac[rt]=ed; dp[rt]=max(sum[rt],(SZ(ms[rt])>0?sum[rt]+*ms[rt].begin()+pac[rt]:0LL)); sum[to]+=dp[rt]; pac[to]=0; ms[to].insert(sum[rt]-dp[rt]+ed); dp[to]=max(sum[to],(SZ(ms[to])>0?sum[to]+*ms[to].begin()+pac[to]:0LL)); } void reroot(ll nd,ll pa){ ans=max(ans,sum[nd]); for(auto i:v[nd]){ if(i.X==pa)continue; move(nd,i.X,i.Y); reroot(i.X,nd); move(i.X,nd,i.Y); } } 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); reroot(1,0); cout<<ans<<"\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...