| # | Time | Username | Problem | Language | Result | Execution time | Memory |
|---|---|---|---|---|---|---|---|
| 739882 | vjudge1 | 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.
/*
Free at last, Free at last, Thank God Almighty, We are free at last.
*/
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template<class T> using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#define ll long long
#define nl cout << '\n'
#define pri(n) cout<<fixed<<setprecision(n)
#define d5l_arr(arr, n) for(int i = 0 ; i < n ; i ++)cin >> arr[i]
const
double eps = -1e-9;
//const long double pi = 3.14159265358979323846;
const ll MOD = 1e9 + 7;
const int inf = 1e9 + 1;
const int N = 2e5 + 1;
void m4_htgeb_tle_kda_ya3ny() {
cin.tie(0);
cin.sync_with_stdio(0);
cout.tie(0);
cout.sync_with_stdio(0);
}
int ans = inf, n, k;
vector<pair<int, int>> adj[N];
map<ll, int> dfs(int u, int p, ll w, int dep) {
map<ll, int> leader, other_leader;
leader[w] = dep;
for (auto v: adj[u]) {
if (v.first == p)continue;
other_leader = dfs(v.first, u, w + v.second, dep + 1);
if (other_leader.size() > leader.size())swap(leader, other_leader);
for (auto el: other_leader) {
ll rem = el.first - w;
ll look_for = (k - rem) + w;
if (leader.count(look_for))ans = min(ans, (el.second - dep) + (leader[look_for] - dep));
}
for (auto el: other_leader)leader.insert(el);
other_leader.clear();
}
return leader;
}
void solveCase() {
cin >> n >> k;
vector<int> edges[n - 1];
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
edges[i].push_back(u);
edges[i].push_back(v);
}
for (int i = 0; i < n - 1; ++i) {
int w;
cin >> w;
adj[edges[i][0]].emplace_back(edges[i][1], w);
adj[edges[i][1]].emplace_back(edges[i][0], w);
}
dfs(0, 0, 0, 0);
cout << ((ans < inf) ? ans : -1);
}
int main() {
m4_htgeb_tle_kda_ya3ny();
int t = 1;
// cin >> t;
for (int i = 1; i <= t; ++i) {
solveCase();
nl;
}
return 0;
}
