Submission #876336

#TimeUsernameProblemLanguageResultExecution timeMemory
876336MONRace (IOI11_race)C++14
100 / 100
1038 ms80244 KiB
#include "race.h" #include<vector> #include<unordered_map> #include<bitset> #include<algorithm> #define fi first #define se second using namespace std; using pii = pair<int,int>; using ll = long long; constexpr int NMAX = 2e5 + 1; vector<pii> fii[NMAX]; bitset<NMAX> scos; int sz[NMAX],n,k,mx,ans; void build(int r,int p = -1) { sz[r] = 1; for(auto &it : fii[r]) if(it.first != p && !scos[it.first]) build(it.first,r),sz[r] += sz[it.first]; } int centroid(int r,int n,int p = -1) { for(auto &it : fii[r]) if(it.first != p && !scos[it.first] && sz[it.first] * 2 > n) return centroid(it.first,n,r); return r; } void dfs(int a,ll cost,int d,int p,bool add,unordered_map<int,int> &m,unordered_map<int,bool> &avem) { if(cost > k) return; if(add) { bool &u = avem[cost]; if(!u) u = 1 , m[cost] = d; else { int &h = m[cost]; h = min(h,d); } } else { bool &u = avem[k-cost]; if(u) ans = min(ans,m[k-cost]+d); } for(auto &it : fii[a]) if(!scos[it.fi] && it.fi != p) dfs(it.fi,cost+it.se,d+1,a,add,m,avem); mx = max(mx,d); } void descompune(int root) { build(root); int c = centroid(root,sz[root]); unordered_map<int,int> nou; unordered_map<int,bool> avem; avem[0] = 1; for(auto &it : fii[c]) if(!scos[it.fi]) dfs(it.fi,it.se,1,c,0,nou,avem), dfs(it.fi,it.se,1,c,1,nou,avem); mx = 0; scos[c] = 1; for(auto &it : fii[c]) if(!scos[it.fi]) descompune(it.fi); } int best_path(int N,int K,int H[][2],int L[]) { n = N,k = K; ans = n + 1; for(int i = 0 ; i < n - 1; i ++) { fii[H[i][0]].push_back({H[i][1],L[i]}); fii[H[i][1]].push_back({H[i][0],L[i]}); } descompune(0); if(ans == n + 1) ans = -1; return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...