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>
using namespace std;
int K, ans = INT_MAX; vector<pair<int, int>> adj[200005]; set<pair<int, int>> S[200005];
void dfs(int u, int p, int dis, int dep){
S[u].insert({dis, dep});
for (auto v : adj[u]) if (v.first != p){
dfs(v.first, u, dis + v.second, dep + 1);
if (S[u].size() < S[v.first].size()) swap(S[u], S[v.first]);
for (auto d : S[v.first]){
int other = K + 2 * dis - d.first; auto x = S[u].lower_bound({other, -1});
if (x != S[u].end() and (*x).first == other) ans = min(ans, d.second + (*x).second - 2 * dep);
}for (auto d : S[v.first]) S[u].insert(d);
}
}
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]});
}K = k; dfs(0, 0, 0, 0); return (ans == INT_MAX) ? -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... |