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>
using namespace std;
const int sz = 2e5 + 5;
int n, k;
vector<vector<pair<int, int>>> adj(sz);
map<int, int> a[sz];
long long sum[sz], dep[sz], res;
void comp(int u, int p, long long c, int h) {
a[u][c] = h;
sum[u] = c;
dep[u] = h;
for (auto n : adj[u]) {
if (p == n.first) { continue; }
comp(n.first, u, c + n.second, h + 1);
}
}
void dfs(int v, int p) {
int t = k + 2 * sum[v];
for (auto i : adj[v]) {
if (i.first == p) {
continue;
}
if (a[i.first].size() > a[v].size()) {
swap(a[i.first], a[v]);
}
for (auto j : a[i.first]) {
if (a[v].find(t - j.first) != a[v].end()) {
res = min(res, a[v][t - j.first] + j.second - 2 * dep[v]);
}
}
for (auto j : a[i.first]) {
if (a[v].find(j.first) == a[v].end()) {
a[v].insert(j);
} else {
a[v][j.first] = min(a[v][j.first], j.second);
}
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
if (K == 1) {
return 0;
}
n = N;
k = K;
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]});
}
res = INT_MAX;
comp(0, -1, 0, 0);
dfs(0, -1);
return (res == INT_MAX ? -1 : res);
}
# | 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... |