Submission #1064210

#TimeUsernameProblemLanguageResultExecution timeMemory
1064210kkkkkkkkRace (IOI11_race)C++17
100 / 100
657 ms41696 KiB
#include <bits/stdc++.h> using namespace std; vector<pair<int,int> > G[200005]; int is_removed[200005], subtree_size[200005]; map<int,int> cekori; int k; int rez; void get_subtree_size(int teme, int parent) { subtree_size[teme]=1; for (auto [next, dist]:G[teme]){ if (next==parent||is_removed[next]) continue; get_subtree_size(next, teme); subtree_size[teme]+=subtree_size[next]; } } int find_centroid(int teme, int parent, int nodes){ for (auto [next, dist]:G[teme]){ if (next==parent||is_removed[next]) continue; if (subtree_size[next]>=nodes/2) return find_centroid(next, teme, nodes); } return teme; } void check(int teme, int parent, int dolz, int depth){ if (dolz>k) return ; if (cekori.find(k-dolz)!=cekori.end()) rez=min(rez, cekori[k-dolz]+depth); for (auto [next, dist]:G[teme]){ if (next==parent||is_removed[next]) continue; check(next, teme, dolz+dist, depth+1); } } void build(int teme, int parent, int dolz, int depth){ if (dolz>k) return ; if (cekori.find(dolz)==cekori.end()) cekori[dolz]=depth; else cekori[dolz]=min(cekori[dolz], depth); for (auto [next, dist]:G[teme]){ if (next==parent||is_removed[next]) continue; build(next, teme, dolz+dist, depth+1); } } void build_centroid_decomp(int teme) { get_subtree_size(teme, -1); int centroid=find_centroid(teme, -1, subtree_size[teme]); cekori[0]=0; for (auto [next, dist]:G[centroid]) { if (is_removed[next]) continue; check(next, centroid, dist, 1); build(next, centroid, dist, 1); } cekori.clear(); is_removed[centroid]=1; for (auto [next, dist]:G[centroid]) { if (is_removed[next]) continue; build_centroid_decomp(next); } } int best_path(int N, int K, int H[][2], int L[]) { k=K; for (int i=0;i<N-1;i++) { G[H[i][0]].push_back({H[i][1], L[i]}); G[H[i][1]].push_back({H[i][0], L[i]}); } rez=INT_MAX; build_centroid_decomp(0); if (rez==INT_MAX) rez=-1; return rez; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...