This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |