제출 #1046205

#제출 시각아이디문제언어결과실행 시간메모리
1046205I_FloPPed21경주 (Race) (IOI11_race)C++14
0 / 100
3 ms22364 KiB
#include <bits/stdc++.h> using namespace std; map<int,long long> mp[200005] ; vector<pair<int,int>> adj[200005]; long long h[200005][2], target = 0,sum[200005],height[200005],ans = 1e9,n,k; void precomp(int nod, int p, int hd) { height[nod] = hd ; for ( auto u : adj[nod] ) { if ( u.first != p ) { sum[u.first] += sum[nod] + u.second; precomp(u.first,nod,hd + 1); } } } void small(int nod,int p ) { long long real_target = target + 2 * sum[nod]; for ( auto u : adj[nod] ) { if ( u.first != p ) { small(u.first,nod); } } for ( auto u : adj[nod] ) { if( u.first != p ) { if( mp[u.first].size() > mp[nod].size()) swap(mp[nod],mp[u.first]); for ( auto k : mp[u.first] ) { long long real = real_target - k.first ; /* if( nod == 6 && u.first == 8 ) { cout << real << '\n'; }*/ if ( mp[nod][real] != 0 ) { //cout << nod << " " << u.first << " " << k.first << " " << k.second << '\n'; ans = min ( long(ans), long(k.second + mp[nod][real] - 2 * height[nod])); } } for ( auto k : mp[u.first]) {if( mp[nod][k.first] == 0 ) mp[nod][k.first] = k .second; else mp[nod][k.first] = min(mp[nod][k.first],k.second); } } } /*if( nod == 6) { cout << mp[nod][real_target]<< '\n'; }*/ } int best_path(int n, int k, int h[][2], int costs [] ) { if( k == 1 ) { return 0 ; } target =k ; for ( int i = 1; i < n ; i ++ ) { adj[h[i][1]].push_back({h[i][0],costs[i]}); adj[h[i][0]].push_back({h[i][1],costs[i]}); } precomp(0,-1,1); small(0,-1); if( ans == 1e9) return -1; else return ans ; } /* int main() { cin >> n >> k; for ( int i = 1; i < n ; i ++ ) { cin >> h[i][0] >> h[i][1] >> v[i] ; } cout << best_path(n,k,h,v); 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...