# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
591889 | thezomb1e | 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.
//thatsramen
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define eb emplace_back
#define pb push_back
#define ft first
#define sd second
#define pi pair<int, int>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define dbg(...) dbg_out(__VA_ARGS__)
using ll = long long;
using ld = long double;
using namespace std;
using namespace __gnu_pbds;
//Constants
const ll INF = 5 * 1e18;
const int IINF = 2 * 1e9;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ld PI = 3.14159265359;
//Templates
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) {return os << '(' << p.first << ", " << p.second << ')';}
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '['; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << ']';}
void dbg_out() {cerr << endl;}
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << H << ' '; dbg_out(T...); }
template<typename T> void mins(T& x, T y) {x = min(x, y);}
template<typename T> void maxs(T& x, T y) {x = max(x, y);}
template<typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using omset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
//order_of_key(k): number of elements strictly less than k
//find_by_order(k): k-th element in the set
void setPrec() {cout << fixed << setprecision(15);}
void unsyncIO() {cin.tie(0)->sync_with_stdio(0);}
void setIn(string s) {freopen(s.c_str(), "r", stdin);}
void setOut(string s) {freopen(s.c_str(), "w", stdout);}
void setIO(string s = "") {
unsyncIO(); setPrec();
if(s.size()) setIn(s + ".in"), setOut(s + ".out");
}
// #define TEST_CASES
void solve() {
int n; ll k;
cin >> n >> k;
vector<vector<pi>> e(n + 5);
for (int i = 0; i < n - 1; i++) {
int u, v; cin >> u >> v;
e[u].pb({v, i}); e[v].pb({u, i});
}
vector<int> l(n);
for (int i = 0; i < n - 1; i++) {
cin >> l[i];
}
vector<map<ll, int>> comp(n + 5);
vector<int> sz(n + 5, 1), len(n + 5, 0);
vector<ll> dist(n + 5, 0);
function<void(int, int)> dfs_size = [&](int f, int p) {
for (auto [to, id] : e[f]) {
if (to == p) continue;
len[to] = len[f] + 1;
dist[to] = dist[f] + l[id];
dfs_size(to, f);
sz[f] += sz[to];
}
};
int ans = IINF;
function<void(int, int)> dfs = [&](int f, int p) {
int lrg = -1;
for (auto [to, id] : e[f]) {
if (to == p) continue;
if (lrg == -1 || sz[to] > sz[lrg]) {
lrg = to;
}
}
for (auto [to, id] : e[f]) {
if (to == p || to == lrg) continue;
dfs(to, f);
}
if (lrg != -1) {
dfs(lrg, f);
swap(comp[f], comp[lrg]);
}
comp[f][dist[f]] = len[f];
for (auto [to, id] : e[f]) {
if (to == p || to == lrg) continue;
for (auto &[x, y] : comp[to]) {
//dist[u] + dist[v] - dist[lca] = k
//dist[u] = k + dist[lca] - dist[v]
ll need = k + dist[f] - x;
if (comp[f].count(need)) {
mins(ans, y + comp[f][need] - 2 * len[f]);
}
}
for (auto &[x, y] : comp[to]) {
if (comp[f].count(x))
mins(comp[f][x], y);
else comp[f][x] = y;
}
}
if (comp[f].count(k + dist[f])) {
mins(ans, comp[f][k + dist[f]] - len[f]);
}
};
dfs_size(0, -1);
dfs(0, -1);
if (ans == IINF) ans = -1;
cout << ans;
}
int main() {
setIO();
int tt = 1;
#ifdef TEST_CASES
cin >> tt;
#endif
while (tt--)
solve();
return 0;
}