Submission #777373

#TimeUsernameProblemLanguageResultExecution timeMemory
777373a_aguiloRace (IOI11_race)C++17
43 / 100
3052 ms41576 KiB
#include "race.h" #include<bits/stdc++.h> using namespace std; int n, k; const int maxN = 200000; vector<pair<int, int>> AdjList[maxN]; vector<bool> exists; int padre[maxN]; int subTreeSize[maxN]; void print(vector<pair<int, int>>& V){ for(pair<int, int>& pii: V){ cout << pii.first << " "; } cout << endl; } void preProcess(int node){ subTreeSize[node] = 1; for(pair<int, int> next: AdjList[node]){ //cout << node << " -> " << next.first << endl; if(next.first != padre[node] && exists[next.first]){ padre[next.first] = node; preProcess(next.first); subTreeSize[node] += subTreeSize[next.first]; } } //cout << node << " " << subTreeSize[node] << endl; } int find_Centroid(int node, int sze){ for(pair<int, int> next: AdjList[node]){ if(next.first == padre[node] || !exists[next.first]) continue; if(subTreeSize[next.first] > (sze/2)) return find_Centroid(next.first, sze); } return node; } void findDistances(int node, int currDist, int numEdges, vector<int>& V){ if(currDist > k) return; V[currDist] = min(V[currDist], numEdges); for(pair<int, int> next: AdjList[node]){ if(exists[next.first] && next.first != padre[node]){ padre[next.first] = node; findDistances(next.first, currDist+next.second, numEdges+1, V); } } } int solveSubTree(int node){ padre[node] = node; preProcess(node); int sze = subTreeSize[node]; node = find_Centroid(node, sze); //cout << node << endl; padre[node] = node; vector<int> lengths(k+1, 1e9); int ans = 1e9; for(pair<int, int> next: AdjList[node]){ if(next.first == padre[node]) continue; if(!exists[next.first]) continue; padre[next.first] = node; vector<int> prov(k+1, 1e9); findDistances(next.first, next.second, 1, prov); for(int i = 1; i < k; ++i) { ans = min(ans, lengths[i]+prov[k-i]); //cout << i << " " << lengths[i] << " " << prov[k-i] << " " << lengths[i]+prov[k-i] << " " << ans << endl; } for(int i = 1; i <= k; ++i) lengths[i] = min(lengths[i], prov[i]); /* cout << node << " -> " << next.first << endl; for(int i = 1; i <= k; ++i) cout << lengths[i] << " "; cout << endl;*/ } ans = min(ans, lengths[k]); exists[node] = false; for(pair<int, int> next: AdjList[node]){ if(exists[next.first]) ans = min(ans, solveSubTree(next.first)); } return ans; } int best_path(int N, int K, int H[][2], int L[]) { n = N; k = K; exists = vector<bool>(N, true); int a, b, c; for(int i = 0; i < N-1; ++i){ a = H[i][0]; b = H[i][1]; c = L[i]; AdjList[a].push_back({b, c}); AdjList[b].push_back({a, c}); } /* for(int i = 0; i < N-1; ++i){ cout << i << '\t'; print(AdjList[i]); }*/ int res = solveSubTree(0); if(res == 1e9) return -1; return res; } /* int main(){ int N, K; cin >> N >> K; int H[N-1][2]; int L[N-1]; for(int i = 0; i < N-1; ++i) cin >> H[i][0] >> H[i][1] >> L[i]; cout << best_path(N, K, H, L); 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...