# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
933502 | Juanchoki | Race (IOI11_race) | C++14 | 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>
#include <vector>
//#include "race.h"
using namespace std;
int mini = 1e9;
const int maxn = 2e5 + 13;
struct edge
{
int u, w;
};
vector<edge> adj[maxn];
struct par
{
int dist, arista;
};
bool operator < (const par &a, const par &b)
{
return a.dist < b.dist;
}
map<int, int> mp[maxn];
int k;
void dfs(int nodo, int padre)
{
for (edge e: adj[nodo])
{
if (e.u == padre) continue;
dfs(e.u, nodo);
if (mp[e.u].size() <= mp[nodo].size())
swap(mp[e.u], mp[nodo]);
for (auto it: mp[e.u])
{
int yo = it.first;
if (mp[nodo].count(k-yo))
mini = min (mini, mp[nodo][k-yo] + it.second);
int nuevo = it.first + e.w;
int aristas = it.second+1;
if (mp[nodo].count(nuevo))
if (mp[nodo][nuevo] < aristas)
mp[nodo][nuevo] = aristas;
else
{}
else
mp[nodo][nuevo] = aristas;
}
}
if (mp[nodo].count(k)) mini = min (mini, mp[nodo][k]);
}
int best_path(int N, int K, int H[][2], int L[])
{
k = K;
for (int i = 0; i < N-1; i++)
{
adj[H[i][0]].push_back({H[i][1], L[i]});
adj[H[i][1]].push_back({H[i][0], L[i]});
}
for (int i = 0; i < N; i++)
mp[i].insert({0, 0});
dfs(0, -1);
return mini;
}
int main()
{
int n, k; cin >> n >> k;
int arr[n-1][2], l[n];
for (int i = 0; i < n-1; i++) cin >> arr[i][0] >> arr[i][1];
for (int i = 0; i < n; i++) cin >> l[i];
cout << best_path(n, k, arr, l);
}