# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
933209 | thefless | 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>
using namespace std;
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define Times (1.0 * clock() / CLOCKS_PER_SEC)
#define fi first
#define base 31
#define sz(x) (int)(x).size()
#define se second
#define ll long long
#define mp make_pair
#define pb push_back
#define pii pair<ll, int>
#define all(x) (x).begin(), (x).end()
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); freopen(name".bug", "w", stderr); }
#define task "name"
#define tsk ""
const int oo = 1e9 + 7;
const ll loo = (ll)1e18 + 7;
const int MOD = 1e9 + 7;
const int MOD2 = 1e9 + 696969;
const int N = 2e5 + 3;
const int BASE = 10;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b){if (a > b) {a = b; return true;} return false;}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b){if (a < b) {a = b; return true;} return false;}
int n , k;
int high[N];
ll d[N];
int ans = oo;
vector<pii> adj[N];
map<ll , int> mpp[N];
void make(int u , int par){
mpp[u][d[u]] = high[u];
for(auto x : adj[u]){
int v = x.se;
int w = x.fi;
if(v == par) continue;
d[v] = d[u] + w;
high[v] = high[u] + 1;
make(v , u);
}
}
void dfs(int u , int par = -1){
int g = k + 2 * d[u];
for(auto x : adj[u]){
int v = x.se;
if(v == par) continue;
dfs(v , u);
if(sz(mpp[u]) < sz(mpp[v])) mpp[u].swap(mpp[v]);
for(auto e : mpp[v]){
if(mpp[u].find(g - e.fi) != mpp[u].end()) minimize(ans , mpp[u][g - e.fi] + e.se - 2 * high[u]);
}
for(auto e : mpp[v]){
if(mpp[u].find(e.fi) == mpp[u].end()) mpp[u].insert(e);
else minimize(mpp[u][e.fi] , e.se);
}
}
}
void Solve(){
cin >> n >> k;
for(int i = 1 ; i < n ; i++){
int u , v , w;
cin >> u >> v >> w;
adj[u].pb({w , v});
adj[v].pb({w , u});
}
make(0 , -1);
dfs(0 , -1);
cout << (ans == oo ? -1 : ans);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
file(task);
file(tsk);
int T = 1;
// cin >> T;
while(T--)
{
Solve();
}
cerr << "Time elapsed: " << Times << " s.\n";
return 0;
}
// author: thefless
// I love Kim Anh