이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std ;
#include "race.h"
const int maxn = 2e5 + 5 ;
const int inf = 1e9 ;
int k, f[maxn], mark[maxn], n, ans, sz[maxn] ;
vector<pair<int,int>> grafo[maxn] ;
void dfs(int v, int p){
sz[v] = 1 ;
for(auto a : grafo[v]){
if(a.first == p) continue ;
dfs(a.first, v) ;
sz[v] += sz[a.first] ;
}
}
int find_cent(int v, int p, int szz){
for(auto a : grafo[v]){
if(a.first == p || mark[a.first] || 2*sz[a.first] <= szz) continue ;
find_cent(a.first, v, szz) ;
}
return v ;
}
void make_count(bool type, int v, int p, int dist, int lvl){
if(dist > k) return ;
if(type == 0) ans = min(ans, f[k-dist] + lvl) ;
else f[dist] = min(f[dist], lvl) ;
for(auto a : grafo[v]){
if(a.first == p || mark[a.first]) continue ;
make_count(type, a.first, v, dist + a.second, lvl + 1) ;
}
}
void make_graph(int v, int p){
dfs(v, p) ;
int c = find_cent(v, p, sz[v]) ;
mark[c] = 1 ;
f[0] = 1 ;
for(auto a : grafo[c]){
if(a.first == p || mark[a.first]) continue ;
make_count(0, a.first, c, a.second, 1) ;
make_count(1, a.first, c, a.second, 1) ;
}
for(auto a : grafo[c]){
if(a.first == p || mark[a.first]) continue ;
make_graph(a.first, c) ;
}
}
int best_path(int N, int K, int H[maxn][2], int L[maxn]) {
for(int i = 1 ; i <= N ; i++){
H[i][0]++, H[i][1]++ ;
grafo[H[i][0]].push_back({H[i][1], L[i]}) ; grafo[H[i][1]].push_back({H[i][0], L[i]}) ;
}
k = K ;
for(int i = 1 ; i <= K ; i++) f[i] = inf ;
ans = inf ;
make_graph(1, 0) ;
return (ans == inf ? -1 : ans) ;
}
// int32_t main(){
// ans = inf ;
// cin >> n >> K ;
// for(int i = 1 ; i < n ; i++){
// int a, b, c ;
// cin >> a >> b >> c ;
// a++, b++ ;
// grafo[a].push_back({b, c}), grafo[b].push_back({a, c}) ;
// }
// for(int i = 1 ; i <= K ; i++) f[i] = inf ;
// make_graph(1, 0) ;
// cout << ans << "\n" ;
// }
# | 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... |