제출 #538709

#제출 시각아이디문제언어결과실행 시간메모리
538709S2speed구슬과 끈 (APIO14_beads)C++17
100 / 100
558 ms55360 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 = 2e5 + 16 , inf = 2e16; ll dp[maxn][2] , w[maxn]; vector<pll> adj[maxn]; multiset<ll , greater<ll>> s[maxn]; void dDFS(ll r , ll par){ ll res = 0 , mx; s[r].insert(-inf); for(auto p : adj[r]){ ll i = p.first , w = p.second; if(i == par) continue; dDFS(i , r); ll h = max(dp[i][0] , dp[i][1] + w) , o = dp[i][0] + w - h; res += h; s[r].insert(o); } dp[r][1] = res + *s[r].begin(); dp[r][0] = res; return; } void reroot(ll v , ll u , ll w){ ll h = max(dp[u][0] , dp[u][1] + w) , o = dp[u][0] + w - h; s[v].erase(s[v].find(o)); dp[v][0] -= h; dp[v][1] = dp[v][0] + *s[v].begin(); h = max(dp[v][0] , dp[v][1] + w); o = dp[v][0] + w - h; s[u].insert(o); dp[u][0] += h; dp[u][1] = dp[u][0] + *s[u].begin(); return; } ll ans = 0; void rDFS(ll r , ll par){ ans = max(ans , dp[r][0]); for(auto p : adj[r]){ ll i = p.first , w = p.second; if(i == par) continue; reroot(r , i , w); rDFS(i , r); reroot(i , r , w); } 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}); } dDFS(0 , -1); rDFS(0 , -1); cout<<ans<<'\n'; return 0; }

컴파일 시 표준 에러 (stderr) 메시지

beads.cpp: In function 'void dDFS(ll, ll)':
beads.cpp:16:15: warning: unused variable 'mx' [-Wunused-variable]
   16 |  ll res = 0 , mx;
      |               ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...