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;
vector<vector<pair<int,int>>> adj;
int n, k;
vector<int> subtr;
vector<bool> vis;
unordered_map<int,int> mp1;
unordered_map<int,int>mp2;
int ans=INT_MAX;
int cnt=0;
void findsize(int u, int p){
    subtr[u]=1;
    for(auto [v,w]:adj[u]){
        if(v!=p and !vis[v]){
            findsize(v,u);
            subtr[u]+=subtr[v];
        }
    }
}
int findcentroid(int u, int p, int sz){
    for(auto [v,w]:adj[u]){
        if(v!=p and !vis[v] and subtr[v]>sz/2)return findcentroid(v,u,sz);
    }
    return u;
}
void dfs1(int u, int p, int dist, int depth){
    if(dist>k)return;
    if((mp1[k-dist]!=0 and mp2[k-dist]==cnt) or k==dist) ans=min(ans,mp1[k-dist]+depth);
    for(auto [v,w]:adj[u]){
        if(v!=p and !vis[v]){
            dfs1(v,u,dist+w,depth+1);
        }
    }
}
void dfs2(int u, int p, int dist, int depth){
    if(dist>k)return;
    if(mp1[dist]==0){
        mp1[dist]=depth;
        mp2[dist]=cnt;
    }
    else{
        if(mp2[dist]<cnt){
            mp1[dist]=depth;
            mp2[dist]=cnt;
        }
        else if(mp1[dist]>depth){
            mp1[dist]=depth;
            mp2[dist]=cnt;
        }
    }
    for(auto [v,w]:adj[u]){
        if(v!=p and !vis[v]){
            dfs2(v,u,dist+w,depth+1);
        }
    }
}
void buildcentroid(int u, int p){
    cnt++;
    findsize(u,u);
    int c=findcentroid(u,u,subtr[u]);
    vis[c]=true;
    mp2[0]=cnt;
    for(auto [v,w]:adj[c]){
        if(!vis[v]){
            dfs1(v,c,w,1);
            dfs2(v,c,w,1);
        }
    }
    for(auto [v,w]:adj[c]){
        if(!vis[v])buildcentroid(v,c);
    }
}
int best_path(int N, int K, int H[][2], int L[]){
    n=N;
    k=K;
    adj.resize(n);
    subtr.resize(n);
    vis.resize(n);
    for(int i=0;i<n-1;i++){
        adj[H[i][1]].push_back({H[i][0],L[i]});
        adj[H[i][0]].push_back({H[i][1],L[i]});
    }
    buildcentroid(0,-1);
    if(ans==INT_MAX)return -1;
    return ans;
} 
Compilation message (stderr)
race.cpp: In function 'void findsize(int, int)':
race.cpp:16:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   16 |     for(auto [v,w]:adj[u]){
      |              ^
race.cpp: In function 'int findcentroid(int, int, int)':
race.cpp:25:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   25 |     for(auto [v,w]:adj[u]){
      |              ^
race.cpp: In function 'void dfs1(int, int, int, int)':
race.cpp:34:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   34 |     for(auto [v,w]:adj[u]){
      |              ^
race.cpp: In function 'void dfs2(int, int, int, int)':
race.cpp:57:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   57 |     for(auto [v,w]:adj[u]){
      |              ^
race.cpp: In function 'void buildcentroid(int, int)':
race.cpp:70:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   70 |     for(auto [v,w]:adj[c]){
      |              ^
race.cpp:76:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   76 |     for(auto [v,w]:adj[c]){
      |              ^| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |