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 "race.h"
#include<bits/stdc++.h>
#define MAXN 200005
using namespace std;
typedef long long int lol;
vector<vector<pair<int,int> > > g(MAXN);
vector<int> subsz(MAXN);
bitset<MAXN> del;
int ans=INT_MAX;
void dfssz(int u,int p=-1)
{
    subsz[u]=1;
    for(auto ch:g[u])
    {
        if(ch.first==p) continue;
        if(!del.test(ch.first))
        {
            dfssz(ch.first,u);
            subsz[u]+=subsz[ch.first];
        }
    }
}
int getcen(int u,int sz,int p=-1)
{
    for(auto ch:g[u])
    {
        if(ch.first==p) continue;
        if(!del.test(ch.first))
        {
            if(subsz[ch.first]>sz/2)    return getcen(ch.first,sz,u);
        }
    }
    return u;
}
void place(map<int,int> &m,map<int,int> &m2,int len,int nm,int K)
{
    if(m2.find(len)==m2.end())
    {
        m2[len]=nm;
    }else
    {
        m2[len]=min(m2[len],nm);
    }
    if(m.find(K-len)!=m.end())
    {
        ans=min(ans,m2[len]+m[K-len]);
    }
}
void solve(int u,map<int,int> &m,map<int,int> &m2,int len,int K,int dep,int p)
{
    for(auto ch:g[u])
    {
        if(ch.first==p) continue;
        if(!del.test(ch.first))
        {
            if(len+ch.second<=K)
            {
                place(m,m2,len+ch.second,1+dep,K);
                solve(ch.first,m,m2,len+ch.second,K,1+dep,u);
            }
        }
    }
}
void centroid(int u,const int& K)
{
    //find subtree sizes
    dfssz(u);
    //get centroid
    int C=getcen(u,subsz[u]);
    //cout<<"C: "<<C<<endl;
    //solve
    map<int,int> m;
    for(auto ch:g[C])
    {
        if(!del.test(ch.first))
        {
            if(ch.second<=K)
            {
                map<int,int> m2;
                place(m,m2,ch.second,1,K);
                solve(ch.first,m,m2,ch.second,K,1,C);
                for(auto p:m2)
                {
                    if(m.find(p.first)==m.end())    m[p.first]=p.second;
                    else    m[p.first]=min(m[p.first],p.second);
                }
            }
        }
    }
    /*for(auto p:m)
    {
        cout<<p.first<<": ";
        for(auto e:p.second)   cout<<"("<<e.first<<","<<e.second<<") ";
        cout<<endl;
    }*/
    if(m.find(K)!=m.end())  ans=min(ans,m[K]);
    //end
    del.flip(C);
    for(auto ch:g[C])
    {
        if(!del.test(ch.first)) centroid(ch.first,K);
    }
}
 
int best_path(int N, int K, int H[][2], int L[])
{
    for(int i=0;i<N-1;i++)
    {
        int u=H[i][0],v=H[i][1];
        g[u].emplace_back(v,L[i]);
        g[v].emplace_back(u,L[i]);
    }
    centroid(0,K);
    if(ans==INT_MAX)  return -1;
    else  return ans;
}
| # | 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... |