# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1000624 | nmts | 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 <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Highway {
int to;
int length;
};
void dfs(int node, int parent, int K, int currentLength, int currentHighways, vector<vector<Highway>>& graph, int& minHighways) {
if (currentLength > K) {
return;
}
if (currentLength == K) {
minHighways = min(minHighways, currentHighways);
return;
}
for (const Highway& highway : graph[node]) {
if (highway.to != parent) {
dfs(highway.to, node, K, currentLength + highway.length, currentHighways + 1, graph, minHighways);
}
}
}
int best_path(int N, int K, vector<vector<int>>& H, vector<int>& L) {
vector<vector<Highway>> graph(N);
for (int i = 0; i < N - 1; ++i) {
graph[H[i][0]].push_back({H[i][1], L[i]});
graph[H[i][1]].push_back({H[i][0], L[i]});
}
int minHighways = INT_MAX;
for (int i = 0; i < N; ++i) {
dfs(i, -1, K, 0, 0, graph, minHighways);
}
return (minHighways == INT_MAX) ? -1 : minHighways;
}