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>
using namespace std;
#define TL ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define rall(s) s.rbegin(),s.rend()
#define all(s) s.begin(),s.end()
#define pb push_back
#define se second
#define fi first
#define ll long long
#define ld long double
#define YES cout<<"YES\n"
#define Yes cout<<"Yes\n"
#define yes cout<<"yes\n"
#define NO cout<<"NO\n"
#define No cout<<"No\n"
#define no cout<<"no\n"
const int N = 5e5 + 9 , mod = 1e9 + 7;
ll a[N] , b[N] , dp[N] , c[N] , d[N] , sz[N] , us[N];
vector<pair<int,int>>v[N];
int dfs_sz(int n , int par = 0){
sz[n] = 1;
for(auto to : v[n])
if(!us[to.fi] && to.fi != par)
sz[n] += dfs_sz(to.fi , n);
return sz[n];
}
ll ans = 1e9 , k;
int get_centroid(int n , int par , int s){
for(auto to : v[n])
if(!us[to.fi] && to.fi != par && sz[to.fi] > s / 2)
return get_centroid(to.fi , n , s);
return n;
}
set<pair<ll,ll>>st,st1;
void dfs(int n , int par, ll dis , ll cnt){
st1.insert({dis , cnt});
if(dis == k)
ans = min(ans , cnt);
auto it = st.lower_bound({k - dis , 0});
if(it->fi == k - dis)
ans = min(ans , cnt + it->se);
for(auto to : v[n])
if(!us[to.fi] && to.fi != par)
dfs(to.fi , n , dis + to.se , cnt + 1);
}
void centroid(int n){
int x = get_centroid(n , 0 , dfs_sz(n));
us[x] = 1;
st.clear();
for(auto to : v[x]){
if(!us[to.fi]){
st1.clear();
dfs(to.fi , x , to.se , 1);
for(auto it : st1)
st.insert(it);
}
}
for(auto to : v[x])
if(!us[to.fi])
centroid(to.fi);
}
int best_path(int N, int K, int H[][2], int L[]){
k = K;
for(int i = 0; i < N - 1; i++) {
int x = H[i][0] + 1,y = H[i][1] + 1;
v[x].pb({y, L[i]});
v[y].pb({x, L[i]});
}
centroid(1);
if(ans > N)
ans = -1;
return ans;
}
// Author : حسن
# | 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... |