이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |