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], cekori[1000005];
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, parent);
subtree_size[teme]+=subtree_size[next];
}
}
int get_centroid(int teme, int parent, int tree_size) {
for (auto [next, dolz]:G[teme]) {
if (next==parent||is_removed[next]) continue;
if (subtree_size[next]*2>tree_size)
return get_centroid(next, teme, tree_size);
}
return teme;
}
void check(int teme, int parent, int dolz, int depth) {
if (dolz>k) return;
if (cekori[k-dolz]!=-1) 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[dolz]==-1) 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 clear_cekori(int teme, int parent, int dolz) {
if (dolz>k) return ;
cekori[dolz]=-1;
for (auto [next, dist]:G[teme]) {
if (next==parent||is_removed[next]) continue;
clear_cekori(next, teme, dolz+dist);
}
}
void build_centroid_decomp(int teme=0){
get_subtree_size(teme, -1);
int centroid=get_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);
}
for (auto [next, dist]:G[centroid]) {
if (is_removed[next]) continue;
clear_cekori(next, centroid, dist);
}
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;
memset(cekori, -1, sizeof(cekori));
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=N+2;
build_centroid_decomp();
if (rez==N+2) rez=-1;
return rez;
}
Compilation message (stderr)
race.cpp: In function 'void get_subtree_size(int, int)':
race.cpp:12:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
12 | for (auto [next, dist]:G[teme]) {
| ^
race.cpp: In function 'int get_centroid(int, int, int)':
race.cpp:20:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
20 | for (auto [next, dolz]:G[teme]) {
| ^
race.cpp: In function 'void check(int, int, int, int)':
race.cpp:31:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
31 | for (auto [next, dist]:G[teme]) {
| ^
race.cpp: In function 'void build(int, int, int, int)':
race.cpp:41:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
41 | for (auto [next, dist]:G[teme]) {
| ^
race.cpp: In function 'void clear_cekori(int, int, int)':
race.cpp:50:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
50 | for (auto [next, dist]:G[teme]) {
| ^
race.cpp: In function 'void build_centroid_decomp(int)':
race.cpp:60:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
60 | for (auto [next, dist]:G[centroid]) {
| ^
race.cpp:65:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
65 | for (auto [next, dist]:G[centroid]) {
| ^
race.cpp:70:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
70 | for (auto [next, dist]:G[centroid]) {
| ^
# | 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... |