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>
#define ll long long
#define f first
#define s second
using namespace std;
const int MAXN = 2e5+10;
vector<pair<ll,ll>> adj[MAXN];
map<ll,ll> help[MAXN];
ll dep[MAXN], dist[MAXN],ans;
int k,n;
void dfs(int u, int p, int de, ll di){
dep[u] = de; dist[u] = di;
help[u][di] = de;
for(auto pa : adj[u]){
if(pa.f != p){
dfs(pa.f,u,dep[u]+1,dist[u]+pa.s);
}
}
}
void comb(int u, int p){
int tar = k+2*dist[u];
for(auto pa : adj[u]){
int v = pa.f;
if(v != p){
comb(v,u);
if(help[v].size() > help[u].size()) swap(help[v],help[u]);
for(auto x : help[v]){
if(help[u].find(tar-x.f) != help[u].end()){
ans = min(ans,help[u][tar-x.f]+x.s-2*dep[u]);
}
}
for(auto x : help[v]){
if(help[u].find(x.f) != help[u].end()){
help[u][x.f] = min(help[u][x.f],x.s);
}
else{
help[u].insert(x);
}
}
}
}
}
int best_path(int N, int K, int H[][2], int L[]){
for(int i = 0;i<N;++i){
int u = H[i][0], v = H[i][1], w = L[i];
adj[u].push_back({v,w}); adj[v].push_back({u,w});
}
dfs(0,-1,0,0);
n = N; k = K; ans = INT_MAX;
comb(0,-1);
if(ans != INT_MAX) return ans;
else return -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... |