제출 #290117

#제출 시각아이디문제언어결과실행 시간메모리
290117REALITYNB경주 (Race) (IOI11_race)C++14
9 / 100
3070 ms13344 KiB
#include <bits/stdc++.h> #include "race.h" #define pb push_back #define mp make_pair #define pii pair<int,int> #define F first #define S second using namespace std; int n , k ; const int mxn = 2e5+1 ; vector<pii> adj[mxn] ; vector<int> sz(mxn) , w(mxn) ,h(mxn) ; void sub(int a , int p){ sz[a]=1 ; if(p!=-1) h[a]=h[p]+1 ; for(pii& x : adj[a]){ if(x.F==p) continue ; w[x.F]=w[a]+x.S ; sub(x.F,a) ; sz[a]+=sz[x.F] ; } } int mn = 1e9 ; vector<int> p[mxn] ; map<int,multiset<int>> cnt ; void dfs(int a , int pp , int keep){ int bigchild = -1 , mx = -1 ; for(pii& y : adj[a]){ int x = y.F ; if(x==pp) continue ; if(sz[x]>mx){ mx=sz[x] ; bigchild= x ; } } for(pii& y : adj[a]){ int x = y.F ; if(x==pp||x==bigchild) continue ; dfs(x,a,0) ; } if(bigchild!=-1){ dfs(bigchild,a,1) ; swap(p[bigchild],p[a]) ; // p[bigchild].swap(p[a]) ; } p[a].push_back(a) ; int target = k+2*w[a] ; if(cnt.count(target-w[a])) mn = min(mn,*(cnt[target-w[a]].begin())-h[a]) ; cnt[w[a]].insert(h[a]) ; for(pii& y : adj[a]){ if(y.F==pp||y.F==bigchild) continue ; for(int& x : p[y.F]){ if(cnt.count(target-w[x])) mn = min(*(cnt[target-w[x]].begin())+h[x]-h[a]*2,mn) ; p[a].pb(x) ; } for(int& x :p[y.F]) cnt[w[a]].insert(h[x]) ; } if(keep==0){ for(int& x: p[a]){ cnt[w[x]].erase(cnt[w[x]].find(h[x])) ; if(cnt[w[x]].empty()) cnt.erase(cnt.find(w[x])) ; } } } int solve(){ sub(1,-1) ; dfs(1,-1,1) ; if(mn==1e9) return -1; return mn ; } int best_path(int N , int K , int H[][2] , int L[]){ n = N ; k = K ; if(1000<=N) return 0; for(int i=0;i<n;i++){ adj[H[i][0]].push_back(mp(H[i][1],L[i])); adj[H[i][1]].push_back(mp(H[i][0],L[i])) ; } return solve() ; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...