| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 373948 | duchung | 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.
#define ii pair<int , int>
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int n , k , ans = 1e9;
int dist[N] , depth[N];
vector<ii> adj[N];
set<ii> S[N];
void dfs(int u , int p = -1)
{
    S[u].insert(make_pair(dist[u] , depth[u]));
    for (auto e : adj[u])
    {
        int v = e.first;
        int w = e.second;
        if (v == p) continue;
        dist[v] = dist[u] + w;
        depth[v] = depth[u] + 1;
        dfs(v , u);
        if (S[u].size() < S[v].size()) swap(S[u] , S[v]);
        for (auto x : S[v])
        {
            auto it = S[u].lower_bound(make_pair(k - x.first + 2 * dist[u] , 0));
            if (it != S[u].end() && it->first + x.first - 2 * dist[u] == k) ans = min(ans , it->second + x.second - 2 * depth[u]);
        }
        for (auto x : S[v])
        {
            S[u].insert(x);
        }
    }
}
int main()
{
    // freopen(".inp","r",stdin);
    // freopen(".out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    for (int i = 1 ; i < n ; ++i)
    {
        int u , v , w;
        cin >> u >> v >> w;
        ++u ; ++v;
        adj[u].push_back(ii(v , w));
        adj[v].push_back(ii(u , w));
    }
    dfs(1);
    cout << (ans == 1e9 ? -1 : ans);
}
