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;
#define ll long long
#define debug if(0)
const int N = 2e5 + 4;
const int inf = 1e9+7;
vector<pair<int, ll>> V[N];
ll dist[N], depth[N], K;
map<ll, int> M[N]; // map [dist, depth]
ll ans = inf;
void dfs(int v, int p=-1) {
M[v][dist[v]] = depth[v];
for(auto e : V[v]) {
int u = e.first; ll w = e.second;
if(u != p) {
dist[u] = dist[v]+w;
depth[u] = depth[v]+1;
dfs(u, v);
}
}
}
void dfs2(int v, int p=-1) {
for(auto e : V[v]) {
int u = e.first;
if(u == p) continue;
dfs2(u, v);
if((int) M[v].size() < (int) M[u].size()) swap(M[v], M[u]);
for(auto it = M[u].begin(); it != M[u].end(); ++it) {
if(M[v].find((K+2LL*dist[v]-it->first)) != M[v].end()) { ans = min(ans, M[v][(K+2LL*dist[v]-it->first)]+it->second-2*depth[v]); }
}
for(auto it = M[u].begin(); it != M[u].end(); ++it) {
if(M[v].find(it->first) != M[v].end()) M[v][it->first] = min(M[v][it->first], it->second);
else M[v][it->first] = it->second;
}
}
}
int best_path(int n, int k, int h[][2], int l[]) {
for(int i=0; i<(n-1); i++) {
V[h[i][0]].push_back({h[i][1], l[i]});
V[h[i][1]].push_back({h[i][0], l[i]});
}
K = k;
dfs(0);
dfs2(0);
return (ans == inf ? -1 : (int) 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... |