# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
495676 | Pietra | Race (IOI11_race) | C++17 | 0 ms | 0 KiB |
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 ;
#include "race.h"
#define int long long
const int maxn = 2e5 + 5 ;
const int inf = 1e18 ;
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[][2], int L[]) {
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]}) ;
}
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" ;
// }