#include "bits/stdc++.h"
#include "race.h"
using namespace std;
using ll = long long;
const int inf = 1e9, N = 2e5 + 5;
int ans = inf, k;
vector<vector<pair<int, int>>> g(N);
vector<int> par(N, -1), heavy(N), height2(N);
vector<ll> height(N);
vector<set<pair<ll, int>>> st(N);
/*
height[heavy[u]] - height[v] = k +
height[v] - height[u] = k?????
height[heavy[u]] - k - height[u] = height[v]
*/
int dfs(int u) {
heavy[u] = u;
int size = 1, max_child_size = 0;
for (auto [v, w]: g[u]) {
if (par[u] == v) continue;
height[v] = height[u] + w;
height2[v] = height2[u] + 1;
par[v] = u;
int child_size = dfs(v);
if (child_size > max_child_size) {
heavy[u] = heavy[v];
max_child_size = child_size;
}
size += child_size;
}
return size;
}
void dfs2(int u) {
for (auto [v, w]: g[u]) {
if (par[u] == v) continue;
dfs2(v);
}
for (auto [v, w]: g[u]) {
if (par[u] == v or heavy[u] == heavy[v]) continue;
for (auto it: st[heavy[v]]) {
st[heavy[u]].insert(it);
}
st[heavy[v]].clear();
}
st[heavy[u]].emplace(height[u], height2[u]);
vector<pair<ll, int>> del;
pair<ll, int> last = {-1, -1};
for (auto it: st[heavy[u]]) {
ll need = k - (it.first - height[u]) + height[u];
auto it2 = st[heavy[u]].lower_bound(make_pair(need, 0));
if (it2 != st[heavy[u]].end() and it2->first == need) {
ans = min(ans, it.second + it2->second - 2 * height2[u]);
}
if (it.first == last.first) {
del.emplace_back(it);
}
last = it;
}
for (auto it: del) st[heavy[u]].erase(it);
}
int best_path(int n, int K, int H[][2], int L[]) {
k = K;
for (int i = 0; i < n - 1; i++) {
g[H[i][0]].emplace_back(H[i][1], L[i]);
g[H[i][1]].emplace_back(H[i][0], L[i]);
}
// change it
dfs(0);
dfs2(0);
return ans == inf ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
20316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
20316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
20316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
20316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |