# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1162881 | tsengang | Race (IOI11_race) | C++17 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
//#include "race.h"
using namespace std;
#define ll int
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define ertunt return
#define vodka void
vector<pair<int,int>>v[1000006];
ll ans = 1e18;
bool vis[200006];
bool ckd = 0;
vodka dfs(ll x,queue<int>q,ll cur,int K){
if(vis[x])ertunt;
vis[x] = 1;
if(cur == K){
ans = min(ans,(ll)q.size());
ckd = 1;
}
while(cur > K){
cur -= q.front();
q.pop();
}
for(auto [z,y] : v[x]){
queue<int> g = q;
g.push(y);
dfs(z,g,cur+y,K);
}
}
int best_path(int N,int K,int H[][2],int L[]){
for(ll i = 0; i < N-1; i++){
v[H[i][0]].pb({H[i][1],L[i]});
v[H[i][1]].pb({H[i][0],L[i]});
}
vector<int>d;
for(ll i = 0; i < N; i++){
if(v[i].size() == 1)d.pb(i);
}
for(auto i : d){
queue<int>f;
dfs(i,f,0,K);
fill(vis,vis+N+5,0);
}
if(ckd == 1)
ertunt ans;
ertunt -1;
}
int main(){
int n,k;
cin >> n >> k;
ll a[n-1][2];
ll b[n-1];
for(ll i = 0; i < n-1; i++)cin >> a[i][0] >> a[i][1];
for(ll i = 0; i < n-1; i++)cin >> b[i];
ll c = best_path(n,k,a,b);
cout << c;
}