# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
946923 | Esgeer | 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.
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <unistd.h>
using namespace __gnu_pbds;
template <class T>
using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#ifndef Local
#pragma GCC optimize("O3,unroll-loops")
#include "race.h"
#endif
#define int long long
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define vpi vector<pii>
#define vvpi vector<vpi>
#define vb vector<bool>
#define vvb vector<vb>
#define endl '\n'
#define sp << " " <<
#define F(i, s, n) for(int i = s; i < n; i++)
#define pb push_back
#define fi first
#define se second
int inf = 1e18;
const int N = 2e5+5;
int32_t l[N];
int32_t h[N][2];
vpi adj[N];
map<int, int> mp[N];
int k;
int ans = inf;
void dfs(int node, int par, int depth, int depl) {
for(pii go : adj[node]) if(go.fi != par) dfs(go.fi, node, depth+1, depl+go.se);
mp[node][depl] = depth;
for(pii go : adj[node]) if(go.fi != par) {
if(mp[go.fi].size() > mp[node].size()) swap(mp[go.fi], mp[node]);
for(pii el : mp[go.fi]) {
int remK = k - (el.fi - depl);
if(mp[node][remK + depl]) {
ans = min(ans, mp[node][remK + depl] + el.se - depth*2);
}
}
for(pii el : mp[go.fi]) {
if(mp[node].find(el.fi) != mp[node].end()) mp[node][el.fi] = min(mp[node][el.fi], el.se);
else mp[node][el.fi] = el.se;
}
}
}
int32_t best_path(int32_t n, int32_t k, int32_t h[][2], int32_t l[]) {
F(i, 0, n-1) {
int u = h[i][0], v = h[i][1];
adj[u].pb({v, l[i]});
adj[v].pb({u, l[i]});
}
::k = k;
dfs(0, -1, 0, 0);
if(ans < 1e16) return ans;
else return -1;
}
void solve() {
int32_t n, k;
cin >> n >> k;
F(i, 0, n-1) cin >> h[i][0] >> h[i][1];
F(i, 0, n-1) cin >> l[i];
cout << best_path(n, k, h, l) << endl;
}
void setIO() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef Local
freopen("local.in", "r", stdin);
freopen("local.out", "w", stdout);
#else
// freopen("disrupt.in","r",stdin);
// freopen("disrupt.out","w",stdout);
#endif
}
signed main() {
setIO();
int t = 1;
//cin >> t;
while(t--) solve();
}