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];
bool is_removed[200005];
int subtree_size[200005];
int rez;
map<int,int> cekori;
int k;
int get_subtree_size(int teme, int parent=-1) {
subtree_size[teme]=1;
for (auto [next, dolz]:G[teme]) {
if (next==parent||is_removed[next]) continue;
subtree_size[teme]+=get_subtree_size(next, teme);
}
return subtree_size[teme];
}
int get_centroid(int teme, int tree_size, int parent=-1) {
for (auto [next, dolz]:G[teme]) {
if (next==parent||is_removed[next]) continue;
if (subtree_size[next]*2>tree_size)
return get_centroid(next, tree_size, teme);
}
return teme;
}
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 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_centroid_decomp(int teme=0) {
int centroid=get_centroid(teme, get_subtree_size(teme));
for (auto [next, dist]:G[centroid]) {
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[]) {
rez=INT_MAX;
for (int i=0;i<n;i++)
G[i].clear();
memset(is_removed, 0, sizeof(is_removed));
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]});
}
k=K;
build_centroid_decomp();
if (rez==INT_MAX) return -1;
return rez;
}
Compilation message (stderr)
race.cpp: In function 'int get_subtree_size(int, int)':
race.cpp:14:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
14 | for (auto [next, dolz]:G[teme]) {
| ^
race.cpp: In function 'int get_centroid(int, int, int)':
race.cpp:22:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
22 | for (auto [next, dolz]:G[teme]) {
| ^
race.cpp: In function 'void build(int, int, int, int)':
race.cpp:35:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
35 | for (auto [next, dist]:G[teme]) {
| ^
race.cpp: In function 'void check(int, int, int, int)':
race.cpp:45:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
45 | for (auto [next, dist]:G[teme]) {
| ^
race.cpp: In function 'void build_centroid_decomp(int)':
race.cpp:53:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
53 | for (auto [next, dist]:G[centroid]) {
| ^
race.cpp:59:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
59 | 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... |