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;
vector<vector<pair<int, int>>> adj(200000);
vector<map<int, int>> mp(200000);
int offsetw[200000];
int offsetl[200000];
int k;
int ans = INT_MAX;
void dfs(int c, int p, int w = 0) {
mp[c][0] = 0;
offsetl[c] = 0;
offsetw[c] = 0;
for (auto &i : adj[c]) {
if (i.first == p) continue;
int x = i.first;
dfs(x, c, i.second);
if (mp[c].size() < mp[x].size()) {
swap(mp[x], mp[c]);
swap(offsetl[c], offsetl[x]);
swap(offsetw[c], offsetw[x]);
}
for (auto &j : mp[x]) {
auto it = mp[c].find(k - (j.first + offsetw[x]) - offsetw[c]);
if (it != mp[c].end()) ans = min(ans, j.second + it->second + 1 + offsetl[c] + offsetl[x]);
}
for (auto &j : mp[x]) {
if (j.first + offsetw[x] > k) continue;
auto it = mp[c].find(j.first + offsetw[x] - offsetw[c]);
if (it != mp[c].end()) {
it->second = min(it->second, j.second + offsetl[x] - offsetl[c]);
}
else {
mp[c][j.first + offsetw[x] - offsetw[c]] = j.second + offsetl[x] - offsetl[c];
}
}
}
offsetl[c]++;
offsetw[c] += w;
}
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, -1);
return (ans == INT_MAX ? -1 : (ans - 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... |