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 <iostream>
#include <vector>
#include <queue>
#include "race.h"
using namespace std;
int n, k, a, b, w, sz, c, ans = 1e9;
vector <vector <pair <int, int>>> g(2e5);
vector <bool> del(2e5, false);
vector <int> mn(1e6 + 1, 1e9);
queue <int> q;
int find_c(int x) {
if (del[x]) return 0;
del[x] = true;
int mx = 0, sum = 1;
for (auto& p : g[x]) {
a = find_c(p.first);
mx = max(mx, a);
sum += a;
}
if (mx <= sz / 2 && sz - sum <= sz / 2) c = x;
del[x] = false;
return sum;
}
void dfs1(int x, int d, int wd) {
if (del[x] || wd > k) return;
del[x] = true;
ans = min(ans, d + mn[k - wd]);
for (auto& p : g[x]) dfs1(p.first, d + 1, wd + p.second);
del[x] = false;
}
void dfs2(int x, int d, int wd) {
if (del[x] || wd > k) return;
del[x] = true;
mn[wd] = min(mn[wd], d);
q.push(wd);
for (auto& p : g[x]) dfs2(p.first, d + 1, wd + p.second);
del[x] = false;
}
void solve(int x) {
if (del[x]) return;
sz = find_c(x);
find_c(x);
cout << endl << x << " " << sz << " " << c;
del[c] = true;
for (auto& p : g[c]) {
dfs1(p.first, 1, p.second);
dfs2(p.first, 1, p.second);
}
while (q.size()) { mn[q.front()] = 1e9; q.pop(); }
for (auto& p : g[c]) solve(p.first);
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N; k = K;
for (int i = 0; i < n - 1; i++) {
a = H[i][0];
b = H[i][1];
w = L[i];
g[a].push_back({ b,w });
g[b].push_back({ a,w });
}
mn[0] = 0;
solve(0);
return (ans < 1e9 ? 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... |