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 u, int p) {
long long target = k + 2 * sum[u];
for (auto n : adj[u]) {
if (n.first == p) { continue; }
dfs(n.first, u);
if (a[n.first].size() > a[u].size()) { swap(a[n.first], a[u]); }
for (auto i : a[n.first]) {
if (a[u].find(target - i.first) != a[u].end()) {
res = min(res, a[u][target - i.first] + i.second - 2 * dep[u]);
}
}
for (auto i : a[n.first]) {
if (a[u].find(i.first) == a[u].end()) {
a[u].insert(i);
} else {
a[u][i.first] = min(a[u][i.first], i.second);
}
}
}
}
int best_path(int N, int K, int H[][2], int L[]) {
if (k == 1) { return 0; }
n = N;
k = K;
res = INT_MAX;
for (int i = 0; i < n - 1; i++) {
int u = H[i][0];
int v = H[i][1];
adj[u].push_back(pair<int,int> (v, L[i]));
adj[v].push_back(pair<int,int> (u, L[i]));
}
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... |