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>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define mp make_pair
#define pub push_back
#define pob pop_back()
#define ss second
#define ff first
#define mt make_tuple
#define pof pop_front()
#define fbo find_by_order
#define ook order_of_key
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
int i,ans=1e9,c[200001],used[200001];
vector<pair<int,int> >v[200001];
map<int,int>xox,yoy;
inline void dfs(int u, int par){
for(auto x : v[u]) if(x.ff!=par) dfs(x.ff,u);
c[par]+=c[u];
}
inline void dfs1(int u, int parr, int s, int d){
if(yoy.find(s) == yoy.end())yoy[s]=d;
else yoy[s]=min(yoy[s],d);
for(auto x : v[u]){
if(x.ff!=parr){
s+=x.ss;
d++;
dfs1(x.ff,u,s,d);
}
}
}
inline int fnd(int u , int sz){
for(auto x : v[u]){
if(!used[x.ff] && c[x.ff]>sz){
c[u]-=c[x.ff];
c[x.ff]+=c[u];
return fnd(x.ff,sz);
}
}
return u;
}
inline void solve(int at, int k){
at=fnd(at,c[at]/2);
used[at]=1;
xox.clear();
xox[0]=0;
for(auto z : v[at]){
if(used[z.ff]) continue;
yoy.clear();
dfs1(z.ff,at,z.ss,1);
for(auto haha : yoy) if(xox.find(k-haha.ff)!=xox.end()) ans=min(ans,haha.ss+xox[k-haha.ff]);
for(auto lol : yoy){
if(xox.find(lol.ff)==xox.end()) xox[lol.ff]=lol.ss;
else xox[lol.ff]=min(xox[lol.ff],lol.ss);
}
}
for(auto hha : v[at]) if(!used[hha.ff]) solve(hha.ff,k);
}
int best_path(int N, int K, int H[][2], int L[]) {
for(i=0;i<N-1;i++){
c[i]=1;
v[H[i][0]].pub(mp(H[i][1],L[i]));
v[H[i][1]].pub(mp(H[i][0],L[i]));
}
c[N-1]=1;
dfs(0,0);
solve(0,K);
if(ans==1e9) ans=-1;
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... |