#include "race.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define fst first
#define snd second
#define pb push_back
#define forn(i,a,b) for(int i = a; i<b; i++)
#define SZ(x) (int)x.size()
#define ALL(x) x.begin(),x.end()
#define mset(a,v) memset(a,v,sizeof(a))
#define FIN ios::sync_with_stdio(0); cout.tie(); cin.tie(0);
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
template <typename L>
using iset = tree<L,null_type,less<L>,rb_tree_tag,tree_order_statistics_node_update>;
const int MAXN = 200000+5;
ll n;
ll k;
vector<pair<ll,ll>> adj[MAXN];
vector<pair<ll,ll>> nadj[MAXN];
ll realv[MAXN];
ll rres = 1000000000;
void dfs(ll nd, ll p,ll dep, ll sz, unordered_map<ll,ll> &path){
//cout<<"ND "<<nd<<"----------------\n";
for(auto i:adj[nd]) if(i.fst!=p){
unordered_map<ll,ll> pathc;
dfs(i.fst,nd,dep+1,sz+i.snd,pathc);
//cout<<i.fst<<": "; for(auto j:pathc) cout<<j.fst<<" "<<j.snd<<" || "; cout<<'\n';
if(SZ(pathc)>SZ(path)) swap(pathc,path);
for(auto [szchild,rdep]:pathc){
if(path.find(k-szchild+sz)!=path.end()){
rres=min(rres,rdep-dep+path[k-szchild+sz]-dep);
}
}
for(auto [szchild,rdep]:pathc){
if(path.find(szchild)==path.end()){
path[szchild]=rdep;
}else{
path[szchild]=min(path[szchild],rdep);
}
}
//cout<<"New path de "<<i.fst<<": "; for(auto j:path) cout<<j.fst<<" "<<j.snd<<" |||| "; cout<<'\n';
}
if(path.find(sz+k)!=path.end()){
rres=min(rres,path[sz+k]-dep);
}
path[sz]=dep;
}
void opti(ll nd, ll p,ll &aux){
realv[nd]=aux;
for(auto i:nadj[nd])if(i.fst!=p){
aux++;
opti(i.fst,nd,aux);
}
}
int best_path(int N, int K, int H[][2], int L[]){
//cout<<"inica\n";
FIN;
n = N;
k = K;
forn(i,0,n-1){
nadj[H[i][0]].pb({H[i][1],L[i]});
nadj[H[i][1]].pb({H[i][0],L[i]});
}
ll aux = 0;
opti(0,-1,aux);
forn(i,0,n-1){
adj[realv[H[i][0]]].pb({realv[H[i][1]],L[i]});
adj[realv[H[i][1]]].pb({realv[H[i][0]],L[i]});
}
unordered_map<ll,ll> path;
dfs(0,-1,0,0,path);
return (rres!=1000000000?rres:-1);
}
| # | 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... |