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> //Andrei Alexandru a.k.a Sho10
#include "race.h"
#define ll long long
#define double long double
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define aint(a) (a).begin(), (a).end()
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pi pair
#define rc(s) return cout<<s,0
#define endl '\n'
#define mod 1000000007
#define PI 3.14159265359
#define MAXN 100005
#define INF 1000000005
#define LINF 1000000000000000005ll
#define CODE_START ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll ans=LINF,k;
vector<pair<ll,ll>>g[200005];
map<ll,ll>d,dist;
set<pair<ll,ll>>s[200005];
void dfs(ll node,ll par){
s[node].insert(mp(dist[node],d[node]));
for(auto it : g[node]){
if(it.f==par){
continue;
}
dist[it.f]=dist[node]+it.s;
d[it.f]=d[node]+1;
dfs(it.f,node);
if(s[node].size()<s[it.f].size()){
swap(s[node],s[it.f]);
}
for(auto it1 : s[it.f]){
auto pos=s[node].lower_bound(mp(k-it1.f+2*dist[node],0ll));
if(pos!=s[node].end()&&pos->f+it1.f-2*dist[node]==k){
//cout<<it1.s<<' '<<pos->s<<' '<<2*d[node]<<endl;
ans=min(ans,it1.s+pos->s-2*d[node]);
}
}
for(auto it1 : s[it.f]){
s[node].insert(it1);
}
}
}
int best_path(int n,int K,int h[][2],int l[]){
for(ll i=0;i<n-1;i++)
{
g[h[i][0]].pb(mp(h[i][1],l[i]));
g[h[i][1]].pb(mp(h[i][0],l[i]));
}
k=K;
dfs(0,-1);
if(ans==LINF){
ans=-1;
}
return ans;
}
/*
int32_t main(){
CODE_START;
int a[5][2];
a[0][0]=0;
a[0][1]=1;
a[1][0]=0;
a[1][1]=2;
int nr[4];
nr[0]=1;
nr[1]=2;
nr[2]=4;
cout<<best_path(3,3,a,nr)<<endl;
}
* */
# | 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... |