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>
#include "race.h"
using namespace std;
const int mx = 2e5+1;
#define F first
#define S second
vector<vector<pair<int,int>>> adj(mx);
map<int,int> arr[mx];
int ans=1e9;
void dfs(int node, int par, int dep, int depth, int k){
for(auto x: adj[node])if(x.F != par){
dfs(x.F, node, dep+1, depth + x.S, k);
if(arr[x.F].size() > arr[node].size()) swap(arr[node], arr[x.F]);
for(auto thing: arr[x.F])if(arr[node].count(k + 2 * depth - thing.F)) ans = min(ans, arr[node][k+2*depth-thing.F]+thing.S - 2*dep);
for(auto thing: arr[x.F]){
if(arr[node].count(thing.F)) arr[node][thing.F] = min(arr[node][thing.F], thing.S);
else arr[node][thing.F] = thing.S;
}
}
if(arr[node].count(k + depth)) ans = min(ans, arr[node][k+depth] - dep);
arr[node][depth] = dep;
}
int best_path(int n, int k, int h[][2], int l[]){
for(int i=0; i<n-1; i++){
adj[h[i][0]].push_back({h[i][1], l[i]});
adj[h[i][1]].push_back({h[i][0], l[i]});
}
dfs(0,0,0,0,k);
return (ans == 1e9 ? -1 : 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... |