| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 1315453 | vlomaczk | Race (IOI11_race) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
typedef long long ll;
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int n,kk,inf=1e7;
int ans = inf;
int M = 200'010;
vector<vector<pair<int, int>>> g(M);
void dfs(int v, int p, int d, int c) {
if(c==kk) ans = min(ans,d);
for(auto[u,k] : g[v]) {
if(u==p) continue;
dfs(u,v,d+1,c+k);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n=N; kk=K;
for(ll i=0; i<n-1; ++i) {
ll a = H[i][0];
ll b = H[i][1];
ll c = L[i];
g[a].push_back({b,c});
g[b].push_back({a,c});
}
for(int i=0; i<n; ++i) {
dfs(i,-1, 0,0);
}
if(ans==inf) return -1;
return ans;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
static int H[200000][2];
static int L[200000];
for(int i = 0; i < n-1; ++i) {
cin >> H[i][0] >> H[i][1];
}
for(int i=0; i<n-1; ++i) cin >> L[i];
cout << best_path(n, k, H, L) << "\n";
return 0;
}
